src/server/proxy/httpclient.c

changeset 674
6a031133a498
parent 673
144bdc33fdb6
equal deleted inserted replaced
673:144bdc33fdb6 674:6a031133a498
357 357
358 /* --------------------------------- Tests --------------------------------- */ 358 /* --------------------------------- Tests --------------------------------- */
359 359
360 static CX_TEST(test_http_client_send_request) { 360 static CX_TEST(test_http_client_send_request) {
361 CX_TEST_DO { 361 CX_TEST_DO {
362 362 EventHandler dummy;
363 HttpClient *client = http_client_new(&dummy);
364
365 int fds[2];
366 util_socketpair(fds);
367 util_socket_setnonblock(fds[0], 1);
368 util_socket_setnonblock(fds[1], 1);
369 client->socketfd = fds[0];
370 int sock = fds[1];
371
372 // create a large test buffer, that is bigger than the socket buffer
373 // 32mb should be enough
374 size_t len = 32*1024*1024;
375 char *str = malloc(len);
376 // init the buffer with random data
377 for(size_t i=0;i<len;i+=sizeof(int)) {
378 int *p = (int*)(str+i);
379 *p = rand();;
380 }
381
382 client->req_buffer = str;
383 client->req_buffer_len = len;
384
385 // test client_send_request
386
387 int ret = client_send_request(client);
388 // It is very likely that the first client_send_request call doesn't
389 // fully write the request buffer to the socket
390 // In that case it returns 1 but without the error flag
391 CX_TEST_ASSERT(ret == 1 && !client->error);
392 CX_TEST_ASSERT(client->req_buffer_pos > 0);
393 CX_TEST_ASSERT(client->req_buffer_pos < len);
394
395 // read the request buffer from sock and continue with client_send_request
396 CxBuffer buf;
397 cxBufferInit(&buf, cxDefaultAllocator, NULL, len, CX_BUFFER_AUTO_EXTEND);
398 char tmpbuf[1024];
399 int writes = 1;
400 while(client->req_buffer_pos < client->req_buffer_len && writes < 2000000) {
401 ssize_t r = read(sock, tmpbuf, 1024);
402 CX_TEST_ASSERT(r >= 0);
403 cxBufferWrite(tmpbuf, 1, r, &buf);
404 ret = client_send_request(client);
405 CX_TEST_ASSERT(ret == 0 || (ret == 1 && !client->error));
406
407 writes++;
408 }
409 CX_TEST_ASSERT(client->req_buffer_pos == client->req_buffer_len);
410
411 // finish reading the request buffer from sock
412 ssize_t r;
413 while((r = read(sock, tmpbuf, 1024)) > 0 && writes < 2000000) {
414 cxBufferWrite(tmpbuf, 1, r, &buf);
415 writes++;
416 }
417
418 CX_TEST_ASSERT(buf.size == len);
419 CX_TEST_ASSERT(!memcmp(str, buf.space, len));
420
421 // cleanup
422 close(fds[0]);
423 close(fds[1]);
424 http_client_free(client);
363 } 425 }
364 } 426 }
365 427
366 void http_client_add_tests(CxTestSuite *suite) { 428 void http_client_add_tests(CxTestSuite *suite) {
367 cx_test_register(suite, test_http_client_send_request); 429 cx_test_register(suite, test_http_client_send_request);

mercurial