Mercurial
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/seobeo/tests/seobeo_client_test.c Wed Jan 07 16:05:57 2026 -0800 @@ -0,0 +1,278 @@ +#include "seobeo/seobeo.h" +#include <stdio.h> +#include <stdlib.h> + +void Test_Simple_Get() +{ + printf("\n=== Test 1: Simple GET Request ===\n"); + + Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/get"); + if (!p_req) + { + printf("Failed to create request\n"); + return; + } + + Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); + if (p_resp) + { + printf("Status: %d %s\n", p_resp->status_code, p_resp->status_text ? p_resp->status_text : ""); + printf("Body length: %zu bytes\n", p_resp->body_length); + if (p_resp->body && p_resp->body_length > 0) + { + printf("Body preview: %.200s...\n", p_resp->body); + } + Seobeo_Client_Response_Destroy(p_resp); + } + else + { + printf("Request failed\n"); + } + + Seobeo_Client_Request_Destroy(p_req); +} + +void Test_Custom_Headers_HashMap() +{ + printf("\n=== Test 2: Custom Headers using HashMap ===\n"); + + Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/headers"); + if (!p_req) + { + printf("Failed to create request\n"); + return; + } + + Seobeo_Client_Request_Add_Header_Map(p_req, "User-Agent", "Seobeo/1.0"); + Seobeo_Client_Request_Add_Header_Map(p_req, "Accept", "application/json"); + Seobeo_Client_Request_Add_Header_Map(p_req, "X-Custom-Header", "CustomValue"); + + Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); + if (p_resp) + { + printf("Status: %d\n", p_resp->status_code); + if (p_resp->body) + { + printf("Response:\n%s\n", p_resp->body); + } + Seobeo_Client_Response_Destroy(p_resp); + } + else + { + printf("Request failed\n"); + } + + Seobeo_Client_Request_Destroy(p_req); +} + +void Test_Custom_Headers_Array() +{ + printf("\n=== Test 3: Custom Headers using Array ===\n"); + + Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/headers"); + if (!p_req) + { + printf("Failed to create request\n"); + return; + } + + Seobeo_Client_Request_Add_Header_Array(p_req, "User-Agent: Seobeo/1.0 (Array Mode)"); + Seobeo_Client_Request_Add_Header_Array(p_req, "Accept: application/json"); + Seobeo_Client_Request_Add_Header_Array(p_req, "X-Test-Header: TestValue"); + + Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); + if (p_resp) + { + printf("Status: %d\n", p_resp->status_code); + if (p_resp->body) + { + printf("Response:\n%s\n", p_resp->body); + } + Seobeo_Client_Response_Destroy(p_resp); + } + else + { + printf("Request failed\n"); + } + + Seobeo_Client_Request_Destroy(p_req); +} + +void Test_Post_With_Body() +{ + printf("\n=== Test 4: POST Request with Body ===\n"); + + Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/post"); + if (!p_req) + { + printf("Failed to create request\n"); + return; + } + + Seobeo_Client_Request_Set_Method(p_req, "POST"); + Seobeo_Client_Request_Add_Header_Map(p_req, "Content-Type", "application/json"); + + const char *json_body = "{\"name\": \"Seobeo\", \"version\": \"1.0\", \"message\": \"Hello from Seobeo HTTP Client!\"}"; + Seobeo_Client_Request_Set_Body(p_req, json_body, 0); + + Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); + if (p_resp) + { + printf("Status: %d\n", p_resp->status_code); + if (p_resp->body) + { + printf("Response:\n%s\n", p_resp->body); + } + Seobeo_Client_Response_Destroy(p_resp); + } + else + { + printf("Request failed\n"); + } + + Seobeo_Client_Request_Destroy(p_req); +} + +void Test_Follow_Redirects() +{ + printf("\n=== Test 5: Following Redirects ===\n"); + + Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/redirect/2"); + if (!p_req) + { + printf("Failed to create request\n"); + return; + } + + Seobeo_Client_Request_Set_Follow_Redirects(p_req, TRUE, 10); + + Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); + if (p_resp) + { + printf("Final Status: %d\n", p_resp->status_code); + printf("Body length: %zu bytes\n", p_resp->body_length); + Seobeo_Client_Response_Destroy(p_resp); + } + else + { + printf("Request failed\n"); + } + + Seobeo_Client_Request_Destroy(p_req); +} + +void Test_Download_File() +{ + printf("\n=== Test 6: Download File ===\n"); + + Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://mrjunejune.com/public/epi_favicon.svg"); + if (!p_req) + { + printf("Failed to create request\n"); + return; + } + + Seobeo_Client_Request_Set_Download_Path(p_req, "downloaded_example.html"); + + Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); + if (p_resp) + { + printf("Status: %d\n", p_resp->status_code); + printf("Downloaded %zu bytes to 'downloaded_example.html'\n", p_resp->body_length); + Seobeo_Client_Response_Destroy(p_resp); + } + else + { + printf("Request failed\n"); + } + + Seobeo_Client_Request_Destroy(p_req); +} + +void Test_Mixed_Headers() +{ + printf("\n=== Test 7: Mixed HashMap and Array Headers ===\n"); + + Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/headers"); + if (!p_req) + { + printf("Failed to create request\n"); + return; + } + + Seobeo_Client_Request_Add_Header_Map(p_req, "User-Agent", "Seobeo/1.0"); + Seobeo_Client_Request_Add_Header_Map(p_req, "Accept", "application/json"); + + Seobeo_Client_Request_Add_Header_Array(p_req, "X-Custom-1: Value1"); + Seobeo_Client_Request_Add_Header_Array(p_req, "X-Custom-2: Value2"); + + Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); + if (p_resp) + { + printf("Status: %d\n", p_resp->status_code); + if (p_resp->body) + { + printf("Response:\n%s\n", p_resp->body); + } + Seobeo_Client_Response_Destroy(p_resp); + } + else + { + printf("Request failed\n"); + } + + Seobeo_Client_Request_Destroy(p_req); +} + +void Test_Response_Headers() +{ + printf("\n=== Test 8: Accessing Response Headers ===\n"); + + Seobeo_Client_Request *p_req = Seobeo_Client_Request_Create("https://httpbin.org/get"); + if (!p_req) + { + printf("Failed to create request\n"); + return; + } + + Seobeo_Client_Response *p_resp = Seobeo_Client_Request_Execute(p_req); + if (p_resp) + { + printf("Status: %d %s\n", p_resp->status_code, p_resp->status_text ? p_resp->status_text : ""); + + if (p_resp->headers) + { + printf("\nResponse Headers:\n"); + size_t count = Dowa_Array_Length(p_resp->headers); + for (size_t i = 0; i < count; i++) + { + printf(" %s: %s\n", p_resp->headers[i].key, p_resp->headers[i].value); + } + } + + Seobeo_Client_Response_Destroy(p_resp); + } + else + { + printf("Request failed\n"); + } + + Seobeo_Client_Request_Destroy(p_req); +} + +int main() +{ + printf("=== Seobeo HTTP Client Tests ===\n"); + + Test_Simple_Get(); + Test_Custom_Headers_HashMap(); + Test_Custom_Headers_Array(); + Test_Post_With_Body(); + Test_Follow_Redirects(); + Test_Download_File(); + Test_Mixed_Headers(); + Test_Response_Headers(); + + printf("\n=== All Tests Completed ===\n"); + return 0; +}