Set up tests for http_template, fixes

This commit is contained in:
Stephen Seo 2024-09-04 15:26:20 +09:00
parent 4d400c43be
commit 2f5ea544e5
4 changed files with 86 additions and 20 deletions

View file

@ -121,8 +121,8 @@ int c_simple_http_required_iter_fn(void *data, void *ud) {
}
typedef struct C_SIMPLE_HTTP_INTERNAL_RequiredCheck {
SDArchiverHashMap *map_of_paths_and_their_vars;
SDArchiverLinkedList *required;
const SDArchiverHashMap *map_of_paths_and_their_vars;
const SDArchiverLinkedList *required;
} C_SIMPLE_HTTP_INTERNAL_RequiredCheck;
int c_simple_http_check_required_iter_fn(void *path_void_str, void *ud) {
@ -150,7 +150,7 @@ int c_simple_http_check_required_iter_fn(void *path_void_str, void *ud) {
C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config(
const char *config_filename,
const char *separating_key,
SDArchiverLinkedList *required_names
const SDArchiverLinkedList *required_names
) {
C_SIMPLE_HTTP_ParsedConfig config;
config.hash_map = NULL;

View file

@ -62,7 +62,7 @@ typedef C_SIMPLE_HTTP_ParsedConfig C_SIMPLE_HTTP_HashMapWrapper;
C_SIMPLE_HTTP_ParsedConfig c_simple_http_parse_config(
const char *config_filename,
const char *separating_key,
SDArchiverLinkedList *required_names
const SDArchiverLinkedList *required_names
);
void c_simple_http_clean_up_parsed_config(C_SIMPLE_HTTP_ParsedConfig *config);

View file

@ -154,9 +154,12 @@ char *c_simple_http_path_to_generated(
last_template_idx = last_node->orig_end_idx;
}
template_node = malloc(sizeof(C_SIMPLE_HTTP_INTERNAL_Template_Node));
template_node->html = malloc(idx - last_template_idx);
memcpy(template_node->html, html_buf + idx, idx - last_template_idx);
template_node->html_size = idx - last_template_idx;
template_node->html_size = idx - last_template_idx - 2;
template_node->html = malloc(template_node->html_size);
memcpy(
template_node->html,
html_buf + last_template_idx,
template_node->html_size);
template_node->orig_end_idx = idx + 1;
template_node->forced_next = NULL;
simple_archiver_list_add(template_html_list, template_node,
@ -176,21 +179,12 @@ char *c_simple_http_path_to_generated(
state &= 0xFFFFFFFE;
C_SIMPLE_HTTP_INTERNAL_Template_Node *last_node =
template_html_list->tail->prev->data;
size_t end_of_var_size =
idx - last_node->orig_end_idx;
if (end_of_var_size <= 6) {
fprintf(
stderr,
"ERROR generating from html template, invalid delimeter at index "
"%lu!\n",
idx);
return NULL;
}
size_t var_size = end_of_var_size - 6;
size_t var_size = idx - 2 - last_node->orig_end_idx;
__attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
char *var = malloc(var_size + 1);
memcpy(
var,
html_buf + last_node->orig_end_idx + 3,
html_buf + last_node->orig_end_idx,
var_size);
var[var_size] = 0;
const char *value_c_str =

View file

@ -1,9 +1,11 @@
// Standard library includes.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Local includes.
#include "config.h"
#include "http_template.h"
// Third party includes.
#include <SimpleArchiver/src/helpers.h>
@ -78,7 +80,7 @@ static int checks_passed = 0;
} while (0);
int main(void) {
// Test set up templates.
// Test config.
{
const char *test_config_filename = "/tmp/c_simple_http_test.config";
__attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
@ -205,6 +207,76 @@ int main(void) {
ASSERT_FALSE(templates.paths);
}
// Test http_template.
{
const char *test_http_template_filename =
"/tmp/c_simple_http_template_test.config";
__attribute__((cleanup(simple_archiver_helper_cleanup_FILE)))
FILE *test_file = fopen(test_http_template_filename, "w");
ASSERT_TRUE(test_file);
ASSERT_TRUE(
fwrite("PATH=/\nHTML=<h1>Test</h1>\n", 1, 26, test_file)
== 26);
simple_archiver_helper_cleanup_FILE(&test_file);
__attribute__((cleanup(simple_archiver_list_free)))
SDArchiverLinkedList *required_names = simple_archiver_list_init();
simple_archiver_list_add(
required_names,
"HTML",
simple_archiver_helper_datastructure_cleanup_nop
);
__attribute__((cleanup(c_simple_http_clean_up_parsed_config)))
C_SIMPLE_HTTP_ParsedConfig config = c_simple_http_parse_config(
test_http_template_filename,
"PATH",
required_names
);
ASSERT_TRUE(config.paths != NULL);
__attribute__((cleanup(simple_archiver_helper_cleanup_c_string)))
char *buf = c_simple_http_path_to_generated("/", &config);
ASSERT_TRUE(buf != NULL);
ASSERT_TRUE(strcmp(buf, "<h1>Test</h1>") == 0);
simple_archiver_helper_cleanup_c_string(&buf);
const char *test_http_template_filename2 =
"/tmp/c_simple_http_template_test2.config";
test_file = fopen(test_http_template_filename2, "w");
ASSERT_TRUE(test_file);
ASSERT_TRUE(
fwrite(
"PATH=/\nHTML=<h1>{{{testVar}}}</h1><br><h2>{{{testVar2}}}</h2>\n",
1,
62,
test_file)
== 62);
ASSERT_TRUE(
fwrite("testVar=''' Some text. '''\n", 1, 27, test_file)
== 27);
ASSERT_TRUE(
fwrite("testVar2=''' More text. '''\n", 1, 28, test_file)
== 28);
simple_archiver_helper_cleanup_FILE(&test_file);
c_simple_http_clean_up_parsed_config(&config);
config = c_simple_http_parse_config(
test_http_template_filename2,
"PATH",
required_names
);
ASSERT_TRUE(config.paths != NULL);
buf = c_simple_http_path_to_generated("/", &config);
ASSERT_TRUE(buf != NULL);
printf("%s\n", buf);
ASSERT_TRUE(strcmp(buf, "<h1> Some text. </h1><br><h2> More text. </h2>") == 0);
simple_archiver_helper_cleanup_c_string(&buf);
}
RETURN()
}