| 41 static EVHandler *test_eventhandler; |
41 static EVHandler *test_eventhandler; |
| 42 static pthread_mutex_t test_mutex; |
42 static pthread_mutex_t test_mutex; |
| 43 static pthread_cond_t test_cond; |
43 static pthread_cond_t test_cond; |
| 44 static volatile int test_finished; |
44 static volatile int test_finished; |
| 45 |
45 |
| |
46 void http_client_tests_init(void) { |
| |
47 pthread_mutex_init(&test_mutex, NULL); |
| |
48 pthread_cond_init(&test_cond, NULL); |
| |
49 |
| |
50 EventHandlerConfig config; |
| |
51 config.isdefault = 0; |
| |
52 config.name = cx_str(NULL); |
| |
53 config.nthreads = 1; |
| |
54 test_eventhandler = evhandler_create(&config); |
| |
55 } |
| |
56 |
| |
57 void http_client_tests_cleanup(void) { |
| |
58 ev_instance_shutdown(test_eventhandler->instances[0]); |
| |
59 } |
| |
60 |
| 46 static void test_response_finished(HttpClient *client, void *userdata) { |
61 static void test_response_finished(HttpClient *client, void *userdata) { |
| 47 pthread_mutex_lock(&test_mutex); |
62 pthread_mutex_lock(&test_mutex); |
| 48 pthread_cond_signal(&test_cond); |
63 pthread_cond_signal(&test_cond); |
| 49 pthread_mutex_unlock(&test_mutex); |
64 pthread_mutex_unlock(&test_mutex); |
| 50 http_client_free(client); |
65 http_client_free(client); |
| 51 test_finished = 1; |
66 test_finished = 1; |
| 52 } |
67 } |
| 53 |
68 |
| 54 static ssize_t test_response_body_write(HttpClient *client, void *buf, size_t nbytes, void *userdata) { |
69 static ssize_t test_response_body_write(HttpClient *client, void *buf, size_t nbytes, void *userdata) { |
| 55 char *str = buf; |
70 char *str = buf; |
| 56 return cxBufferWrite(buf, 1, nbytes, userdata); |
71 return cxBufferWrite(str, 1, nbytes, userdata); |
| 57 } |
72 } |
| 58 |
73 |
| 59 CX_TEST_SUBROUTINE(test_httpclient, cxstring *response_blocks, size_t num_blocks, CxBuffer *out, int error_interval) { |
74 CX_TEST_SUBROUTINE(test_httpclient, cxstring *response_blocks, size_t num_blocks, CxBuffer *out, int error_interval) { |
| 60 pthread_mutex_init(&test_mutex, NULL); |
|
| 61 pthread_cond_init(&test_cond, NULL); |
|
| 62 |
|
| 63 if(!test_eventhandler) { |
|
| 64 EventHandlerConfig config; |
|
| 65 config.isdefault = 0; |
|
| 66 config.name = cx_str(NULL); |
|
| 67 config.nthreads = 1; |
|
| 68 test_eventhandler = evhandler_create(&config); |
|
| 69 } |
|
| 70 |
|
| 71 HttpClient *client = http_client_new(test_eventhandler->instances[0]); |
75 HttpClient *client = http_client_new(test_eventhandler->instances[0]); |
| 72 client->response_body_write = test_response_body_write; |
76 client->response_body_write = test_response_body_write; |
| 73 client->response_body_write_userdata = out; |
77 client->response_body_write_userdata = out; |
| 74 client->response_finished = test_response_finished; |
78 client->response_finished = test_response_finished; |
| 75 |
79 |
| 78 util_socket_setnonblock(fds[0], 1); |
82 util_socket_setnonblock(fds[0], 1); |
| 79 |
83 |
| 80 http_client_set_socket(client, fds[0]); |
84 http_client_set_socket(client, fds[0]); |
| 81 http_client_set_method(client, "GET"); |
85 http_client_set_method(client, "GET"); |
| 82 http_client_set_uri(client, "/testx01"); |
86 http_client_set_uri(client, "/testx01"); |
| 83 |
87 |
| 84 test_finished = 0; |
88 test_finished = 0; |
| 85 int ret = http_client_start(client); |
89 int ret = http_client_start(client); |
| 86 CX_TEST_ASSERT(ret == 0); |
90 CX_TEST_ASSERT(ret == 0); |
| 87 |
91 |
| 88 for(int i=0;i<num_blocks;i++) { |
92 for(int i=0;i<num_blocks;i++) { |
| 95 CX_TEST_ASSERT(w >= 0); |
99 CX_TEST_ASSERT(w >= 0); |
| 96 response_pos += w; |
100 response_pos += w; |
| 97 } |
101 } |
| 98 } |
102 } |
| 99 |
103 |
| 100 close(fds[1]); |
|
| 101 |
|
| 102 pthread_mutex_lock(&test_mutex); |
104 pthread_mutex_lock(&test_mutex); |
| 103 if(test_finished == 0) { |
105 if(test_finished == 0) { |
| 104 pthread_cond_wait(&test_cond, &test_mutex); |
106 pthread_cond_wait(&test_cond, &test_mutex); |
| 105 } |
107 } |
| 106 pthread_mutex_unlock(&test_mutex); |
108 pthread_mutex_unlock(&test_mutex); |
| |
109 |
| |
110 close(fds[1]); |
| 107 } |
111 } |
| 108 |
112 |
| 109 CX_TEST(test_http_client_simple_get1) { |
113 CX_TEST(test_http_client_simple_get1) { |
| 110 CX_TEST_DO { |
114 CX_TEST_DO { |
| 111 cxstring response = cx_str( |
115 cxstring response = cx_str( |