File: HttpHeaderProcessorTest.cc

package info (click to toggle)
aria2 1.18.8-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,392 kB
  • ctags: 16,036
  • sloc: cpp: 115,823; sh: 12,015; ansic: 7,394; makefile: 1,445; ruby: 462; python: 216; xml: 176; asm: 58; sed: 16
file content (281 lines) | stat: -rw-r--r-- 8,370 bytes parent folder | download
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "HttpHeaderProcessor.h"

#include <iostream>

#include <cppunit/extensions/HelperMacros.h>

#include "HttpHeader.h"
#include "DlRetryEx.h"
#include "DlAbortEx.h"

namespace aria2 {

class HttpHeaderProcessorTest:public CppUnit::TestFixture {

  CPPUNIT_TEST_SUITE(HttpHeaderProcessorTest);
  CPPUNIT_TEST(testParse1);
  CPPUNIT_TEST(testParse2);
  CPPUNIT_TEST(testParse3);
  CPPUNIT_TEST(testGetLastBytesProcessed);
  CPPUNIT_TEST(testGetLastBytesProcessed_nullChar);
  CPPUNIT_TEST(testGetHttpResponseHeader);
  CPPUNIT_TEST(testGetHttpResponseHeader_statusOnly);
  CPPUNIT_TEST(testGetHttpResponseHeader_insufficientStatusLength);
  CPPUNIT_TEST(testGetHttpResponseHeader_nameStartsWs);
  CPPUNIT_TEST(testBeyondLimit);
  CPPUNIT_TEST(testGetHeaderString);
  CPPUNIT_TEST(testGetHttpRequestHeader);
  CPPUNIT_TEST_SUITE_END();

public:
  void testParse1();
  void testParse2();
  void testParse3();
  void testGetLastBytesProcessed();
  void testGetLastBytesProcessed_nullChar();
  void testGetHttpResponseHeader();
  void testGetHttpResponseHeader_statusOnly();
  void testGetHttpResponseHeader_insufficientStatusLength();
  void testGetHttpResponseHeader_nameStartsWs();
  void testBeyondLimit();
  void testGetHeaderString();
  void testGetHttpRequestHeader();
};


CPPUNIT_TEST_SUITE_REGISTRATION( HttpHeaderProcessorTest );

void HttpHeaderProcessorTest::testParse1()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  std::string hd1 = "HTTP/1.1 200 OK\r\n";
  CPPUNIT_ASSERT(!proc.parse(hd1));
  CPPUNIT_ASSERT(proc.parse("\r\n"));
}

void HttpHeaderProcessorTest::testParse2()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  std::string hd1 = "HTTP/1.1 200 OK\n";
  CPPUNIT_ASSERT(!proc.parse(hd1));
  CPPUNIT_ASSERT(proc.parse("\n"));
}

void HttpHeaderProcessorTest::testParse3()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
  std::string s =
    "GET / HTTP/1.1\r\n"
    "Host: aria2.sourceforge.net\r\n"
    "Connection: close \r\n" // trailing white space (BWS)
    "Accept-Encoding: text1\r\n" // Multi-line header
    "  text2\r\n"
    "  text3\r\n"
    "Authorization: foo\r\n"
    "Authorization: bar\r\n"
    "Content-Type:\r\n"
    "\r\n";
  CPPUNIT_ASSERT(proc.parse(s));
  auto h = proc.getResult();
  CPPUNIT_ASSERT_EQUAL(std::string("close"), h->find(HttpHeader::CONNECTION));
  CPPUNIT_ASSERT_EQUAL(std::string("text1 text2 text3"),
                       h->find(HttpHeader::ACCEPT_ENCODING));
  CPPUNIT_ASSERT_EQUAL(std::string("foo"),
                       h->findAll(HttpHeader::AUTHORIZATION)[0]);
  CPPUNIT_ASSERT_EQUAL(std::string("bar"),
                       h->findAll(HttpHeader::AUTHORIZATION)[1]);
  CPPUNIT_ASSERT_EQUAL(std::string(""), h->find(HttpHeader::CONTENT_TYPE));
  CPPUNIT_ASSERT(h->defined(HttpHeader::CONTENT_TYPE));
}

void HttpHeaderProcessorTest::testGetLastBytesProcessed()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  std::string hd1 =
    "HTTP/1.1 200 OK\r\n"
    "\r\nputbackme";
  CPPUNIT_ASSERT(proc.parse(hd1));
  CPPUNIT_ASSERT_EQUAL((size_t)19, proc.getLastBytesProcessed());

  proc.clear();

  std::string hd2 =
    "HTTP/1.1 200 OK\n"
    "\nputbackme";
  CPPUNIT_ASSERT(proc.parse(hd2));
  CPPUNIT_ASSERT_EQUAL((size_t)17, proc.getLastBytesProcessed());
}

void HttpHeaderProcessorTest::testGetLastBytesProcessed_nullChar()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  const char x[] =
    "HTTP/1.1 200 OK\r\n"
    "foo: foo\0bar\r\n"
    "\r\nputbackme";
  std::string hd1(&x[0], &x[sizeof(x)-1]);
  CPPUNIT_ASSERT(proc.parse(hd1));
  CPPUNIT_ASSERT_EQUAL((size_t)33, proc.getLastBytesProcessed());
}

