Mercurial
comparison seobeo/tests/seobeo_client_test.c @ 119:c39582f937e5
[Seobeo Client] Added client side logic which will be used for all my other calls instead of curl.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Wed, 07 Jan 2026 16:05:57 -0800 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 118:249881ceff7b | 119:c39582f937e5 |
|---|---|
| 1 #include "seobeo/seobeo.h" | |
| 2 #include <stdio.h> | |
| 3 #include <stdlib.h> | |
| 4 | |
| 5 void Test_Simple_Get() | |
| 6 { | |
| 7 printf("\n=== Test 1: Simple GET Request ===\n"); | |
| 8 | |
| 9 Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/get"); | |
| 10 if (!p_req) | |
| 11 { | |
| 12 printf("Failed to create request\n"); | |
| 13 return; | |
| 14 } | |
| 15 | |
| 16 Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); | |
| 17 if (p_resp) | |
| 18 { | |
| 19 printf("Status: %d %s\n", p_resp->status_code, p_resp->status_text ? p_resp->status_text : ""); | |
| 20 printf("Body length: %zu bytes\n", p_resp->body_length); | |
| 21 if (p_resp->body && p_resp->body_length > 0) | |
| 22 { | |
| 23 printf("Body preview: %.200s...\n", p_resp->body); | |
| 24 } | |
| 25 Seobeo_Client_Response_Destroy(p_resp); | |
| 26 } | |
| 27 else | |
| 28 { | |
| 29 printf("Request failed\n"); | |
| 30 } | |
| 31 | |
| 32 Seobeo_Client_Request_Destroy(p_req); | |
| 33 } | |
| 34 | |
| 35 void Test_Custom_Headers_HashMap() | |
| 36 { | |
| 37 printf("\n=== Test 2: Custom Headers using HashMap ===\n"); | |
| 38 | |
| 39 Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/headers"); | |
| 40 if (!p_req) | |
| 41 { | |
| 42 printf("Failed to create request\n"); | |
| 43 return; | |
| 44 } | |
| 45 | |
| 46 Seobeo_Client_Request_Add_Header_Map(p_req, "User-Agent", "Seobeo/1.0"); | |
| 47 Seobeo_Client_Request_Add_Header_Map(p_req, "Accept", "application/json"); | |
| 48 Seobeo_Client_Request_Add_Header_Map(p_req, "X-Custom-Header", "CustomValue"); | |
| 49 | |
| 50 Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); | |
| 51 if (p_resp) | |
| 52 { | |
| 53 printf("Status: %d\n", p_resp->status_code); | |
| 54 if (p_resp->body) | |
| 55 { | |
| 56 printf("Response:\n%s\n", p_resp->body); | |
| 57 } | |
| 58 Seobeo_Client_Response_Destroy(p_resp); | |
| 59 } | |
| 60 else | |
| 61 { | |
| 62 printf("Request failed\n"); | |
| 63 } | |
| 64 | |
| 65 Seobeo_Client_Request_Destroy(p_req); | |
| 66 } | |
| 67 | |
| 68 void Test_Custom_Headers_Array() | |
| 69 { | |
| 70 printf("\n=== Test 3: Custom Headers using Array ===\n"); | |
| 71 | |
| 72 Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/headers"); | |
| 73 if (!p_req) | |
| 74 { | |
| 75 printf("Failed to create request\n"); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 Seobeo_Client_Request_Add_Header_Array(p_req, "User-Agent: Seobeo/1.0 (Array Mode)"); | |
| 80 Seobeo_Client_Request_Add_Header_Array(p_req, "Accept: application/json"); | |
| 81 Seobeo_Client_Request_Add_Header_Array(p_req, "X-Test-Header: TestValue"); | |
| 82 | |
| 83 Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); | |
| 84 if (p_resp) | |
| 85 { | |
| 86 printf("Status: %d\n", p_resp->status_code); | |
| 87 if (p_resp->body) | |
| 88 { | |
| 89 printf("Response:\n%s\n", p_resp->body); | |
| 90 } | |
| 91 Seobeo_Client_Response_Destroy(p_resp); | |
| 92 } | |
| 93 else | |
| 94 { | |
| 95 printf("Request failed\n"); | |
| 96 } | |
| 97 | |
| 98 Seobeo_Client_Request_Destroy(p_req); | |
| 99 } | |
| 100 | |
| 101 void Test_Post_With_Body() | |
| 102 { | |
| 103 printf("\n=== Test 4: POST Request with Body ===\n"); | |
| 104 | |
| 105 Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/post"); | |
| 106 if (!p_req) | |
| 107 { | |
| 108 printf("Failed to create request\n"); | |
| 109 return; | |
| 110 } | |
| 111 | |
| 112 Seobeo_Client_Request_Set_Method(p_req, "POST"); | |
| 113 Seobeo_Client_Request_Add_Header_Map(p_req, "Content-Type", "application/json"); | |
| 114 | |
| 115 const char *json_body = "{\"name\": \"Seobeo\", \"version\": \"1.0\", \"message\": \"Hello from Seobeo HTTP Client!\"}"; | |
| 116 Seobeo_Client_Request_Set_Body(p_req, json_body, 0); | |
| 117 | |
| 118 Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); | |
| 119 if (p_resp) | |
| 120 { | |
| 121 printf("Status: %d\n", p_resp->status_code); | |
| 122 if (p_resp->body) | |
| 123 { | |
| 124 printf("Response:\n%s\n", p_resp->body); | |
| 125 } | |
| 126 Seobeo_Client_Response_Destroy(p_resp); | |
| 127 } | |
| 128 else | |
| 129 { | |
| 130 printf("Request failed\n"); | |
| 131 } | |
| 132 | |
| 133 Seobeo_Client_Request_Destroy(p_req); | |
| 134 } | |
| 135 | |
| 136 void Test_Follow_Redirects() | |
| 137 { | |
| 138 printf("\n=== Test 5: Following Redirects ===\n"); | |
| 139 | |
| 140 Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/redirect/2"); | |
| 141 if (!p_req) | |
| 142 { | |
| 143 printf("Failed to create request\n"); | |
| 144 return; | |
| 145 } | |
| 146 | |
| 147 Seobeo_Client_Request_Set_Follow_Redirects(p_req, TRUE, 10); | |
| 148 | |
| 149 Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); | |
| 150 if (p_resp) | |
| 151 { | |
| 152 printf("Final Status: %d\n", p_resp->status_code); | |
| 153 printf("Body length: %zu bytes\n", p_resp->body_length); | |
| 154 Seobeo_Client_Response_Destroy(p_resp); | |
| 155 } | |
| 156 else | |
| 157 { | |
| 158 printf("Request failed\n"); | |
| 159 } | |
| 160 | |
| 161 Seobeo_Client_Request_Destroy(p_req); | |
| 162 } | |
| 163 | |
| 164 void Test_Download_File() | |
| 165 { | |
| 166 printf("\n=== Test 6: Download File ===\n"); | |
| 167 | |
| 168 Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://mrjunejune.com/public/epi_favicon.svg"); | |
| 169 if (!p_req) | |
| 170 { | |
| 171 printf("Failed to create request\n"); | |
| 172 return; | |
| 173 } | |
| 174 | |
| 175 Seobeo_Client_Request_Set_Download_Path(p_req, "downloaded_example.html"); | |
| 176 | |
| 177 Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); | |
| 178 if (p_resp) | |
| 179 { | |
| 180 printf("Status: %d\n", p_resp->status_code); | |
| 181 printf("Downloaded %zu bytes to 'downloaded_example.html'\n", p_resp->body_length); | |
| 182 Seobeo_Client_Response_Destroy(p_resp); | |
| 183 } | |
| 184 else | |
| 185 { | |
| 186 printf("Request failed\n"); | |
| 187 } | |
| 188 | |
| 189 Seobeo_Client_Request_Destroy(p_req); | |
| 190 } | |
| 191 | |
| 192 void Test_Mixed_Headers() | |
| 193 { | |
| 194 printf("\n=== Test 7: Mixed HashMap and Array Headers ===\n"); | |
| 195 | |
| 196 Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/headers"); | |
| 197 if (!p_req) | |
| 198 { | |
| 199 printf("Failed to create request\n"); | |
| 200 return; | |
| 201 } | |
| 202 | |
| 203 Seobeo_Client_Request_Add_Header_Map(p_req, "User-Agent", "Seobeo/1.0"); | |
| 204 Seobeo_Client_Request_Add_Header_Map(p_req, "Accept", "application/json"); | |
| 205 | |
| 206 Seobeo_Client_Request_Add_Header_Array(p_req, "X-Custom-1: Value1"); | |
| 207 Seobeo_Client_Request_Add_Header_Array(p_req, "X-Custom-2: Value2"); | |
| 208 | |
| 209 Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); | |
| 210 if (p_resp) | |
| 211 { | |
| 212 printf("Status: %d\n", p_resp->status_code); | |
| 213 if (p_resp->body) | |
| 214 { | |
| 215 printf("Response:\n%s\n", p_resp->body); | |
| 216 } | |
| 217 Seobeo_Client_Response_Destroy(p_resp); | |
| 218 } | |
| 219 else | |
| 220 { | |
| 221 printf("Request failed\n"); | |
| 222 } | |
| 223 | |
| 224 Seobeo_Client_Request_Destroy(p_req); | |
| 225 } | |
| 226 | |
| 227 void Test_Response_Headers() | |
| 228 { | |
| 229 printf("\n=== Test 8: Accessing Response Headers ===\n"); | |
| 230 | |
| 231 Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/get"); | |
| 232 if (!p_req) | |
| 233 { | |
| 234 printf("Failed to create request\n"); | |
| 235 return; | |
| 236 } | |
| 237 | |
| 238 Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); | |
| 239 if (p_resp) | |
| 240 { | |
| 241 printf("Status: %d %s\n", p_resp->status_code, p_resp->status_text ? p_resp->status_text : ""); | |
| 242 | |
| 243 if (p_resp->headers) | |
| 244 { | |
| 245 printf("\nResponse Headers:\n"); | |
| 246 size_t count = Dowa_Array_Length(p_resp->headers); | |
| 247 for (size_t i = 0; i < count; i++) | |
| 248 { | |
| 249 printf(" %s: %s\n", p_resp->headers[i].key, p_resp->headers[i].value); | |
| 250 } | |
| 251 } | |
| 252 | |
| 253 Seobeo_Client_Response_Destroy(p_resp); | |
| 254 } | |
| 255 else | |
| 256 { | |
| 257 printf("Request failed\n"); | |
| 258 } | |
| 259 | |
| 260 Seobeo_Client_Request_Destroy(p_req); | |
| 261 } | |
| 262 | |
| 263 int main() | |
| 264 { | |
| 265 printf("=== Seobeo HTTP Client Tests ===\n"); | |
| 266 | |
| 267 Test_Simple_Get(); | |
| 268 Test_Custom_Headers_HashMap(); | |
| 269 Test_Custom_Headers_Array(); | |
| 270 Test_Post_With_Body(); | |
| 271 Test_Follow_Redirects(); | |
| 272 Test_Download_File(); | |
| 273 Test_Mixed_Headers(); | |
| 274 Test_Response_Headers(); | |
| 275 | |
| 276 printf("\n=== All Tests Completed ===\n"); | |
| 277 return 0; | |
| 278 } |