| |
1 /* |
| |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| |
3 * |
| |
4 * Copyright 2026 Olaf Wintermann. All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright |
| |
10 * notice, this list of conditions and the following disclaimer. |
| |
11 * |
| |
12 * 2. Redistributions in binary form must reproduce the above copyright |
| |
13 * notice, this list of conditions and the following disclaimer in the |
| |
14 * documentation and/or other materials provided with the distribution. |
| |
15 * |
| |
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
26 * POSSIBILITY OF SUCH DAMAGE. |
| |
27 */ |
| |
28 |
| |
29 |
| |
30 #include "httpclient.h" |
| |
31 |
| |
32 #include "../util/socket.h" |
| |
33 #include "../daemon/event.h" |
| |
34 #include "../proxy/httpclient.h" |
| |
35 |
| |
36 #include <cx/buffer.h> |
| |
37 #include <cx/string.h> |
| |
38 |
| |
39 #include <pthread.h> |
| |
40 |
| |
41 static EVHandler *test_eventhandler; |
| |
42 static pthread_mutex_t test_mutex; |
| |
43 static pthread_cond_t test_cond; |
| |
44 static volatile int test_finished; |
| |
45 |
| |
46 static void test_response_finished(HttpClient *client, void *userdata) { |
| |
47 pthread_mutex_lock(&test_mutex); |
| |
48 pthread_cond_signal(&test_cond); |
| |
49 pthread_mutex_unlock(&test_mutex); |
| |
50 http_client_free(client); |
| |
51 } |
| |
52 |
| |
53 static ssize_t test_response_body_write(HttpClient *client, void *buf, size_t nbytes, void *userdata) { |
| |
54 char *str = buf; |
| |
55 return cxBufferWrite(buf, 1, nbytes, userdata); |
| |
56 } |
| |
57 |
| |
58 CX_TEST_SUBROUTINE(test_httpclient, cxstring response, CxBuffer *out, int response_blocksz, int error_interval) { |
| |
59 pthread_mutex_init(&test_mutex, NULL); |
| |
60 pthread_cond_init(&test_cond, NULL); |
| |
61 |
| |
62 if(!test_eventhandler) { |
| |
63 EventHandlerConfig config; |
| |
64 config.isdefault = 0; |
| |
65 config.name = cx_str(NULL); |
| |
66 config.nthreads = 1; |
| |
67 test_eventhandler = evhandler_create(&config); |
| |
68 } |
| |
69 |
| |
70 HttpClient *client = http_client_new(test_eventhandler->instances[0]); |
| |
71 client->response_body_write = test_response_body_write; |
| |
72 client->response_body_write_userdata = out; |
| |
73 client->response_finished = test_response_finished; |
| |
74 |
| |
75 int fds[2]; |
| |
76 util_socketpair(fds); |
| |
77 util_socket_setnonblock(fds[0], 1); |
| |
78 |
| |
79 http_client_set_socket(client, fds[0]); |
| |
80 http_client_set_method(client, "GET"); |
| |
81 http_client_set_uri(client, "/testx01"); |
| |
82 |
| |
83 int ret = http_client_start(client); |
| |
84 CX_TEST_ASSERT(ret == 0); |
| |
85 |
| |
86 size_t response_pos = 0; |
| |
87 while(response_pos < response.length) { |
| |
88 size_t nbytes = response.length - response_pos; |
| |
89 const char *str = response.ptr + response_pos; |
| |
90 ssize_t w = write(fds[1], str, nbytes); |
| |
91 CX_TEST_ASSERT(w >= 0); |
| |
92 response_pos += w; |
| |
93 } |
| |
94 close(fds[1]); |
| |
95 |
| |
96 pthread_mutex_lock(&test_mutex); |
| |
97 if(test_finished == 0) { |
| |
98 pthread_cond_wait(&test_cond, &test_mutex); |
| |
99 } |
| |
100 pthread_mutex_unlock(&test_mutex); |
| |
101 |
| |
102 |
| |
103 } |
| |
104 |
| |
105 CX_TEST(test_http_client_simple_get1) { |
| |
106 CX_TEST_DO { |
| |
107 cxstring response = cx_str( |
| |
108 "HTTP/1.1 200 OK\r\n" |
| |
109 "Content-length: 13\r\n" |
| |
110 "\r\n" |
| |
111 "Hello World!\n"); |
| |
112 CxBuffer *out = cxBufferCreate(NULL, NULL, 256, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS); |
| |
113 |
| |
114 CX_TEST_CALL_SUBROUTINE(test_httpclient, response, out, 1024, 0); |
| |
115 CX_TEST_ASSERT(!cx_strcmp(cx_strn(out->space, out->size), "Hello World!\n")); |
| |
116 |
| |
117 cxBufferFree(out); |
| |
118 } |
| |
119 } |