void HttpHeaderProcessorTest::testGetHttpResponseHeader()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  std::string hd =
    "HTTP/1.1 404 Not Found\r\n"
    "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
    "Server: Apache/2.2.3 (Debian)\r\n"
    "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
    "ETag: \"594065-23e3-50825cc0\"\r\n"
    "Accept-Ranges: bytes\r\n"
    "Content-Length: 9187\r\n"
    "Connection: close\r\n"
    "Content-Type: text/html; charset=UTF-8\r\n"
    "\r\n"
    "Content-Encoding: body";

  CPPUNIT_ASSERT(proc.parse(hd));

  auto header = proc.getResult();
  CPPUNIT_ASSERT_EQUAL(404, header->getStatusCode());
  CPPUNIT_ASSERT_EQUAL(std::string("Not Found"), header->getReasonPhrase());
  CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), header->getVersion());
  CPPUNIT_ASSERT_EQUAL(std::string("9187"),
                       header->find(HttpHeader::CONTENT_LENGTH));
  CPPUNIT_ASSERT_EQUAL(std::string("text/html; charset=UTF-8"),
                       header->find(HttpHeader::CONTENT_TYPE));
  CPPUNIT_ASSERT(!header->defined(HttpHeader::CONTENT_ENCODING));
}

void HttpHeaderProcessorTest::testGetHttpResponseHeader_statusOnly()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);

  std::string hd = "HTTP/1.1 200\r\n\r\n";
  CPPUNIT_ASSERT(proc.parse(hd));
  auto header = proc.getResult();
  CPPUNIT_ASSERT_EQUAL(200, header->getStatusCode());
}

void HttpHeaderProcessorTest::testGetHttpResponseHeader_insufficientStatusLength()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);

  std::string hd = "HTTP/1.1 20\r\n\r\n";
  try {
    proc.parse(hd);
    CPPUNIT_FAIL("Exception must be thrown.");
  } catch(DlAbortEx& ex) {
    // Success
  }
}

void HttpHeaderProcessorTest::testGetHttpResponseHeader_nameStartsWs()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);

  std::string hd =
    "HTTP/1.1 200\r\n"
    " foo:bar\r\n"
    "\r\n";
  try {
    proc.parse(hd);
    CPPUNIT_FAIL("Exception must be thrown.");
  } catch(DlAbortEx& ex) {
    // Success
  }

  proc.clear();
  hd =
    "HTTP/1.1 200\r\n"
    ":foo:bar\r\n"
    "\r\n";
  try {
    proc.parse(hd);
    CPPUNIT_FAIL("Exception must be thrown.");
  } catch(DlAbortEx& ex) {
    // Success
  }

  proc.clear();
  hd =
    "HTTP/1.1 200\r\n"
    ":foo\r\n"
    "\r\n";
  try {
    proc.parse(hd);
    CPPUNIT_FAIL("Exception must be thrown.");
  } catch(DlAbortEx& ex) {
    // Success
  }
}

void HttpHeaderProcessorTest::testBeyondLimit()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);

  std::string hd1 = "HTTP/1.1 200 OK\r\n";
  std::string hd2 = std::string(1025, 'A');

  proc.parse(hd1);
  try {
    proc.parse(hd2);
    CPPUNIT_FAIL("Exception must be thrown.");
  } catch(DlAbortEx& ex) {
    // Success
  }
}

void HttpHeaderProcessorTest::testGetHeaderString()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::CLIENT_PARSER);
  std::string hd =
    "HTTP/1.1 200 OK\r\n"
    "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
    "Server: Apache/2.2.3 (Debian)\r\n"
    "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
    "ETag: \"594065-23e3-50825cc0\"\r\n"
    "Accept-Ranges: bytes\r\n"
    "Content-Length: 9187\r\n"
    "Connection: close\r\n"
    "Content-Type: text/html; charset=UTF-8\r\n"
    "\r\nputbackme";

  CPPUNIT_ASSERT(proc.parse(hd));

  CPPUNIT_ASSERT_EQUAL
    (std::string("HTTP/1.1 200 OK\r\n"
                 "Date: Mon, 25 Jun 2007 16:04:59 GMT\r\n"
                 "Server: Apache/2.2.3 (Debian)\r\n"
                 "Last-Modified: Tue, 12 Jun 2007 14:28:43 GMT\r\n"
                 "ETag: \"594065-23e3-50825cc0\"\r\n"
                 "Accept-Ranges: bytes\r\n"
                 "Content-Length: 9187\r\n"
                 "Connection: close\r\n"
                 "Content-Type: text/html; charset=UTF-8\r\n"
                 "\r\n"),
     proc.getHeaderString());
}

void HttpHeaderProcessorTest::testGetHttpRequestHeader()
{
  HttpHeaderProcessor proc(HttpHeaderProcessor::SERVER_PARSER);
  std::string request =
    "GET /index.html HTTP/1.1\r\n"
    "Host: host\r\n"
    "Connection: close\r\n"
    "\r\n"
    "Content-Encoding: body";

  CPPUNIT_ASSERT(proc.parse(request));

  auto httpHeader = proc.getResult();
  CPPUNIT_ASSERT_EQUAL(std::string("GET"), httpHeader->getMethod());
  CPPUNIT_ASSERT_EQUAL(std::string("/index.html"),httpHeader->getRequestPath());
  CPPUNIT_ASSERT_EQUAL(std::string("HTTP/1.1"), httpHeader->getVersion());
  CPPUNIT_ASSERT_EQUAL(std::string("close"),
                       httpHeader->find(HttpHeader::CONNECTION));
  CPPUNIT_ASSERT(!httpHeader->defined(HttpHeader::CONTENT_ENCODING));
}

} // namespace aria2