1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/util/http_client/format_request.h"
20
21 #include <string.h>
22
23 #include <memory>
24
25 #include "gtest/gtest.h"
26 #include "src/core/lib/slice/slice_internal.h"
27 #include "src/core/util/http_client/httpcli.h"
28 #include "test/core/test_util/test_config.h"
29
TEST(FormatRequestTest,FormatGetRequest)30 TEST(FormatRequestTest, FormatGetRequest) {
31 grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
32 grpc_http_request req;
33 grpc_slice slice;
34
35 const char* host = "example.com";
36 memset(&req, 0, sizeof(req));
37 req.hdr_count = 1;
38 req.hdrs = &hdr;
39
40 slice = grpc_httpcli_format_get_request(&req, host, "/index.html");
41
42 ASSERT_EQ(grpc_core::StringViewFromSlice(slice),
43 "GET /index.html HTTP/1.1\r\n"
44 "Host: example.com\r\n"
45 "Connection: close\r\n"
46 "User-Agent: " GRPC_HTTPCLI_USER_AGENT
47 "\r\n"
48 "x-yz: abc\r\n"
49 "\r\n");
50
51 grpc_slice_unref(slice);
52 }
53
TEST(FormatRequestTest,FormatPostRequest)54 TEST(FormatRequestTest, FormatPostRequest) {
55 grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
56 grpc_http_request req;
57 grpc_slice slice;
58
59 const char* host = "example.com";
60 memset(&req, 0, sizeof(req));
61 req.hdr_count = 1;
62 req.hdrs = &hdr;
63 req.body = const_cast<char*>("fake body");
64 req.body_length = 9;
65
66 slice = grpc_httpcli_format_post_request(&req, host, "/index.html");
67
68 ASSERT_EQ(grpc_core::StringViewFromSlice(slice),
69 "POST /index.html HTTP/1.1\r\n"
70 "Host: example.com\r\n"
71 "Connection: close\r\n"
72 "User-Agent: " GRPC_HTTPCLI_USER_AGENT
73 "\r\n"
74 "x-yz: abc\r\n"
75 "Content-Type: text/plain\r\n"
76 "Content-Length: 9\r\n"
77 "\r\n"
78 "fake body");
79
80 grpc_slice_unref(slice);
81 }
82
TEST(FormatRequestTest,FormatPostRequestNoBody)83 TEST(FormatRequestTest, FormatPostRequestNoBody) {
84 grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
85 grpc_http_request req;
86 grpc_slice slice;
87
88 const char* host = "example.com";
89 memset(&req, 0, sizeof(req));
90 req.hdr_count = 1;
91 req.hdrs = &hdr;
92
93 slice = grpc_httpcli_format_post_request(&req, host, "/index.html");
94
95 ASSERT_EQ(grpc_core::StringViewFromSlice(slice),
96 "POST /index.html HTTP/1.1\r\n"
97 "Host: example.com\r\n"
98 "Connection: close\r\n"
99 "User-Agent: " GRPC_HTTPCLI_USER_AGENT
100 "\r\n"
101 "x-yz: abc\r\n"
102 "\r\n");
103
104 grpc_slice_unref(slice);
105 }
106
TEST(FormatRequestTest,FormatPostRequestContentTypeOverride)107 TEST(FormatRequestTest, FormatPostRequestContentTypeOverride) {
108 grpc_http_header hdrs[2];
109 grpc_http_request req;
110 grpc_slice slice;
111
112 const char* host = "example.com";
113 hdrs[0].key = const_cast<char*>("x-yz");
114 hdrs[0].value = const_cast<char*>("abc");
115 hdrs[1].key = const_cast<char*>("Content-Type");
116 hdrs[1].value = const_cast<char*>("application/x-www-form-urlencoded");
117 memset(&req, 0, sizeof(req));
118 req.hdr_count = 2;
119 req.hdrs = hdrs;
120 req.body = const_cast<char*>("fake%20body");
121 req.body_length = 11;
122
123 slice = grpc_httpcli_format_post_request(&req, host, "/index.html");
124
125 ASSERT_EQ(grpc_core::StringViewFromSlice(slice),
126 "POST /index.html HTTP/1.1\r\n"
127 "Host: example.com\r\n"
128 "Connection: close\r\n"
129 "User-Agent: " GRPC_HTTPCLI_USER_AGENT
130 "\r\n"
131 "x-yz: abc\r\n"
132 "Content-Type: application/x-www-form-urlencoded\r\n"
133 "Content-Length: 11\r\n"
134 "\r\n"
135 "fake%20body");
136
137 grpc_slice_unref(slice);
138 }
139
140 // scope
141
main(int argc,char ** argv)142 int main(int argc, char** argv) {
143 grpc::testing::TestEnvironment env(&argc, argv);
144 ::testing::InitGoogleTest(&argc, argv);
145 grpc::testing::TestGrpcScope grpc_scope;
146 return RUN_ALL_TESTS();
147 }
148