| 28 |
28 |
| 29 #include "httpparser.h" |
29 #include "httpparser.h" |
| 30 |
30 |
| 31 #include "../daemon/httpparser.h" |
31 #include "../daemon/httpparser.h" |
| 32 |
32 |
| |
33 #include <cx/string.h> |
| |
34 |
| 33 CX_TEST(test_parse_request_line) { |
35 CX_TEST(test_parse_request_line) { |
| 34 CX_TEST_DO { |
36 CX_TEST_DO { |
| 35 HttpParser parser; |
37 HttpParser parser; |
| 36 memset(&parser, 0, sizeof(HttpParser)); |
38 memset(&parser, 0, sizeof(HttpParser)); |
| 37 parser.start_line = cx_mutstr("GET / HTTP/1.1\r\n"); |
39 parser.start_line = cx_mutstr("GET / HTTP/1.1\r\n"); |
| 177 parser.type = 1; |
179 parser.type = 1; |
| 178 int nret6 = parse_response_line(&parser); |
180 int nret6 = parse_response_line(&parser); |
| 179 CX_TEST_ASSERT(nret6); |
181 CX_TEST_ASSERT(nret6); |
| 180 } |
182 } |
| 181 } |
183 } |
| |
184 |
| |
185 CX_TEST_SUBROUTINE(test_http_parser_process, cxstring request, int chunksize) { |
| |
186 netbuf buf; |
| |
187 memset(&buf, 0, sizeof(netbuf)); |
| |
188 buf.inbuf = (unsigned char*)request.ptr; |
| |
189 buf.maxsize = request.length; |
| |
190 HeaderArray *header = header_array_create(); |
| |
191 HttpParser *parser = http_parser_new2(0, &buf, header); |
| |
192 |
| |
193 while(buf.cursize < request.length) { |
| |
194 buf.cursize += chunksize; |
| |
195 if(buf.cursize > buf.maxsize) { |
| |
196 buf.cursize = buf.maxsize; |
| |
197 } |
| |
198 |
| |
199 int ret = http_parser_process(parser); |
| |
200 if(buf.cursize < buf.maxsize) { |
| |
201 CX_TEST_ASSERT(ret == 1); |
| |
202 } else { |
| |
203 CX_TEST_ASSERT(ret == 0); |
| |
204 } |
| |
205 } |
| |
206 CX_TEST_ASSERT(http_parser_validate(parser)); |
| |
207 |
| |
208 http_parser_free(parser); |
| |
209 header_array_free(header); |
| |
210 } |
| |
211 |
| |
212 CX_TEST(test_http_parser_process_full) { |
| |
213 CX_TEST_DO { |
| |
214 cxstring req0 = cx_str( |
| |
215 "GET / HTTP/1.1\r\n" |
| |
216 "Host: example.com\r\n" |
| |
217 "\r\n"); |
| |
218 |
| |
219 cxstring req1 = cx_str("GET /api/data?id=42 HTTP/1.1\r\n" |
| |
220 "Host: api.example.com\r\n" |
| |
221 "User-Agent: TestClient/1.0\r\n" |
| |
222 "Accept: application/json\r\n" |
| |
223 "Accept-Language: en-US,en;q=0.9\r\n" |
| |
224 "Connection: keep-alive\r\n" |
| |
225 "Cache-Control: no-cache\r\n" |
| |
226 "\r\n"); |
| |
227 |
| |
228 cxstring req2 = cx_str("GET /search?q=network+testing HTTP/1.1\r\n" |
| |
229 "Host: www.example.com\r\n" |
| |
230 "User-Agent: Mozilla/5.0 (X11; Linux x86_64) TestBrowser/99.0\r\n" |
| |
231 "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" |
| |
232 "Accept-Language: en-US,en;q=0.9,de;q=0.8\r\n" |
| |
233 "Accept-Encoding: gzip, deflate, br\r\n" |
| |
234 "Connection: keep-alive\r\n" |
| |
235 "Cache-Control: max-age=0\r\n" |
| |
236 "Upgrade-Insecure-Requests: 1\r\n" |
| |
237 "Pragma: no-cache\r\n" |
| |
238 "DNT: 1\r\n" |
| |
239 "Sec-Fetch-Dest: document\r\n" |
| |
240 "Sec-Fetch-Mode: navigate\r\n" |
| |
241 "Sec-Fetch-Site: none\r\n" |
| |
242 "Sec-Fetch-User: ?1\r\n" |
| |
243 "Referer: https://www.example.com/\r\n" |
| |
244 "X-Forwarded-For: 203.0.113.195\r\n" |
| |
245 "X-Request-ID: 123e4567-e89b-12d3-a456-426614174000\r\n" |
| |
246 "\r\n"); |
| |
247 |
| |
248 CX_TEST_CALL_SUBROUTINE(test_http_parser_process, req0, req0.length); |
| |
249 CX_TEST_CALL_SUBROUTINE(test_http_parser_process, req1, req1.length); |
| |
250 CX_TEST_CALL_SUBROUTINE(test_http_parser_process, req2, req2.length); |
| |
251 } |
| |
252 } |