]>
Commit | Line | Data |
---|---|---|
f5bffbd5 | 1 | #include "config.h" |
e1f7507e | 2 | |
c21ad0f5 | 3 | #include <cppunit/TestAssert.h> |
4 | ||
c21ad0f5 | 5 | #include "testHttpRequest.h" |
6 | #include "HttpRequest.h" | |
e1f7507e | 7 | #include "Mem.h" |
c21ad0f5 | 8 | |
9 | ||
10 | CPPUNIT_TEST_SUITE_REGISTRATION( testHttpRequest ); | |
11 | ||
0f9db2d6 AJ |
12 | /** wrapper for testing HttpRequest object private and protected functions */ |
13 | class PrivateHttpRequest : public HttpRequest | |
14 | { | |
15 | public: | |
16 | bool doSanityCheckStartLine(MemBuf *b, const size_t h, http_status *e) { return sanityCheckStartLine(b,h,e); }; | |
17 | }; | |
18 | ||
c21ad0f5 | 19 | /* stub functions to link successfully */ |
20 | void | |
21 | shut_down(int) | |
22 | {} | |
23 | ||
757a2291 GS |
24 | void |
25 | reconfigure(int) | |
26 | {} | |
27 | ||
c21ad0f5 | 28 | /* end stubs */ |
29 | ||
30 | /* init memory pools */ | |
31 | ||
16555581 | 32 | void |
33 | testHttpRequest::setUp() | |
c21ad0f5 | 34 | { |
16555581 | 35 | Mem::Init(); |
0f9db2d6 | 36 | httpHeaderInitModule(); |
16555581 | 37 | } |
c21ad0f5 | 38 | |
39 | /* | |
40 | * Test creating an HttpRequest object from a Url and method | |
41 | */ | |
42 | void | |
43 | testHttpRequest::testCreateFromUrlAndMethod() | |
44 | { | |
45 | /* vanilla url */ | |
46 | ushort expected_port; | |
47 | char * url = xstrdup("http://foo:90/bar"); | |
48 | HttpRequest *aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_GET); | |
49 | expected_port = 90; | |
50 | HttpRequest *nullRequest = NULL; | |
51 | CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port); | |
914b89a2 | 52 | CPPUNIT_ASSERT(aRequest->method == METHOD_GET); |
cc192b50 | 53 | CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost())); |
30abd221 | 54 | CPPUNIT_ASSERT_EQUAL(String("/bar"), aRequest->urlpath); |
c21ad0f5 | 55 | CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol); |
30abd221 | 56 | CPPUNIT_ASSERT_EQUAL(String("http://foo:90/bar"), String(url)); |
c21ad0f5 | 57 | xfree(url); |
cc192b50 | 58 | |
c21ad0f5 | 59 | /* vanilla url, different method */ |
60 | url = xstrdup("http://foo/bar"); | |
61 | aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_PUT); | |
62 | expected_port = 80; | |
63 | CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port); | |
914b89a2 | 64 | CPPUNIT_ASSERT(aRequest->method == METHOD_PUT); |
cc192b50 | 65 | CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost())); |
30abd221 | 66 | CPPUNIT_ASSERT_EQUAL(String("/bar"), aRequest->urlpath); |
c21ad0f5 | 67 | CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol); |
30abd221 | 68 | CPPUNIT_ASSERT_EQUAL(String("http://foo/bar"), String(url)); |
cc192b50 | 69 | |
c21ad0f5 | 70 | /* a connect url with non-CONNECT data */ |
71 | url = xstrdup(":foo/bar"); | |
72 | aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_CONNECT); | |
73 | xfree(url); | |
74 | CPPUNIT_ASSERT_EQUAL(nullRequest, aRequest); | |
cc192b50 | 75 | |
c21ad0f5 | 76 | /* a CONNECT url with CONNECT data */ |
77 | url = xstrdup("foo:45"); | |
78 | aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_CONNECT); | |
79 | expected_port = 45; | |
80 | CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port); | |
914b89a2 | 81 | CPPUNIT_ASSERT(aRequest->method == METHOD_CONNECT); |
cc192b50 | 82 | CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost())); |
30abd221 | 83 | CPPUNIT_ASSERT_EQUAL(String(""), aRequest->urlpath); |
c21ad0f5 | 84 | CPPUNIT_ASSERT_EQUAL(PROTO_NONE, aRequest->protocol); |
30abd221 | 85 | CPPUNIT_ASSERT_EQUAL(String("foo:45"), String(url)); |
c21ad0f5 | 86 | xfree(url); |
87 | } | |
88 | ||
89 | /* | |
90 | * Test creating an HttpRequest object from a Url alone. | |
91 | */ | |
92 | void | |
93 | testHttpRequest::testCreateFromUrl() | |
94 | { | |
95 | /* vanilla url */ | |
96 | ushort expected_port; | |
97 | char * url = xstrdup("http://foo:90/bar"); | |
98 | HttpRequest *aRequest = HttpRequest::CreateFromUrl(url); | |
99 | expected_port = 90; | |
100 | CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port); | |
914b89a2 | 101 | CPPUNIT_ASSERT(aRequest->method == METHOD_GET); |
cc192b50 | 102 | CPPUNIT_ASSERT_EQUAL(String("foo"), String(aRequest->GetHost())); |
30abd221 | 103 | CPPUNIT_ASSERT_EQUAL(String("/bar"), aRequest->urlpath); |
c21ad0f5 | 104 | CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol); |
30abd221 | 105 | CPPUNIT_ASSERT_EQUAL(String("http://foo:90/bar"), String(url)); |
c21ad0f5 | 106 | xfree(url); |
107 | } | |
cc192b50 | 108 | |
109 | /* | |
110 | * Test BUG: URL '2000:800:45' opens host 2000 port 800 !! | |
111 | */ | |
112 | void | |
113 | testHttpRequest::testIPv6HostColonBug() | |
114 | { | |
115 | ushort expected_port; | |
116 | char * url = NULL; | |
117 | HttpRequest *aRequest = NULL; | |
118 | ||
119 | /* valid IPv6 address without port */ | |
120 | url = xstrdup("http://[2000:800::45]/foo"); | |
121 | aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_GET); | |
122 | expected_port = 80; | |
123 | CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port); | |
914b89a2 | 124 | CPPUNIT_ASSERT(aRequest->method == METHOD_GET); |
cc192b50 | 125 | CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest->GetHost())); |
126 | CPPUNIT_ASSERT_EQUAL(String("/foo"), aRequest->urlpath); | |
127 | CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol); | |
128 | CPPUNIT_ASSERT_EQUAL(String("http://[2000:800::45]/foo"), String(url)); | |
129 | xfree(url); | |
130 | ||
131 | /* valid IPv6 address with port */ | |
132 | url = xstrdup("http://[2000:800::45]:90/foo"); | |
133 | aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_GET); | |
134 | expected_port = 90; | |
135 | CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port); | |
914b89a2 | 136 | CPPUNIT_ASSERT(aRequest->method == METHOD_GET); |
cc192b50 | 137 | CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest->GetHost())); |
138 | CPPUNIT_ASSERT_EQUAL(String("/foo"), aRequest->urlpath); | |
139 | CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol); | |
140 | CPPUNIT_ASSERT_EQUAL(String("http://[2000:800::45]:90/foo"), String(url)); | |
141 | xfree(url); | |
142 | ||
143 | /* IPv6 address as invalid (bug trigger) */ | |
144 | url = xstrdup("http://2000:800::45/foo"); | |
145 | aRequest = HttpRequest::CreateFromUrlAndMethod(url, METHOD_GET); | |
146 | expected_port = 80; | |
147 | CPPUNIT_ASSERT_EQUAL(expected_port, aRequest->port); | |
914b89a2 | 148 | CPPUNIT_ASSERT(aRequest->method == METHOD_GET); |
cc192b50 | 149 | #if USE_IPV6 |
26ac0430 | 150 | /* We hasve fixed this in IPv6 build. */ |
cc192b50 | 151 | CPPUNIT_ASSERT_EQUAL(String("[2000:800::45]"), String(aRequest->GetHost())); |
152 | #else | |
26ac0430 | 153 | /* NO fix is possible in IPv4-pure build. */ |
cc192b50 | 154 | CPPUNIT_ASSERT_EQUAL(String("2000:800::45"), String(aRequest->GetHost())); |
155 | #endif | |
156 | CPPUNIT_ASSERT_EQUAL(String("/foo"), aRequest->urlpath); | |
157 | CPPUNIT_ASSERT_EQUAL(PROTO_HTTP, aRequest->protocol); | |
158 | CPPUNIT_ASSERT_EQUAL(String("http://2000:800::45/foo"), String(url)); | |
159 | xfree(url); | |
160 | } | |
0f9db2d6 AJ |
161 | |
162 | void | |
163 | testHttpRequest::testSanityCheckStartLine() | |
164 | { | |
165 | MemBuf input; | |
166 | PrivateHttpRequest engine; | |
167 | http_status error = HTTP_STATUS_NONE; | |
168 | size_t hdr_len; | |
169 | input.init(); | |
170 | ||
171 | // a valid request line | |
172 | input.append("GET / HTTP/1.1\n\n", 16); | |
173 | hdr_len = headersEnd(input.content(), input.contentSize()); | |
174 | CPPUNIT_ASSERT(engine.doSanityCheckStartLine(&input, hdr_len, &error) ); | |
175 | CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE); | |
176 | input.reset(); | |
177 | error = HTTP_STATUS_NONE; | |
178 | ||
179 | input.append("GET / HTTP/1.1\n\n", 18); | |
180 | hdr_len = headersEnd(input.content(), input.contentSize()); | |
181 | CPPUNIT_ASSERT(engine.doSanityCheckStartLine(&input, hdr_len, &error) ); | |
182 | CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE); | |
183 | input.reset(); | |
184 | error = HTTP_STATUS_NONE; | |
185 | ||
186 | // strange but valid methods | |
187 | input.append(". / HTTP/1.1\n\n", 14); | |
188 | hdr_len = headersEnd(input.content(), input.contentSize()); | |
189 | CPPUNIT_ASSERT(engine.doSanityCheckStartLine(&input, hdr_len, &error) ); | |
190 | CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE); | |
191 | input.reset(); | |
192 | error = HTTP_STATUS_NONE; | |
193 | ||
194 | input.append("OPTIONS * HTTP/1.1\n\n", 20); | |
195 | hdr_len = headersEnd(input.content(), input.contentSize()); | |
196 | CPPUNIT_ASSERT(engine.doSanityCheckStartLine(&input, hdr_len, &error) ); | |
197 | CPPUNIT_ASSERT_EQUAL(error, HTTP_STATUS_NONE); | |
198 | input.reset(); | |
199 | error = HTTP_STATUS_NONE; | |
200 | ||
201 | // TODO no method | |
202 | ||
203 | // TODO binary code in method | |
204 | ||
205 | // TODO no URL | |
206 | ||
207 | // TODO no status (okay) | |
208 | ||
209 | // TODO non-HTTP protocol | |
210 | ||
211 | input.append(" \n\n", 8); | |
212 | hdr_len = headersEnd(input.content(), input.contentSize()); | |
213 | CPPUNIT_ASSERT(!engine.doSanityCheckStartLine(&input, hdr_len, &error) ); | |
214 | CPPUNIT_ASSERT_EQUAL(error, HTTP_INVALID_HEADER); | |
215 | input.reset(); | |
216 | error = HTTP_STATUS_NONE; | |
217 | } |