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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
* Copyright (c) 2015 DeNA Co., Ltd., Kazuho Oku
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "../../test.h"
#include "../../../../lib/handler/headers.c"
static int headers_are(h2o_mem_pool_t *pool, h2o_headers_t *headers, const char *s, size_t len)
{
size_t i;
h2o_iovec_t flattened = {NULL};
for (i = 0; i != headers->size; ++i) {
flattened = h2o_concat(pool, flattened, *headers->entries[i].name, h2o_iovec_init(H2O_STRLIT(": ")),
headers->entries[i].value, h2o_iovec_init(H2O_STRLIT("\n")));
}
return h2o_memis(flattened.base, flattened.len, s, len);
}
static void setup_headers(h2o_mem_pool_t *pool, h2o_headers_t *headers)
{
*headers = (h2o_headers_t){NULL};
h2o_add_header(pool, headers, H2O_TOKEN_CONTENT_TYPE, NULL, H2O_STRLIT("text/plain"));
h2o_add_header(pool, headers, H2O_TOKEN_CACHE_CONTROL, NULL, H2O_STRLIT("public, max-age=86400"));
h2o_add_header(pool, headers, H2O_TOKEN_SET_COOKIE, NULL, H2O_STRLIT("a=b"));
h2o_add_header_by_str(pool, headers, H2O_STRLIT("x-foo"), 0, NULL, H2O_STRLIT("bar"));
}
void test_lib__handler__headers_c(void)
{
h2o_mem_pool_t pool;
h2o_headers_t headers;
h2o_headers_command_t cmd;
h2o_iovec_t header_str;
h2o_mem_init_pool(&pool);
/* tests using token headers */
setup_headers(&pool, &headers);
ok(headers_are(&pool, &headers,
H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\n")));
cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_ADD, &H2O_TOKEN_SET_COOKIE->buf, {H2O_STRLIT("c=d")}};
h2o_rewrite_headers(&pool, &headers, &cmd);
ok(headers_are(
&pool, &headers,
H2O_STRLIT(
"content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\nset-cookie: c=d\n")));
setup_headers(&pool, &headers);
cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_APPEND, &H2O_TOKEN_CACHE_CONTROL->buf, {H2O_STRLIT("public")}};
h2o_rewrite_headers(&pool, &headers, &cmd);
ok(headers_are(
&pool, &headers,
H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400, public\nset-cookie: a=b\nx-foo: bar\n")));
setup_headers(&pool, &headers);
cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_MERGE, &H2O_TOKEN_CACHE_CONTROL->buf, {H2O_STRLIT("public")}};
h2o_rewrite_headers(&pool, &headers, &cmd);
ok(headers_are(&pool, &headers,
H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\n")));
setup_headers(&pool, &headers);
cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_SET, &H2O_TOKEN_CACHE_CONTROL->buf, {H2O_STRLIT("no-cache")}};
h2o_rewrite_headers(&pool, &headers, &cmd);
ok(headers_are(&pool, &headers,
H2O_STRLIT("content-type: text/plain\nset-cookie: a=b\nx-foo: bar\ncache-control: no-cache\n")));
setup_headers(&pool, &headers);
cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_SETIFEMPTY, &H2O_TOKEN_CACHE_CONTROL->buf, {H2O_STRLIT("no-cache")}};
h2o_rewrite_headers(&pool, &headers, &cmd);
ok(headers_are(&pool, &headers,
H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\n")));
/* tests using non-token headers */
header_str = h2o_iovec_init(H2O_STRLIT("x-foo"));
setup_headers(&pool, &headers);
cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_ADD, &header_str, {H2O_STRLIT("baz")}};
h2o_rewrite_headers(&pool, &headers, &cmd);
ok(headers_are(
&pool, &headers,
H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\nx-foo: baz\n")));
setup_headers(&pool, &headers);
cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_APPEND, &header_str, {H2O_STRLIT("bar")}};
h2o_rewrite_headers(&pool, &headers, &cmd);
ok(headers_are(
&pool, &headers,
H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar, bar\n")));
setup_headers(&pool, &headers);
cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_MERGE, &header_str, {H2O_STRLIT("bar")}};
h2o_rewrite_headers(&pool, &headers, &cmd);
ok(headers_are(&pool, &headers,
H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\n")));
setup_headers(&pool, &headers);
cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_SET, &header_str, {H2O_STRLIT("baz")}};
h2o_rewrite_headers(&pool, &headers, &cmd);
ok(headers_are(&pool, &headers,
H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: baz\n")));
setup_headers(&pool, &headers);
cmd = (h2o_headers_command_t){H2O_HEADERS_CMD_SETIFEMPTY, &header_str, {H2O_STRLIT("baz")}};
h2o_rewrite_headers(&pool, &headers, &cmd);
ok(headers_are(&pool, &headers,
H2O_STRLIT("content-type: text/plain\ncache-control: public, max-age=86400\nset-cookie: a=b\nx-foo: bar\n")));
h2o_mem_clear_pool(&pool);
}
|