xref: /aosp_15_r20/external/grpc-grpc/test/core/http/format_request_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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/lib/http/format_request.h"
20 
21 #include <string.h>
22 
23 #include <memory>
24 
25 #include "gtest/gtest.h"
26 
27 #include "src/core/lib/http/httpcli.h"
28 #include "src/core/lib/slice/slice_internal.h"
29 #include "test/core/util/test_config.h"
30 
TEST(FormatRequestTest,FormatGetRequest)31 TEST(FormatRequestTest, FormatGetRequest) {
32   grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
33   grpc_http_request req;
34   grpc_slice slice;
35 
36   const char* host = "example.com";
37   memset(&req, 0, sizeof(req));
38   req.hdr_count = 1;
39   req.hdrs = &hdr;
40 
41   slice = grpc_httpcli_format_get_request(&req, host, "/index.html");
42 
43   ASSERT_EQ(grpc_core::StringViewFromSlice(slice),
44             "GET /index.html HTTP/1.1\r\n"
45             "Host: example.com\r\n"
46             "Connection: close\r\n"
47             "User-Agent: " GRPC_HTTPCLI_USER_AGENT
48             "\r\n"
49             "x-yz: abc\r\n"
50             "\r\n");
51 
52   grpc_slice_unref(slice);
53 }
54 
TEST(FormatRequestTest,FormatPostRequest)55 TEST(FormatRequestTest, FormatPostRequest) {
56   grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
57   grpc_http_request req;
58   grpc_slice slice;
59 
60   const char* host = "example.com";
61   memset(&req, 0, sizeof(req));
62   req.hdr_count = 1;
63   req.hdrs = &hdr;
64   req.body = const_cast<char*>("fake body");
65   req.body_length = 9;
66 
67   slice = grpc_httpcli_format_post_request(&req, host, "/index.html");
68 
69   ASSERT_EQ(grpc_core::StringViewFromSlice(slice),
70             "POST /index.html HTTP/1.1\r\n"
71             "Host: example.com\r\n"
72             "Connection: close\r\n"
73             "User-Agent: " GRPC_HTTPCLI_USER_AGENT
74             "\r\n"
75             "x-yz: abc\r\n"
76             "Content-Type: text/plain\r\n"
77             "Content-Length: 9\r\n"
78             "\r\n"
79             "fake body");
80 
81   grpc_slice_unref(slice);
82 }
83 
TEST(FormatRequestTest,FormatPostRequestNoBody)84 TEST(FormatRequestTest, FormatPostRequestNoBody) {
85   grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
86   grpc_http_request req;
87   grpc_slice slice;
88 
89   const char* host = "example.com";
90   memset(&req, 0, sizeof(req));
91   req.hdr_count = 1;
92   req.hdrs = &hdr;
93 
94   slice = grpc_httpcli_format_post_request(&req, host, "/index.html");
95 
96   ASSERT_EQ(grpc_core::StringViewFromSlice(slice),
97             "POST /index.html HTTP/1.1\r\n"
98             "Host: example.com\r\n"
99             "Connection: close\r\n"
100             "User-Agent: " GRPC_HTTPCLI_USER_AGENT
101             "\r\n"
102             "x-yz: abc\r\n"
103             "\r\n");
104 
105   grpc_slice_unref(slice);
106 }
107 
TEST(FormatRequestTest,FormatPostRequestContentTypeOverride)108 TEST(FormatRequestTest, FormatPostRequestContentTypeOverride) {
109   grpc_http_header hdrs[2];
110   grpc_http_request req;
111   grpc_slice slice;
112 
113   const char* host = "example.com";
114   hdrs[0].key = const_cast<char*>("x-yz");
115   hdrs[0].value = const_cast<char*>("abc");
116   hdrs[1].key = const_cast<char*>("Content-Type");
117   hdrs[1].value = const_cast<char*>("application/x-www-form-urlencoded");
118   memset(&req, 0, sizeof(req));
119   req.hdr_count = 2;
120   req.hdrs = hdrs;
121   req.body = const_cast<char*>("fake%20body");
122   req.body_length = 11;
123 
124   slice = grpc_httpcli_format_post_request(&req, host, "/index.html");
125 
126   ASSERT_EQ(grpc_core::StringViewFromSlice(slice),
127             "POST /index.html HTTP/1.1\r\n"
128             "Host: example.com\r\n"
129             "Connection: close\r\n"
130             "User-Agent: " GRPC_HTTPCLI_USER_AGENT
131             "\r\n"
132             "x-yz: abc\r\n"
133             "Content-Type: application/x-www-form-urlencoded\r\n"
134             "Content-Length: 11\r\n"
135             "\r\n"
136             "fake%20body");
137 
138   grpc_slice_unref(slice);
139 }
140 
141 // scope
142 
main(int argc,char ** argv)143 int main(int argc, char** argv) {
144   grpc::testing::TestEnvironment env(&argc, argv);
145   ::testing::InitGoogleTest(&argc, argv);
146   grpc::testing::TestGrpcScope grpc_scope;
147   return RUN_ALL_TESTS();
148 }
149