Mercurial
comparison seobeo/tests/seobeo_response_test.c @ 226:3fa4bf481f42
[merge] Merge hg-web into default
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Sun, 02 Aug 2026 14:42:01 -0700 |
| parents | 0e7b9464248d |
| children |
comparison
equal
deleted
inserted
replaced
| 220:eb8b4230fdb9 | 226:3fa4bf481f42 |
|---|---|
| 1 #include "seobeo/seobeo.h" | |
| 2 | |
| 3 #include <stdio.h> | |
| 4 #include <stdlib.h> | |
| 5 #include <string.h> | |
| 6 #include <sys/socket.h> | |
| 7 | |
| 8 int main(void) | |
| 9 { | |
| 10 int sockets[2]; | |
| 11 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) != 0) | |
| 12 return 1; | |
| 13 | |
| 14 Seobeo_Handle handle = {0}; | |
| 15 handle.socket = sockets[0]; | |
| 16 handle.write_buffer_capacity = 4096; | |
| 17 handle.write_buffer = malloc(handle.write_buffer_capacity); | |
| 18 | |
| 19 Dowa_Arena *arena = Dowa_Arena_Create(4096); | |
| 20 Seobeo_Request_Entry *response = NULL; | |
| 21 Dowa_HashMap_Push_Arena(response, "status", "200", arena); | |
| 22 Dowa_HashMap_Push_Arena(response, "content-type", "text/plain", arena); | |
| 23 Dowa_HashMap_Push_Arena(response, "body", "hello", arena); | |
| 24 Dowa_HashMap_Push_Arena(response, "X-Test", "present", arena); | |
| 25 Dowa_HashMap_Push_Arena(response, "X-Unsafe", "bad\r\nInjected: yes", arena); | |
| 26 | |
| 27 Seobeo_Router_Send_Response(&handle, response, arena); | |
| 28 | |
| 29 char received[4096] = {0}; | |
| 30 ssize_t length = read(sockets[1], received, sizeof(received) - 1); | |
| 31 int failed = 0; | |
| 32 if (length <= 0) | |
| 33 failed = 1; | |
| 34 if (!strstr(received, "Content-Length: 5\r\n")) | |
| 35 failed = 1; | |
| 36 if (!strstr(received, "X-Test: present\r\n\r\nhello")) | |
| 37 failed = 1; | |
| 38 if (strstr(received, "Injected: yes")) | |
| 39 failed = 1; | |
| 40 | |
| 41 if (failed) | |
| 42 fprintf(stderr, "Unexpected response:\n%s\n", received); | |
| 43 | |
| 44 close(sockets[0]); | |
| 45 close(sockets[1]); | |
| 46 free(handle.write_buffer); | |
| 47 Dowa_Arena_Free(arena); | |
| 48 | |
| 49 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) != 0) | |
| 50 return 1; | |
| 51 | |
| 52 memset(&handle, 0, sizeof(handle)); | |
| 53 handle.socket = sockets[0]; | |
| 54 handle.write_buffer_capacity = 4096; | |
| 55 handle.write_buffer = malloc(handle.write_buffer_capacity); | |
| 56 arena = Dowa_Arena_Create(4096); | |
| 57 response = NULL; | |
| 58 Dowa_HashMap_Push_Arena(response, "status", "204", arena); | |
| 59 Dowa_HashMap_Push_Arena(response, "content-type", "text/plain", arena); | |
| 60 Dowa_HashMap_Push_Arena(response, "body", "", arena); | |
| 61 Seobeo_Router_Send_Response(&handle, response, arena); | |
| 62 | |
| 63 memset(received, 0, sizeof(received)); | |
| 64 length = read(sockets[1], received, sizeof(received) - 1); | |
| 65 if (length <= 0 || | |
| 66 !strstr(received, "HTTP/1.1 204 No Content\r\n") || | |
| 67 !strstr(received, "Content-Length: 0\r\n") || | |
| 68 !strstr(received, "\r\n\r\n")) | |
| 69 failed = 1; | |
| 70 | |
| 71 if (failed) | |
| 72 fprintf(stderr, "Unexpected no-content response:\n%s\n", received); | |
| 73 | |
| 74 close(sockets[0]); | |
| 75 close(sockets[1]); | |
| 76 free(handle.write_buffer); | |
| 77 Dowa_Arena_Free(arena); | |
| 78 return failed; | |
| 79 } |