src/server/test/httpclient.c

Tue, 24 Feb 2026 21:28:06 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 24 Feb 2026 21:28:06 +0100
changeset 703
395c62fac7e5
parent 701
936e5487418a
permissions
-rw-r--r--

add more httpclient tests

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

mercurial