| 69 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) { |
| 70 char *str = buf; |
70 char *str = buf; |
| 71 return cxBufferWrite(str, 1, nbytes, userdata); |
71 return cxBufferWrite(str, 1, nbytes, userdata); |
| 72 } |
72 } |
| 73 |
73 |
| 74 CX_TEST_SUBROUTINE(test_httpclient, cxstring *response_blocks, size_t num_blocks, CxBuffer *out, int error_interval) { |
74 static cxstring request_body_str; |
| |
75 static int request_body_pos = 0; |
| |
76 static size_t request_body_max_read = 16; |
| |
77 |
| |
78 ssize_t test_request_body_read(HttpClient *client, void *buf, size_t nbytes, void *userdata) { |
| |
79 if(nbytes > request_body_max_read) { |
| |
80 nbytes = request_body_max_read; |
| |
81 } |
| |
82 size_t available = request_body_str.length - request_body_pos; |
| |
83 if(nbytes > available) { |
| |
84 nbytes = available; |
| |
85 } |
| |
86 if(nbytes > 0) { |
| |
87 memcpy(buf, request_body_str.ptr + request_body_pos, nbytes); |
| |
88 request_body_pos += nbytes; |
| |
89 } |
| |
90 return nbytes; |
| |
91 } |
| |
92 |
| |
93 CX_TEST_SUBROUTINE(test_httpclient, cxstring request_body, int chunked_transfer, cxstring *response_blocks, size_t num_blocks, CxBuffer *out) { |
| 75 HttpClient *client = http_client_new(test_eventhandler->instances[0]); |
94 HttpClient *client = http_client_new(test_eventhandler->instances[0]); |
| 76 client->response_body_write = test_response_body_write; |
95 client->response_body_write = test_response_body_write; |
| 77 client->response_body_write_userdata = out; |
96 client->response_body_write_userdata = out; |
| 78 client->response_finished = test_response_finished; |
97 client->response_finished = test_response_finished; |
| 79 |
98 |
| 82 util_socket_setnonblock(fds[0], 1); |
101 util_socket_setnonblock(fds[0], 1); |
| 83 |
102 |
| 84 http_client_set_socket(client, fds[0]); |
103 http_client_set_socket(client, fds[0]); |
| 85 http_client_set_method(client, "GET"); |
104 http_client_set_method(client, "GET"); |
| 86 http_client_set_uri(client, "/testx01"); |
105 http_client_set_uri(client, "/testx01"); |
| |
106 |
| |
107 request_body_str = request_body; |
| |
108 request_body_pos = 0; |
| |
109 if(request_body.length > 0) { |
| |
110 client->request_body_read = test_request_body_read; |
| |
111 if(chunked_transfer) { |
| |
112 http_client_enable_chunked_transfer_encoding(client); |
| |
113 } else { |
| |
114 http_client_set_content_length(client, request_body.length); |
| |
115 } |
| |
116 } |
| 87 |
117 |
| 88 test_finished = 0; |
118 test_finished = 0; |
| 89 int ret = http_client_start(client); |
119 int ret = http_client_start(client); |
| 90 CX_TEST_ASSERT(ret == 0); |
120 CX_TEST_ASSERT(ret == 0); |
| 91 |
121 |
| 117 "Content-length: 13\r\n" |
147 "Content-length: 13\r\n" |
| 118 "\r\n" |
148 "\r\n" |
| 119 "Hello World!\n"); |
149 "Hello World!\n"); |
| 120 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
150 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
| 121 |
151 |
| 122 CX_TEST_CALL_SUBROUTINE(test_httpclient, &response, 1, out, 0); |
152 CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, &response, 1, out); |
| 123 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n")); |
153 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n")); |
| 124 |
154 |
| 125 cxBufferFree(out); |
155 cxBufferFree(out); |
| 126 } |
156 } |
| 127 } |
157 } |
| 134 response[2] = cx_str("\r\n"); |
164 response[2] = cx_str("\r\n"); |
| 135 response[3] = cx_str("Hello World!\n"); |
165 response[3] = cx_str("Hello World!\n"); |
| 136 |
166 |
| 137 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
167 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
| 138 |
168 |
| 139 CX_TEST_CALL_SUBROUTINE(test_httpclient, response, 4, out, 0); |
169 CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, response, 4, out); |
| 140 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n")); |
170 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n")); |
| 141 |
171 |
| 142 cxBufferFree(out); |
172 cxBufferFree(out); |
| 143 } |
173 } |
| 144 } |
174 } |
| 163 response[14] = cx_str("d!"); |
193 response[14] = cx_str("d!"); |
| 164 response[15] = cx_str("\n"); |
194 response[15] = cx_str("\n"); |
| 165 |
195 |
| 166 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
196 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
| 167 |
197 |
| 168 CX_TEST_CALL_SUBROUTINE(test_httpclient, response, 16, out, 0); |
198 CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, response, 16, out); |
| 169 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\nHello World!\n")); |
199 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\nHello World!\n")); |
| 170 |
200 |
| 171 cxBufferFree(out); |
201 cxBufferFree(out); |
| 172 } |
202 } |
| 173 } |
203 } |
| 184 response[i] = cx_strn(response_str.ptr+i, 1); |
214 response[i] = cx_strn(response_str.ptr+i, 1); |
| 185 } |
215 } |
| 186 |
216 |
| 187 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
217 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
| 188 |
218 |
| 189 CX_TEST_CALL_SUBROUTINE(test_httpclient, response, response_str.length, out, 0); |
219 CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, response, response_str.length, out); |
| 190 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n")); |
220 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n")); |
| 191 |
221 |
| 192 cxBufferFree(out); |
222 cxBufferFree(out); |
| 193 } |
223 } |
| 194 } |
224 } |
| |
225 |
| |
226 CX_TEST(test_http_client_post_ctlen) { |
| |
227 CX_TEST_DO { |
| |
228 cxstring response_str = cx_str( |
| |
229 "HTTP/1.1 200 OK\r\n" |
| |
230 "Content-length: 21\r\n" |
| |
231 "\r\n" |
| |
232 "Hello World!---post1\n"); |
| |
233 cxstring *response = calloc(response_str.length, sizeof(cxstring)); |
| |
234 for(int i=0;i<response_str.length;i++) { |
| |
235 response[i] = cx_strn(response_str.ptr+i, 1); |
| |
236 } |
| |
237 |
| |
238 cxstring request_body = cx_str("test request body, needs more than one read (len > 16)\n"); |
| |
239 |
| |
240 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
| |
241 |
| |
242 CX_TEST_CALL_SUBROUTINE(test_httpclient, request_body, FALSE, response, response_str.length, out); |
| |
243 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!---post1\n")); |
| |
244 |
| |
245 cxBufferFree(out); |
| |
246 } |
| |
247 } |
| |
248 |
| |
249 CX_TEST(test_http_client_post_chunked) { |
| |
250 CX_TEST_DO { |
| |
251 cxstring response_str = cx_str( |
| |
252 "HTTP/1.1 200 OK\r\n" |
| |
253 "Content-length: 21\r\n" |
| |
254 "\r\n" |
| |
255 "Hello World!---post1\n"); |
| |
256 cxstring *response = calloc(response_str.length, sizeof(cxstring)); |
| |
257 for(int i=0;i<response_str.length;i++) { |
| |
258 response[i] = cx_strn(response_str.ptr+i, 1); |
| |
259 } |
| |
260 |
| |
261 cxstring request_body = cx_str("test request body, needs more than one read (len > 16)\n"); |
| |
262 |
| |
263 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
| |
264 |
| |
265 CX_TEST_CALL_SUBROUTINE(test_httpclient, request_body, TRUE, response, response_str.length, out); |
| |
266 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!---post1\n")); |
| |
267 |
| |
268 cxBufferFree(out); |
| |
269 } |
| |
270 } |