src/server/test/httpclient.c

Wed, 25 Feb 2026 23:14:47 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Wed, 25 Feb 2026 23:14:47 +0100
changeset 706
df64b4b79912
parent 705
30de3bfd0412
permissions
-rw-r--r--

add error handling in proxy_request_read

701
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2026 Olaf Wintermann. All rights reserved.
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include "httpclient.h"
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include "../util/socket.h"
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "../daemon/event.h"
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "../proxy/httpclient.h"
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include <cx/buffer.h>
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include <cx/string.h>
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include <pthread.h>
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 static EVHandler *test_eventhandler;
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 static pthread_mutex_t test_mutex;
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 static pthread_cond_t test_cond;
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 static volatile int test_finished;
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
704
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
46 void http_client_tests_init(void) {
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
47 pthread_mutex_init(&test_mutex, NULL);
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
48 pthread_cond_init(&test_cond, NULL);
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
49
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
50 EventHandlerConfig config;
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
51 config.isdefault = 0;
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
52 config.name = cx_str(NULL);
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
53 config.nthreads = 1;
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
54 test_eventhandler = evhandler_create(&config);
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
55 }
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
56
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
57 void http_client_tests_cleanup(void) {
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
58 ev_instance_shutdown(test_eventhandler->instances[0]);
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
59 }
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
60
701
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 static void test_response_finished(HttpClient *client, void *userdata) {
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 pthread_mutex_lock(&test_mutex);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 pthread_cond_signal(&test_cond);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 pthread_mutex_unlock(&test_mutex);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 http_client_free(client);
703
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
66 test_finished = 1;
701
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 }
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 static ssize_t test_response_body_write(HttpClient *client, void *buf, size_t nbytes, void *userdata) {
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 char *str = buf;
704
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
71 return cxBufferWrite(str, 1, nbytes, userdata);
701
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 }
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
705
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
74 static cxstring request_body_str;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
75 static int request_body_pos = 0;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
76 static size_t request_body_max_read = 16;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
77
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
78 ssize_t test_request_body_read(HttpClient *client, void *buf, size_t nbytes, void *userdata) {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
79 if(nbytes > request_body_max_read) {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
80 nbytes = request_body_max_read;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
81 }
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
82 size_t available = request_body_str.length - request_body_pos;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
83 if(nbytes > available) {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
84 nbytes = available;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
85 }
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
86 if(nbytes > 0) {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
87 memcpy(buf, request_body_str.ptr + request_body_pos, nbytes);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
88 request_body_pos += nbytes;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
89 }
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
90 return nbytes;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
91 }
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
92
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
93 CX_TEST_SUBROUTINE(test_httpclient, cxstring request_body, int chunked_transfer, cxstring *response_blocks, size_t num_blocks, CxBuffer *out) {
701
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 HttpClient *client = http_client_new(test_eventhandler->instances[0]);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 client->response_body_write = test_response_body_write;
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 client->response_body_write_userdata = out;
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 client->response_finished = test_response_finished;
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 int fds[2];
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 util_socketpair(fds);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 util_socket_setnonblock(fds[0], 1);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 http_client_set_socket(client, fds[0]);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 http_client_set_method(client, "GET");
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 http_client_set_uri(client, "/testx01");
705
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
106
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
107 request_body_str = request_body;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
108 request_body_pos = 0;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
109 if(request_body.length > 0) {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
110 client->request_body_read = test_request_body_read;
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
111 if(chunked_transfer) {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
112 http_client_enable_chunked_transfer_encoding(client);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
113 } else {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
114 http_client_set_content_length(client, request_body.length);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
115 }
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
116 }
704
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
117
703
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
118 test_finished = 0;
701
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 int ret = http_client_start(client);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 CX_TEST_ASSERT(ret == 0);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121
703
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
122 for(int i=0;i<num_blocks;i++) {
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
123 size_t response_pos = 0;
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
124 cxstring response = response_blocks[i];
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
125 while(response_pos < response.length) {
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
126 size_t nbytes = response.length - response_pos;
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
127 const char *str = response.ptr + response_pos;
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
128 ssize_t w = write(fds[1], str, nbytes);
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
129 CX_TEST_ASSERT(w >= 0);
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
130 response_pos += w;
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
131 }
701
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 }
703
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
133
701
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 pthread_mutex_lock(&test_mutex);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 if(test_finished == 0) {
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 pthread_cond_wait(&test_cond, &test_mutex);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 }
703
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
138 pthread_mutex_unlock(&test_mutex);
704
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
139
778dcf4ad63c fix multiple reads could corrupt the httpclient buffer content
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 703
diff changeset
140 close(fds[1]);
701
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 }
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 CX_TEST(test_http_client_simple_get1) {
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 CX_TEST_DO {
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 cxstring response = cx_str(
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 "HTTP/1.1 200 OK\r\n"
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 "Content-length: 13\r\n"
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 "\r\n"
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 "Hello World!\n");
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151
705
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
152 CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, &response, 1, out);
703
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
153 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
154
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
155 cxBufferFree(out);
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
156 }
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
157 }
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
158
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
159 CX_TEST(test_http_client_simple_get_line_io) {
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
160 CX_TEST_DO {
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
161 cxstring response[8];
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
162 response[0] = cx_str("HTTP/1.1 200 OK\r\n");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
163 response[1] = cx_str("Content-length: 13\r\n");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
164 response[2] = cx_str("\r\n");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
165 response[3] = cx_str("Hello World!\n");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
166
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
167 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
168
705
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
169 CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, response, 4, out);
701
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 cxBufferFree(out);
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
936e5487418a add first full httpclient test
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 }
703
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
175
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
176 CX_TEST(test_http_client_simple_get_small_blocksize) {
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
177 CX_TEST_DO {
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
178 cxstring response[16];
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
179 response[0] = cx_str("HTTP/1.1");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
180 response[1] = cx_str(" 2");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
181 response[2] = cx_str("00 ");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
182 response[3] = cx_str(" O");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
183 response[4] = cx_str("K\r\nContent-");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
184 response[5] = cx_str("length:");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
185 response[6] = cx_str(" 26");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
186 response[7] = cx_str("\r");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
187 response[8] = cx_str("\nHost");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
188 response[9] = cx_str(": localhost\r\n\r\nHello");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
189 response[10] = cx_str(" World!\n");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
190 response[11] = cx_str("He");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
191 response[12] = cx_str("llo Wor");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
192 response[13] = cx_str("l");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
193 response[14] = cx_str("d!");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
194 response[15] = cx_str("\n");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
195
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
196 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
197
705
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
198 CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, response, 16, out);
703
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
199 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\nHello World!\n"));
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
200
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
201 cxBufferFree(out);
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
202 }
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
203 }
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
204
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
205 CX_TEST(test_http_client_simple_get_1b_blocksize) {
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
206 CX_TEST_DO {
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
207 cxstring response_str = cx_str(
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
208 "HTTP/1.1 200 OK\r\n"
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
209 "Content-length: 13\r\n"
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
210 "\r\n"
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
211 "Hello World!\n");
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
212 cxstring *response = calloc(response_str.length, sizeof(cxstring));
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
213 for(int i=0;i<response_str.length;i++) {
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
214 response[i] = cx_strn(response_str.ptr+i, 1);
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
215 }
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
216
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
217 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
218
705
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
219 CX_TEST_CALL_SUBROUTINE(test_httpclient, cx_str(NULL), FALSE, response, response_str.length, out);
703
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
220 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n"));
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
221
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
222 cxBufferFree(out);
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
223 }
395c62fac7e5 add more httpclient tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 701
diff changeset
224 }
705
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
225
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
226 CX_TEST(test_http_client_post_ctlen) {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
227 CX_TEST_DO {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
228 cxstring response_str = cx_str(
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
229 "HTTP/1.1 200 OK\r\n"
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
230 "Content-length: 21\r\n"
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
231 "\r\n"
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
232 "Hello World!---post1\n");
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
233 cxstring *response = calloc(response_str.length, sizeof(cxstring));
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
234 for(int i=0;i<response_str.length;i++) {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
235 response[i] = cx_strn(response_str.ptr+i, 1);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
236 }
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
237
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
238 cxstring request_body = cx_str("test request body, needs more than one read (len > 16)\n");
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
239
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
240 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
241
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
242 CX_TEST_CALL_SUBROUTINE(test_httpclient, request_body, FALSE, response, response_str.length, out);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
243 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!---post1\n"));
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
244
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
245 cxBufferFree(out);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
246 }
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
247 }
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
248
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
249 CX_TEST(test_http_client_post_chunked) {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
250 CX_TEST_DO {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
251 cxstring response_str = cx_str(
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
252 "HTTP/1.1 200 OK\r\n"
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
253 "Content-length: 21\r\n"
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
254 "\r\n"
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
255 "Hello World!---post1\n");
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
256 cxstring *response = calloc(response_str.length, sizeof(cxstring));
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
257 for(int i=0;i<response_str.length;i++) {
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
258 response[i] = cx_strn(response_str.ptr+i, 1);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
259 }
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
260
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
261 cxstring request_body = cx_str("test request body, needs more than one read (len > 16)\n");
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
262
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
263 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
264
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
265 CX_TEST_CALL_SUBROUTINE(test_httpclient, request_body, TRUE, response, response_str.length, out);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
266 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!---post1\n"));
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
267
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
268 cxBufferFree(out);
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
269 }
30de3bfd0412 add httpclient request body tests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 704
diff changeset
270 }

mercurial