summaryrefslogtreecommitdiff
path: root/test/ChunkedEncodingTest.cc
blob: 0515d80e67bf4725e444655a872148669dfcfabb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "ChunkedEncoding.h"
#include <fstream>
#include <iostream>
#include <string>
#include <cppunit/extensions/HelperMacros.h>

using namespace std;

class ChunkedEncodingTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(ChunkedEncodingTest);
  CPPUNIT_TEST(testInflate1);
  CPPUNIT_TEST(testInflateLargeChunk);
  CPPUNIT_TEST_SUITE_END();
private:
  ChunkedEncoding* enc;
public:
  void setUp() {
    enc = new ChunkedEncoding();
    enc->init();
  }

  void testInflate1();
  void testInflateLargeChunk();

};


CPPUNIT_TEST_SUITE_REGISTRATION( ChunkedEncodingTest );

void ChunkedEncodingTest::testInflate1() {
  string msg = "a\r\n1234567890\r\n";
  char buf[100];
  int len = sizeof(buf);
  enc->inflate(buf, len, msg.c_str(), msg.size());
  buf[len] = '\0';
  CPPUNIT_ASSERT_EQUAL(string("1234567890"), string(buf));
  // second pass
  len = sizeof(buf);
  msg = "3;extensionIgnored\r\n123\r\n0\r\n";
  enc->inflate(buf, len, msg.c_str(), msg.size());
  buf[len] = '\0';
  CPPUNIT_ASSERT_EQUAL(string("123"), string(buf));
  // input is over
  CPPUNIT_ASSERT(enc->finished());
}

void ChunkedEncodingTest::testInflateLargeChunk() {
  // give over 4096 character chunk
  fstream is("4096chunk.txt", ios::in);
  if(is.fail()) {
    CPPUNIT_FAIL("cannot open file 4096chunk.txt");
  }
  string body;
  is >> body;
  char buf[4097];
  int len = sizeof(buf);
  for(int i = 0; i < 2; i++) {
    string msg = "1000\r\n"+body+"\r\n";
    len = sizeof(buf);
    enc->inflate(buf, len, msg.c_str(), msg.size());
    buf[len] = '\0';
    CPPUNIT_ASSERT_EQUAL(body, string(buf));
  }
  enc->inflate(buf, len, "0\r\n", 3);
  CPPUNIT_ASSERT_EQUAL(0, len);
  CPPUNIT_ASSERT(enc->finished());
}