Mercurial
comparison mrjunejune/test/integration_test.c @ 128:7eb79fd91c7e
[Misc] Fixed all bazel targets. I should creat a separate scripts for these lol.
| author | June Park <parkjune1995@gmail.com> |
|---|---|
| date | Thu, 08 Jan 2026 19:20:56 -0800 |
| parents | e7899c93da77 |
| children | f3084bca7317 |
comparison
equal
deleted
inserted
replaced
| 127:9af248484ba2 | 128:7eb79fd91c7e |
|---|---|
| 1 #include "seobeo/test/test.h" | 1 #include "mrjunejune/test/test.h" |
| 2 | 2 |
| 3 // Test case structure | 3 // Test case structure |
| 4 typedef struct { | 4 typedef struct { |
| 5 const char *path; | 5 const char *path; |
| 6 int expected_status; | 6 int expected_status; |
| 7 const char *expected_content; // Loaded from file | 7 const char *expected_content; // Loaded from file |
| 8 char *expected_file_path; // Path to expected snapshot file | 8 char *expected_file_path; // Path to expected snapshot file |
| 9 char *actual_response; | 9 char *actual_response; |
| 10 size_t response_len; | 10 size_t response_len; |
| 11 } TestCase; | 11 } TestCase; |
| 12 | |
| 13 void debug_diff(const char *expected, const char *actual) | |
| 14 { | |
| 15 printf("\n--- DIFF (Expected vs Actual) ---\n"); | |
| 16 | |
| 17 // Create copies to use with strtok (strtok modifies the string) | |
| 18 char *exp_copy = strdup(expected); | |
| 19 char *act_copy = strdup(actual); | |
| 20 | |
| 21 char *exp_line = strtok(exp_copy, "\n"); | |
| 22 char *act_line = strtok(act_copy, "\n"); | |
| 23 | |
| 24 int line_num = 1; | |
| 25 while (exp_line != NULL || act_line != NULL) { | |
| 26 if (exp_line && act_line && strcmp(exp_line, act_line) == 0) { | |
| 27 // Lines match - optional: print nothing or a dot | |
| 28 } else { | |
| 29 printf("Line %d mismatch:\n", line_num); | |
| 30 printf(" EXP: [%s]\n", exp_line ? exp_line : "(end of string)"); | |
| 31 printf(" ACT: [%s]\n", act_line ? act_line : "(end of string)"); | |
| 32 printf(" -----------------------------------\n"); | |
| 33 } | |
| 34 | |
| 35 exp_line = strtok(NULL, "\n"); | |
| 36 act_line = strtok(NULL, "\n"); | |
| 37 line_num++; | |
| 38 } | |
| 39 | |
| 40 free(exp_copy); | |
| 41 free(act_copy); | |
| 42 } | |
| 12 | 43 |
| 13 // Helper: Convert URL path to filename | 44 // Helper: Convert URL path to filename |
| 14 // "/" -> "root.snapshot" | 45 // "/" -> "root.snapshot" |
| 15 // "/index.html" -> "index.html.snapshot" | 46 // "/index.html" -> "index.html.snapshot" |
| 16 // "/api/users" -> "api_users.snapshot" | 47 // "/api/users" -> "api_users.snapshot" |
| 147 | 178 |
| 148 if (strcmp(p_resp->body, test->expected_content) != 0) | 179 if (strcmp(p_resp->body, test->expected_content) != 0) |
| 149 { | 180 { |
| 150 printf(" ✗ Response does not match expected snapshot\n"); | 181 printf(" ✗ Response does not match expected snapshot\n"); |
| 151 printf(" Expected file: %s\n", test->expected_file_path); | 182 printf(" Expected file: %s\n", test->expected_file_path); |
| 183 debug_diff(p_resp->body, test->expected_content); | |
| 152 Seobeo_Client_Response_Destroy(p_resp); | 184 Seobeo_Client_Response_Destroy(p_resp); |
| 153 Seobeo_Client_Request_Destroy(p_req); | 185 Seobeo_Client_Request_Destroy(p_req); |
| 154 return -1; | 186 return -1; |
| 155 } | 187 } |
| 156 | 188 |
| 295 Seobeo_Client_Response_Destroy(p_resp); | 327 Seobeo_Client_Response_Destroy(p_resp); |
| 296 Seobeo_Client_Request_Destroy(p_req); | 328 Seobeo_Client_Request_Destroy(p_req); |
| 297 return 0; | 329 return 0; |
| 298 } | 330 } |
| 299 | 331 |
| 300 pid_t start_test_server(const char *server_binary) | |
| 301 { | |
| 302 pid_t server_pid = fork(); | |
| 303 | |
| 304 if (server_pid < 0) | |
| 305 { | |
| 306 perror("fork"); | |
| 307 return -1; | |
| 308 } | |
| 309 | |
| 310 if (server_pid == 0) | |
| 311 { | |
| 312 printf("Starting server on port %s...\n", TEST_PORT); | |
| 313 execl(server_binary, server_binary, NULL); | |
| 314 perror("execl failed"); | |
| 315 exit(1); | |
| 316 } | |
| 317 | |
| 318 printf("Server started (PID: %d)\n", server_pid); | |
| 319 | |
| 320 usleep(100000); | |
| 321 int status; | |
| 322 pid_t result = waitpid(server_pid, &status, WNOHANG); | |
| 323 if (result != 0) | |
| 324 { | |
| 325 if (WIFEXITED(status)) | |
| 326 { | |
| 327 fprintf(stderr, "Server exited immediately with code: %d\n", WEXITSTATUS(status)); | |
| 328 } | |
| 329 else if (WIFSIGNALED(status)) | |
| 330 { | |
| 331 fprintf(stderr, "Server was killed by signal: %d\n", WTERMSIG(status)); | |
| 332 } | |
| 333 return -1; | |
| 334 } | |
| 335 | |
| 336 sleep(2); | |
| 337 printf("Server ready\n\n"); | |
| 338 | |
| 339 return server_pid; | |
| 340 } | |
| 341 | |
| 342 // Helper: Stop test server | |
| 343 void stop_test_server(pid_t server_pid) | |
| 344 { | |
| 345 if (server_pid > 0) | |
| 346 { | |
| 347 printf("\nStopping server (PID: %d)...\n", server_pid); | |
| 348 kill(server_pid, SIGTERM); | |
| 349 waitpid(server_pid, NULL, 0); | |
| 350 printf("Server stopped\n"); | |
| 351 } | |
| 352 } | |
| 353 | |
| 354 // Helper: Initialize test case with snapshot file | 332 // Helper: Initialize test case with snapshot file |
| 355 void init_test_case(TestCase *test) | 333 void init_test_case(TestCase *test) |
| 356 { | 334 { |
| 357 char filename[256]; | 335 char filename[256]; |
| 358 path_to_filename(test->path, filename, sizeof(filename)); | 336 path_to_filename(test->path, filename, sizeof(filename)); |
| 411 {"/", 200, NULL, NULL, NULL, 0}, | 389 {"/", 200, NULL, NULL, NULL, 0}, |
| 412 {"/resume", 200, NULL, NULL, NULL, 0}, | 390 {"/resume", 200, NULL, NULL, NULL, 0}, |
| 413 {"/tools", 200, NULL, NULL, NULL, 0}, | 391 {"/tools", 200, NULL, NULL, NULL, 0}, |
| 414 {"/tools/markdown_to_html", 200, NULL, NULL, NULL, 0}, | 392 {"/tools/markdown_to_html", 200, NULL, NULL, NULL, 0}, |
| 415 {"/tools/file_converter", 200, NULL, NULL, NULL, 0}, | 393 {"/tools/file_converter", 200, NULL, NULL, NULL, 0}, |
| 394 {"/talk", 200, NULL, NULL, NULL, 0}, | |
| 416 }; | 395 }; |
| 417 int num_success_tests = sizeof(success_tests) / sizeof(success_tests[0]); | 396 int num_success_tests = sizeof(success_tests) / sizeof(success_tests[0]); |
| 418 | 397 |
| 419 TestCase redirect_tests[] = { | 398 TestCase redirect_tests[] = { |
| 420 {"/index.html", 301, NULL, NULL, NULL, 0}, | 399 {"/index.html", 301, NULL, NULL, NULL, 0}, |