format_request_test.cc
Go to the documentation of this file.
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 
20 
21 #include <string.h>
22 
23 #include <grpc/grpc.h>
24 #include <grpc/support/log.h>
25 
28 
29 static void test_format_get_request(void) {
30  grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
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 
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 
51 }
52 
53 static void test_format_post_request(void) {
54  grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
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 
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 
80 }
81 
83  grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
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 
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 
104 }
105 
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 
125  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 "\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 
137 }
138 
139 int main(int argc, char** argv) {
140  grpc::testing::TestEnvironment env(&argc, argv);
141  grpc_init();
142 
147 
148  grpc_shutdown();
149  return 0;
150 }
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
format_request.h
log.h
generate.env
env
Definition: generate.py:37
memset
return memset(p, 0, total)
string.h
fix_build_deps.hdrs
hdrs
Definition: fix_build_deps.py:402
test_format_post_request_no_body
static void test_format_post_request_no_body(void)
Definition: format_request_test.cc:82
main
int main(int argc, char **argv)
Definition: format_request_test.cc:139
test_format_post_request
static void test_format_post_request(void)
Definition: format_request_test.cc:53
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_http_header
Definition: src/core/lib/http/parser.h:36
grpc_httpcli_format_get_request
grpc_slice grpc_httpcli_format_get_request(const grpc_http_request *request, const char *host, const char *path)
Definition: format_request.cc:59
req
static uv_connect_t req
Definition: test-connection-fail.c:30
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
httpcli.h
grpc.h
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
test_config.h
GRPC_HTTPCLI_USER_AGENT
#define GRPC_HTTPCLI_USER_AGENT
Definition: httpcli.h:57
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
grpc_httpcli_format_post_request
grpc_slice grpc_httpcli_format_post_request(const grpc_http_request *request, const char *host, const char *path)
Definition: format_request.cc:69
test_format_post_request_content_type_override
static void test_format_post_request_content_type_override(void)
Definition: format_request_test.cc:106
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_slice_str_cmp
GPRAPI int grpc_slice_str_cmp(grpc_slice a, const char *b)
Definition: slice/slice.cc:426
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
test_format_get_request
static void test_format_get_request(void)
Definition: format_request_test.cc:29
grpc_http_request
Definition: src/core/lib/http/parser.h:69


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:22