File: http.c

package info (click to toggle)
chiaki 2.2.0-1.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 5,312 kB
  • sloc: ansic: 17,616; cpp: 7,901; xml: 2,691; sh: 395; python: 96; makefile: 6
file content (78 lines) | stat: -rw-r--r-- 2,052 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// SPDX-License-Identifier: LicenseRef-AGPL-3.0-only-OpenSSL

#include <munit.h>

#include <chiaki/http.h>
#include <stdio.h>

static char * const response_crlf =
		"HTTP/1.1 200 OK\r\n"
		"Content-type: text/html, text, plain\r\n"
		"Ultimate Ability: Gamer\r\n"
		"\r\n";

static char * const response_lf =
		"HTTP/1.1 200 Ok\n"
		"Content-type: text/html, text, plain\n"
		"Ultimate Ability:Gamer\n";

static void *test_http_response_parse_setup(const MunitParameter params[], void *user)
{
	const char *response = NULL;
	if(strcmp(params[0].value, "crlf") == 0)
		response = response_crlf;
	else if(strcmp(params[0].value, "lf") == 0)
		response = response_lf;
	return response ? strdup(response) : NULL;
}

static void test_http_response_parse_teardown(void *fixture)
{
	free(fixture);
}

static MunitResult test_http_response_parse(const MunitParameter params[], void *fixture)
{
	char *buf = fixture;
	ChiakiHttpResponse parsed_response;
	ChiakiErrorCode err = chiaki_http_response_parse(&parsed_response, buf, strlen(buf));
	munit_assert_int(err, ==, CHIAKI_ERR_SUCCESS);
	munit_assert_int(parsed_response.code, ==, 200);

	ChiakiHttpHeader *header = parsed_response.headers;
	munit_assert_ptr_not_null(header);
	munit_assert_string_equal(header->key, "Ultimate Ability");
	munit_assert_string_equal(header->value, "Gamer");

	header = header->next;
	munit_assert_ptr_not_null(header);
	munit_assert_string_equal(header->key, "Content-type");
	munit_assert_string_equal(header->value, "text/html, text, plain");

	header = header->next;
	munit_assert_ptr_null(header);

	chiaki_http_response_fini(&parsed_response);
	return MUNIT_OK;
}

static char *response_params[] = {
		"crlf", "lf", NULL
};

static MunitParameterEnum params[] = {
		{ "line ending", response_params },
		{ NULL, NULL }
};

MunitTest tests_http[] = {
	{
		"/response_parse",
		test_http_response_parse,
		test_http_response_parse_setup,
		test_http_response_parse_teardown,
		MUNIT_TEST_OPTION_NONE,
		params
	},
	{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};