Debian Codegraph @

test/core/http/format_request_test.cc

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