Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / shrpx_downstream_test.cc
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2013 Tatsuhiro Tsujikawa
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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.
24  */
25 #include "shrpx_downstream_test.h"
26
27 #include <iostream>
28
29 #include <CUnit/CUnit.h>
30
31 #include "shrpx_downstream.h"
32
33 namespace shrpx {
34
35 void test_downstream_index_request_headers(void) {
36   Downstream d(nullptr, nullptr, 0, 0);
37   d.add_request_header("1", "0");
38   d.add_request_header("2", "1");
39   d.add_request_header("Charlie", "2");
40   d.add_request_header("Alpha", "3");
41   d.add_request_header("Delta", "4");
42   d.add_request_header("BravO", "5");
43   d.add_request_header(":method", "6");
44   d.add_request_header(":authority", "7");
45   d.index_request_headers();
46
47   auto ans = Headers{{"1", "0"},
48                      {"2", "1"},
49                      {"charlie", "2"},
50                      {"alpha", "3"},
51                      {"delta", "4"},
52                      {"bravo", "5"},
53                      {":method", "6"},
54                      {":authority", "7"}};
55   CU_ASSERT(ans == d.get_request_headers());
56 }
57
58 void test_downstream_index_response_headers(void) {
59   Downstream d(nullptr, nullptr, 0, 0);
60   d.add_response_header("Charlie", "0");
61   d.add_response_header("Alpha", "1");
62   d.add_response_header("Delta", "2");
63   d.add_response_header("BravO", "3");
64   d.index_response_headers();
65
66   auto ans =
67       Headers{{"charlie", "0"}, {"alpha", "1"}, {"delta", "2"}, {"bravo", "3"}};
68   CU_ASSERT(ans == d.get_response_headers());
69 }
70
71 void test_downstream_get_request_header(void) {
72   Downstream d(nullptr, nullptr, 0, 0);
73   d.add_request_header("alpha", "0");
74   d.add_request_header(":authority", "1");
75   d.add_request_header("content-length", "2");
76   d.index_request_headers();
77
78   // By token
79   CU_ASSERT(Header(":authority", "1") ==
80             *d.get_request_header(http2::HD__AUTHORITY));
81   CU_ASSERT(nullptr == d.get_request_header(http2::HD__METHOD));
82
83   // By name
84   CU_ASSERT(Header("alpha", "0") == *d.get_request_header("alpha"));
85   CU_ASSERT(nullptr == d.get_request_header("bravo"));
86 }
87
88 void test_downstream_get_response_header(void) {
89   Downstream d(nullptr, nullptr, 0, 0);
90   d.add_response_header("alpha", "0");
91   d.add_response_header(":status", "1");
92   d.add_response_header("content-length", "2");
93   d.index_response_headers();
94
95   // By token
96   CU_ASSERT(Header(":status", "1") ==
97             *d.get_response_header(http2::HD__STATUS));
98   CU_ASSERT(nullptr == d.get_response_header(http2::HD__METHOD));
99 }
100
101 void test_downstream_crumble_request_cookie(void) {
102   Downstream d(nullptr, nullptr, 0, 0);
103   d.add_request_header(":method", "get");
104   d.add_request_header(":path", "/");
105   auto val = "alpha; bravo; ; ;; charlie;;";
106   d.add_request_header(
107       reinterpret_cast<const uint8_t *>("cookie"), sizeof("cookie") - 1,
108       reinterpret_cast<const uint8_t *>(val), strlen(val), true, -1);
109   d.add_request_header("cookie", ";delta");
110   d.add_request_header("cookie", "echo");
111   auto cookies = d.crumble_request_cookie();
112
113   Headers ans = {{"cookie", "alpha"},
114                  {"cookie", "bravo"},
115                  {"cookie", "charlie"},
116                  {"cookie", "delta"},
117                  {"cookie", "echo"}};
118   CU_ASSERT(ans == cookies);
119   CU_ASSERT(cookies[0].no_index);
120   CU_ASSERT(cookies[1].no_index);
121   CU_ASSERT(cookies[2].no_index);
122 }
123
124 void test_downstream_assemble_request_cookie(void) {
125   Downstream d(nullptr, nullptr, 0, 0);
126   d.add_request_header(":method", "get");
127   d.add_request_header(":path", "/");
128   d.add_request_header("cookie", "alpha");
129   d.add_request_header("cookie", "bravo;");
130   d.add_request_header("cookie", "charlie; ");
131   d.add_request_header("cookie", "delta;;");
132   d.assemble_request_cookie();
133   CU_ASSERT("alpha; bravo; charlie; delta" == d.get_assembled_request_cookie());
134 }
135
136 void test_downstream_rewrite_location_response_header(void) {
137   {
138     Downstream d(nullptr, nullptr, 0, 0);
139     d.set_request_downstream_host("localhost:3000");
140     d.add_request_header("host", "localhost");
141     d.add_response_header("location", "http://localhost:3000/");
142     d.index_request_headers();
143     d.index_response_headers();
144     d.rewrite_location_response_header("https");
145     auto location = d.get_response_header(http2::HD_LOCATION);
146     CU_ASSERT("https://localhost/" == (*location).value);
147   }
148   {
149     Downstream d(nullptr, nullptr, 0, 0);
150     d.set_request_downstream_host("localhost");
151     d.set_request_http2_authority("localhost");
152     d.add_response_header("location", "http://localhost:3000/");
153     d.index_response_headers();
154     d.rewrite_location_response_header("https");
155     auto location = d.get_response_header(http2::HD_LOCATION);
156     CU_ASSERT("https://localhost/" == (*location).value);
157   }
158 }
159
160 } // namespace shrpx