2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2016 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "shrpx_http_test.h"
29 #endif // HAVE_UNISTD_H
33 #include <CUnit/CUnit.h>
35 #include "shrpx_http.h"
36 #include "shrpx_config.h"
37 #include "shrpx_log.h"
41 void test_shrpx_http_create_forwarded(void) {
42 BlockAllocator balloc(1024, 1024);
44 CU_ASSERT("by=\"example.com:3000\";for=\"[::1]\";host=\"www.example.com\";"
46 http::create_forwarded(balloc,
47 FORWARDED_BY | FORWARDED_FOR |
48 FORWARDED_HOST | FORWARDED_PROTO,
49 StringRef::from_lit("example.com:3000"),
50 StringRef::from_lit("[::1]"),
51 StringRef::from_lit("www.example.com"),
52 StringRef::from_lit("https")));
54 CU_ASSERT("for=192.168.0.1" ==
55 http::create_forwarded(
56 balloc, FORWARDED_FOR, StringRef::from_lit("alpha"),
57 StringRef::from_lit("192.168.0.1"),
58 StringRef::from_lit("bravo"), StringRef::from_lit("charlie")));
60 CU_ASSERT("by=_hidden;for=\"[::1]\"" ==
61 http::create_forwarded(
62 balloc, FORWARDED_BY | FORWARDED_FOR,
63 StringRef::from_lit("_hidden"), StringRef::from_lit("[::1]"),
64 StringRef::from_lit(""), StringRef::from_lit("")));
66 CU_ASSERT("by=\"[::1]\";for=_hidden" ==
67 http::create_forwarded(
68 balloc, FORWARDED_BY | FORWARDED_FOR,
69 StringRef::from_lit("[::1]"), StringRef::from_lit("_hidden"),
70 StringRef::from_lit(""), StringRef::from_lit("")));
73 http::create_forwarded(
75 FORWARDED_BY | FORWARDED_FOR | FORWARDED_HOST | FORWARDED_PROTO,
76 StringRef::from_lit(""), StringRef::from_lit(""),
77 StringRef::from_lit(""), StringRef::from_lit("")));
80 void test_shrpx_http_create_via_header_value(void) {
81 std::array<char, 16> buf;
83 auto end = http::create_via_header_value(std::begin(buf), 1, 1);
85 CU_ASSERT(("1.1 nghttpx" == StringRef{std::begin(buf), end}));
87 std::fill(std::begin(buf), std::end(buf), '\0');
89 end = http::create_via_header_value(std::begin(buf), 2, 0);
91 CU_ASSERT(("2 nghttpx" == StringRef{std::begin(buf), end}));
94 void test_shrpx_http_create_affinity_cookie(void) {
95 BlockAllocator balloc(1024, 1024);
98 c = http::create_affinity_cookie(balloc, StringRef::from_lit("cookie-val"),
99 0xf1e2d3c4u, StringRef{}, false);
101 CU_ASSERT("cookie-val=f1e2d3c4" == c);
103 c = http::create_affinity_cookie(balloc, StringRef::from_lit("alpha"),
104 0x00000000u, StringRef{}, true);
106 CU_ASSERT("alpha=00000000; Secure" == c);
108 c = http::create_affinity_cookie(balloc, StringRef::from_lit("bravo"),
109 0x01111111u, StringRef::from_lit("bar"),
112 CU_ASSERT("bravo=01111111; Path=bar" == c);
114 c = http::create_affinity_cookie(balloc, StringRef::from_lit("charlie"),
115 0x01111111u, StringRef::from_lit("bar"),
118 CU_ASSERT("charlie=01111111; Path=bar; Secure" == c);