src/server/test/httpclient.c

changeset 703
395c62fac7e5
parent 701
936e5487418a
equal deleted inserted replaced
702:ee80191310ca 703:395c62fac7e5
46 static void test_response_finished(HttpClient *client, void *userdata) { 46 static void test_response_finished(HttpClient *client, void *userdata) {
47 pthread_mutex_lock(&test_mutex); 47 pthread_mutex_lock(&test_mutex);
48 pthread_cond_signal(&test_cond); 48 pthread_cond_signal(&test_cond);
49 pthread_mutex_unlock(&test_mutex); 49 pthread_mutex_unlock(&test_mutex);
50 http_client_free(client); 50 http_client_free(client);
51 test_finished = 1;
51 } 52 }
52 53
53 static ssize_t test_response_body_write(HttpClient *client, void *buf, size_t nbytes, void *userdata) { 54 static ssize_t test_response_body_write(HttpClient *client, void *buf, size_t nbytes, void *userdata) {
54 char *str = buf; 55 char *str = buf;
55 return cxBufferWrite(buf, 1, nbytes, userdata); 56 return cxBufferWrite(buf, 1, nbytes, userdata);
56 } 57 }
57 58
58 CX_TEST_SUBROUTINE(test_httpclient, cxstring response, CxBuffer *out, int response_blocksz, int error_interval) { 59 CX_TEST_SUBROUTINE(test_httpclient, cxstring *response_blocks, size_t num_blocks, CxBuffer *out, int error_interval) {
59 pthread_mutex_init(&test_mutex, NULL); 60 pthread_mutex_init(&test_mutex, NULL);
60 pthread_cond_init(&test_cond, NULL); 61 pthread_cond_init(&test_cond, NULL);
61 62
62 if(!test_eventhandler) { 63 if(!test_eventhandler) {
63 EventHandlerConfig config; 64 EventHandlerConfig config;
78 79
79 http_client_set_socket(client, fds[0]); 80 http_client_set_socket(client, fds[0]);
80 http_client_set_method(client, "GET"); 81 http_client_set_method(client, "GET");
81 http_client_set_uri(client, "/testx01"); 82 http_client_set_uri(client, "/testx01");
82 83
84 test_finished = 0;
83 int ret = http_client_start(client); 85 int ret = http_client_start(client);
84 CX_TEST_ASSERT(ret == 0); 86 CX_TEST_ASSERT(ret == 0);
85 87
86 size_t response_pos = 0; 88 for(int i=0;i<num_blocks;i++) {
87 while(response_pos < response.length) { 89 size_t response_pos = 0;
88 size_t nbytes = response.length - response_pos; 90 cxstring response = response_blocks[i];
89 const char *str = response.ptr + response_pos; 91 while(response_pos < response.length) {
90 ssize_t w = write(fds[1], str, nbytes); 92 size_t nbytes = response.length - response_pos;
91 CX_TEST_ASSERT(w >= 0); 93 const char *str = response.ptr + response_pos;
92 response_pos += w; 94 ssize_t w = write(fds[1], str, nbytes);
95 CX_TEST_ASSERT(w >= 0);
96 response_pos += w;
97 }
93 } 98 }
99
94 close(fds[1]); 100 close(fds[1]);
95 101
96 pthread_mutex_lock(&test_mutex); 102 pthread_mutex_lock(&test_mutex);
97 if(test_finished == 0) { 103 if(test_finished == 0) {
98 pthread_cond_wait(&test_cond, &test_mutex); 104 pthread_cond_wait(&test_cond, &test_mutex);
99 } 105 }
100 pthread_mutex_unlock(&test_mutex); 106 pthread_mutex_unlock(&test_mutex);
101
102
103 } 107 }
104 108
105 CX_TEST(test_http_client_simple_get1) { 109 CX_TEST(test_http_client_simple_get1) {
106 CX_TEST_DO { 110 CX_TEST_DO {
107 cxstring response = cx_str( 111 cxstring response = cx_str(
109 "Content-length: 13\r\n" 113 "Content-length: 13\r\n"
110 "\r\n" 114 "\r\n"
111 "Hello World!\n"); 115 "Hello World!\n");
112 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); 116 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
113 117
114 CX_TEST_CALL_SUBROUTINE(test_httpclient, response, out, 1024, 0); 118 CX_TEST_CALL_SUBROUTINE(test_httpclient, &response, 1, out, 0);
115 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n")); 119 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
116 120
117 cxBufferFree(out); 121 cxBufferFree(out);
118 } 122 }
119 } 123 }
124
125 CX_TEST(test_http_client_simple_get_line_io) {
126 CX_TEST_DO {
127 cxstring response[8];
128 response[0] = cx_str("HTTP/1.1 200 OK\r\n");
129 response[1] = cx_str("Content-length: 13\r\n");
130 response[2] = cx_str("\r\n");
131 response[3] = cx_str("Hello World!\n");
132
133 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
134
135 CX_TEST_CALL_SUBROUTINE(test_httpclient, response, 4, out, 0);
136 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
137
138 cxBufferFree(out);
139 }
140 }
141
142 CX_TEST(test_http_client_simple_get_small_blocksize) {
143 CX_TEST_DO {
144 cxstring response[16];
145 response[0] = cx_str("HTTP/1.1");
146 response[1] = cx_str(" 2");
147 response[2] = cx_str("00 ");
148 response[3] = cx_str(" O");
149 response[4] = cx_str("K\r\nContent-");
150 response[5] = cx_str("length:");
151 response[6] = cx_str(" 26");
152 response[7] = cx_str("\r");
153 response[8] = cx_str("\nHost");
154 response[9] = cx_str(": localhost\r\n\r\nHello");
155 response[10] = cx_str(" World!\n");
156 response[11] = cx_str("He");
157 response[12] = cx_str("llo Wor");
158 response[13] = cx_str("l");
159 response[14] = cx_str("d!");
160 response[15] = cx_str("\n");
161
162 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
163
164 CX_TEST_CALL_SUBROUTINE(test_httpclient, response, 16, out, 0);
165 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\nHello World!\n"));
166
167 cxBufferFree(out);
168 }
169 }
170
171 CX_TEST(test_http_client_simple_get_1b_blocksize) {
172 CX_TEST_DO {
173 cxstring response_str = cx_str(
174 "HTTP/1.1 200 OK\r\n"
175 "Content-length: 13\r\n"
176 "\r\n"
177 "Hello World!\n");
178 cxstring *response = calloc(response_str.length, sizeof(cxstring));
179 for(int i=0;i<response_str.length;i++) {
180 response[i] = cx_strn(response_str.ptr+i, 1);
181 }
182
183 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
184
185 CX_TEST_CALL_SUBROUTINE(test_httpclient, response, response_str.length, out, 0);
186 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
187
188 cxBufferFree(out);
189 }
190 }

mercurial