diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/seobeo/seobeo_http_client_test.c	Wed Dec 24 09:15:55 2025 -0800
@@ -0,0 +1,174 @@
+#include "seobeo/seobeo.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+// Test basic HTTP client functionality
+void test_http_client_without_ssl() {
+  printf("Testing HTTP client (non-SSL)...\n");
+
+  // Create a client handle without TLS
+  Seobeo_Handle *h = Seobeo_Stream_Handle_Client_Create("example.com", "80", FALSE);
+
+  if (!h || h->socket < 0) {
+    printf("  ✗ Failed to create client handle\n");
+    if (h) Seobeo_Handle_Destroy(h);
+    return;
+  }
+
+  printf("  ✓ Client handle created successfully\n");
+
+  // Generate and send HTTP request
+  char request[4096];
+  int request_len = sprintf(
+    request,
+    "GET / HTTP/1.1\r\n"
+    "Host: example.com\r\n"
+    "Connection: close\r\n"
+    "\r\n"
+  );
+
+  Seobeo_Handle_Queue(h, (uint8*)request, (uint32)request_len);
+  int flush_result = Seobeo_Handle_Flush(h);
+
+  if (flush_result < 0) {
+    printf("  ✗ Failed to send request\n");
+    Seobeo_Handle_Destroy(h);
+    return;
+  }
+
+  printf("  ✓ Request sent successfully\n");
+
+  // Read response
+  int bytes_read = 0;
+  int total_bytes = 0;
+  int attempts = 0;
+  const int max_attempts = 100;
+
+  while (attempts++ < max_attempts) {
+    bytes_read = Seobeo_Handle_Read(h);
+    if (bytes_read > 0) {
+      total_bytes += bytes_read;
+    } else if (bytes_read == -2) {
+      // Connection closed
+      break;
+    } else if (bytes_read == 0) {
+      // Would block, wait a bit
+      usleep(10000); // 10ms
+      continue;
+    } else {
+      printf("  ✗ Error reading response\n");
+      Seobeo_Handle_Destroy(h);
+      return;
+    }
+  }
+
+  if (total_bytes > 0) {
+    printf("  ✓ Received response (%d bytes)\n", total_bytes);
+
+    // Check if we got HTTP response
+    if (h->read_buffer_len > 0 && strstr((char*)h->read_buffer, "HTTP/1.1") != NULL) {
+      printf("  ✓ Valid HTTP response received\n");
+    } else {
+      printf("  ✗ Invalid HTTP response\n");
+    }
+  } else {
+    printf("  ⚠ No response received (this may be a network issue)\n");
+  }
+
+  Seobeo_Handle_Destroy(h);
+  printf("  ✓ Client handle destroyed\n");
+}
+
+void test_http_client_with_ssl() {
+  printf("\nTesting HTTP client (with SSL)...\n");
+
+#ifdef SEOBEO_NO_SSL
+  printf("  ⚠ SSL not compiled in, skipping SSL test\n");
+  return;
+#else
+  // Create a client handle with TLS
+  Seobeo_Handle *h = Seobeo_Stream_Handle_Client_Create("example.com", "443", TRUE);
+
+  if (!h || h->socket < 0) {
+    printf("  ✗ Failed to create SSL client handle\n");
+    if (h) Seobeo_Handle_Destroy(h);
+    return;
+  }
+
+  printf("  ✓ SSL client handle created successfully\n");
+  printf("  ✓ SSL handshake completed\n");
+
+  // Generate and send HTTPS request
+  char request[4096];
+  int request_len = sprintf(
+    request,
+    "GET / HTTP/1.1\r\n"
+    "Host: example.com\r\n"
+    "Connection: close\r\n"
+    "\r\n"
+  );
+
+  Seobeo_Handle_Queue(h, (uint8*)request, (uint32)request_len);
+  int flush_result = Seobeo_Handle_Flush(h);
+
+  if (flush_result < 0) {
+    printf("  ✗ Failed to send SSL request\n");
+    Seobeo_Handle_Destroy(h);
+    return;
+  }
+
+  printf("  ✓ SSL request sent successfully\n");
+
+  // Read response
+  int bytes_read = 0;
+  int total_bytes = 0;
+  int attempts = 0;
+  const int max_attempts = 100;
+
+  while (attempts++ < max_attempts) {
+    bytes_read = Seobeo_Handle_Read(h);
+    if (bytes_read > 0) {
+      total_bytes += bytes_read;
+    } else if (bytes_read == -2) {
+      // Connection closed
+      break;
+    } else if (bytes_read == 0) {
+      // Would block, wait a bit
+      usleep(10000); // 10ms
+      continue;
+    } else {
+      printf("  ✗ Error reading SSL response\n");
+      Seobeo_Handle_Destroy(h);
+      return;
+    }
+  }
+
+  if (total_bytes > 0) {
+    printf("  ✓ Received SSL response (%d bytes)\n", total_bytes);
+
+    // Check if we got HTTP response
+    if (h->read_buffer_len > 0 && strstr((char*)h->read_buffer, "HTTP/1.1") != NULL) {
+      printf("  ✓ Valid HTTPS response received\n");
+    } else {
+      printf("  ✗ Invalid HTTPS response\n");
+    }
+  } else {
+    printf("  ⚠ No SSL response received (this may be a network issue)\n");
+  }
+
+  Seobeo_Handle_Destroy(h);
+  printf("  ✓ SSL client handle destroyed\n");
+#endif
+}
+
+int main() {
+  printf("=== Seobeo HTTP Client Tests ===\n\n");
+
+  test_http_client_without_ssl();
+  test_http_client_with_ssl();
+
+  printf("\n=== Tests Complete ===\n");
+  return 0;
+}