vessel/tests/path.c
2025-06-13 17:58:13 +03:00

104 lines
2.9 KiB
C

#include <stdio.h>
#include <string.h>
#include <vessel/path.h>
#include <vessel/main.h>
#define Err(...) \
{ \
fprintf(stderr, __VA_ARGS__); \
return 1; \
}
int main(void) {
Path p = { 0 };
uint64_t idx = 0;
const char *expected[][2] = {
{ "meow", NULL },
{ "test", NULL },
{ "a", "hello" },
{ "b", " " },
{ "c", "world/test" },
{ "x", "1 2 3" },
{ "y", NULL },
{ "z", "encoded value with spaces" },
{ "param1", "value1" },
{ "param2", NULL },
{ "param3", "value3 with spaces" },
{ "param4", "#hash tag" },
{ "param5", "{json: \"value\"}" },
{ "empty1", NULL },
{ "empty2", NULL },
{ "anotherEmpty", NULL },
{ "deeply/nested/path/?query", "value" },
{ "anotherParam", "/leadingSlash" },
{ "trailingSlashParam", "value/" },
{ "one", "1" },
{ "hello", "w0rld?/%" },
{ "last-empty", NULL },
{ 0 },
};
if (!Path_init(&p)) {
perror("Failed to initialise Path");
return 1;
}
if (!Path_parse(&p,
"/hello//world//nested//path//"
"level%201%25?meow&test=&a=hello&b=%20&c=world%2Ftest&"
"x=1%202%203&y=&z="
"encoded%20value%20with%20spaces&param1=value1&param2=&"
"param3=value3%"
"20with%20spaces&param4=%23hash%20tag&param5=%7Bjson%"
"3A%20%22value%22%"
"7D&empty1=&empty2=&&anotherEmpty=&deeply/nested/path/"
"?query=value&anotherParam=%2FleadingSlash&"
"trailingSlashParam=value/&one=1&=&hello=w0rld?/%&last-empty")) {
perror("Failed to parse Path");
Path_destroy(&p);
return 1;
}
puts("Testing path parser...");
if (strcmp(p.path, "/hello/world/nested/path/level 1%") != 0) {
fprintf(stderr, "Parsed invalid path: %s\n", p.path);
Path_destroy(&p);
return 1;
}
while (expected[idx][0]) {
HMapBucket *b;
printf("Testing: \"%s\" == \"%s\"\n",
expected[idx][0],
expected[idx][1] ? expected[idx][1] : "");
b = HMap_find(&p.args, expected[idx][0]);
if (!b) {
fprintf(stderr, "Could not find key: %s\n", expected[idx][0]);
Path_destroy(&p);
return 1;
}
if (expected[idx][1] ? (strcmp(b->value, expected[idx][1]) != 0) : b->value != NULL) {
fprintf(stderr, "Invalid value: %s\n", (char *)b->value);
Path_destroy(&p);
return 1;
}
++idx;
}
if (!Path_destroy(&p)) {
fputs("Failed to destroy path.\n", stderr);
return 1;
}
puts("All tests passed successfully.");
return 0;
}