comparison seobeo/seobeo_http_client_test.c @ 67:6626ec933933

[Seobeo] Separated out Client Server logic. Created test tools.
author June Park <parkjune1995@gmail.com>
date Wed, 24 Dec 2025 09:15:55 -0800
parents
children
comparison
equal deleted inserted replaced
66:a0f0ad5e42eb 67:6626ec933933
1 #include "seobeo/seobeo.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <assert.h>
6
7 // Test basic HTTP client functionality
8 void test_http_client_without_ssl() {
9 printf("Testing HTTP client (non-SSL)...\n");
10
11 // Create a client handle without TLS
12 Seobeo_Handle *h = Seobeo_Stream_Handle_Client_Create("example.com", "80", FALSE);
13
14 if (!h || h->socket < 0) {
15 printf(" ✗ Failed to create client handle\n");
16 if (h) Seobeo_Handle_Destroy(h);
17 return;
18 }
19
20 printf(" ✓ Client handle created successfully\n");
21
22 // Generate and send HTTP request
23 char request[4096];
24 int request_len = sprintf(
25 request,
26 "GET / HTTP/1.1\r\n"
27 "Host: example.com\r\n"
28 "Connection: close\r\n"
29 "\r\n"
30 );
31
32 Seobeo_Handle_Queue(h, (uint8*)request, (uint32)request_len);
33 int flush_result = Seobeo_Handle_Flush(h);
34
35 if (flush_result < 0) {
36 printf(" ✗ Failed to send request\n");
37 Seobeo_Handle_Destroy(h);
38 return;
39 }
40
41 printf(" ✓ Request sent successfully\n");
42
43 // Read response
44 int bytes_read = 0;
45 int total_bytes = 0;
46 int attempts = 0;
47 const int max_attempts = 100;
48
49 while (attempts++ < max_attempts) {
50 bytes_read = Seobeo_Handle_Read(h);
51 if (bytes_read > 0) {
52 total_bytes += bytes_read;
53 } else if (bytes_read == -2) {
54 // Connection closed
55 break;
56 } else if (bytes_read == 0) {
57 // Would block, wait a bit
58 usleep(10000); // 10ms
59 continue;
60 } else {
61 printf(" ✗ Error reading response\n");
62 Seobeo_Handle_Destroy(h);
63 return;
64 }
65 }
66
67 if (total_bytes > 0) {
68 printf(" ✓ Received response (%d bytes)\n", total_bytes);
69
70 // Check if we got HTTP response
71 if (h->read_buffer_len > 0 && strstr((char*)h->read_buffer, "HTTP/1.1") != NULL) {
72 printf(" ✓ Valid HTTP response received\n");
73 } else {
74 printf(" ✗ Invalid HTTP response\n");
75 }
76 } else {
77 printf(" ⚠ No response received (this may be a network issue)\n");
78 }
79
80 Seobeo_Handle_Destroy(h);
81 printf(" ✓ Client handle destroyed\n");
82 }
83
84 void test_http_client_with_ssl() {
85 printf("\nTesting HTTP client (with SSL)...\n");
86
87 #ifdef SEOBEO_NO_SSL
88 printf(" ⚠ SSL not compiled in, skipping SSL test\n");
89 return;
90 #else
91 // Create a client handle with TLS
92 Seobeo_Handle *h = Seobeo_Stream_Handle_Client_Create("example.com", "443", TRUE);
93
94 if (!h || h->socket < 0) {
95 printf(" ✗ Failed to create SSL client handle\n");
96 if (h) Seobeo_Handle_Destroy(h);
97 return;
98 }
99
100 printf(" ✓ SSL client handle created successfully\n");
101 printf(" ✓ SSL handshake completed\n");
102
103 // Generate and send HTTPS request
104 char request[4096];
105 int request_len = sprintf(
106 request,
107 "GET / HTTP/1.1\r\n"
108 "Host: example.com\r\n"
109 "Connection: close\r\n"
110 "\r\n"
111 );
112
113 Seobeo_Handle_Queue(h, (uint8*)request, (uint32)request_len);
114 int flush_result = Seobeo_Handle_Flush(h);
115
116 if (flush_result < 0) {
117 printf(" ✗ Failed to send SSL request\n");
118 Seobeo_Handle_Destroy(h);
119 return;
120 }
121
122 printf(" ✓ SSL request sent successfully\n");
123
124 // Read response
125 int bytes_read = 0;
126 int total_bytes = 0;
127 int attempts = 0;
128 const int max_attempts = 100;
129
130 while (attempts++ < max_attempts) {
131 bytes_read = Seobeo_Handle_Read(h);
132 if (bytes_read > 0) {
133 total_bytes += bytes_read;
134 } else if (bytes_read == -2) {
135 // Connection closed
136 break;
137 } else if (bytes_read == 0) {
138 // Would block, wait a bit
139 usleep(10000); // 10ms
140 continue;
141 } else {
142 printf(" ✗ Error reading SSL response\n");
143 Seobeo_Handle_Destroy(h);
144 return;
145 }
146 }
147
148 if (total_bytes > 0) {
149 printf(" ✓ Received SSL response (%d bytes)\n", total_bytes);
150
151 // Check if we got HTTP response
152 if (h->read_buffer_len > 0 && strstr((char*)h->read_buffer, "HTTP/1.1") != NULL) {
153 printf(" ✓ Valid HTTPS response received\n");
154 } else {
155 printf(" ✗ Invalid HTTPS response\n");
156 }
157 } else {
158 printf(" ⚠ No SSL response received (this may be a network issue)\n");
159 }
160
161 Seobeo_Handle_Destroy(h);
162 printf(" ✓ SSL client handle destroyed\n");
163 #endif
164 }
165
166 int main() {
167 printf("=== Seobeo HTTP Client Tests ===\n\n");
168
169 test_http_client_without_ssl();
170 test_http_client_with_ssl();
171
172 printf("\n=== Tests Complete ===\n");
173 return 0;
174 }