3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "src/core/lib/http/format_request.h"
23 #include <grpc/support/log.h>
24 #include "test/core/util/test_config.h"
26 static void test_format_get_request(void) {
27 grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
28 grpc_httpcli_request req;
31 memset(&req, 0, sizeof(req));
32 req.host = const_cast<char*>("example.com");
33 req.http.path = const_cast<char*>("/index.html");
34 req.http.hdr_count = 1;
37 slice = grpc_httpcli_format_get_request(&req);
39 GPR_ASSERT(0 == grpc_slice_str_cmp(slice,
40 "GET /index.html HTTP/1.0\r\n"
41 "Host: example.com\r\n"
42 "Connection: close\r\n"
43 "User-Agent: " GRPC_HTTPCLI_USER_AGENT
48 grpc_slice_unref(slice);
51 static void test_format_post_request(void) {
52 grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
53 grpc_httpcli_request req;
55 char body_bytes[] = "fake body";
58 memset(&req, 0, sizeof(req));
59 req.host = const_cast<char*>("example.com");
60 req.http.path = const_cast<char*>("/index.html");
61 req.http.hdr_count = 1;
64 slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len);
66 GPR_ASSERT(0 == grpc_slice_str_cmp(slice,
67 "POST /index.html HTTP/1.0\r\n"
68 "Host: example.com\r\n"
69 "Connection: close\r\n"
70 "User-Agent: " GRPC_HTTPCLI_USER_AGENT
73 "Content-Type: text/plain\r\n"
74 "Content-Length: 9\r\n"
78 grpc_slice_unref(slice);
81 static void test_format_post_request_no_body(void) {
82 grpc_http_header hdr = {const_cast<char*>("x-yz"), const_cast<char*>("abc")};
83 grpc_httpcli_request req;
86 memset(&req, 0, sizeof(req));
87 req.host = const_cast<char*>("example.com");
88 req.http.path = const_cast<char*>("/index.html");
89 req.http.hdr_count = 1;
92 slice = grpc_httpcli_format_post_request(&req, nullptr, 0);
94 GPR_ASSERT(0 == grpc_slice_str_cmp(slice,
95 "POST /index.html HTTP/1.0\r\n"
96 "Host: example.com\r\n"
97 "Connection: close\r\n"
98 "User-Agent: " GRPC_HTTPCLI_USER_AGENT
103 grpc_slice_unref(slice);
106 static void test_format_post_request_content_type_override(void) {
107 grpc_http_header hdrs[2];
108 grpc_httpcli_request req;
110 char body_bytes[] = "fake%20body";
111 size_t body_len = 11;
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.host = const_cast<char*>("example.com");
119 req.http.path = const_cast<char*>("/index.html");
120 req.http.hdr_count = 2;
121 req.http.hdrs = hdrs;
123 slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len);
125 GPR_ASSERT(0 == grpc_slice_str_cmp(
127 "POST /index.html HTTP/1.0\r\n"
128 "Host: example.com\r\n"
129 "Connection: close\r\n"
130 "User-Agent: " GRPC_HTTPCLI_USER_AGENT "\r\n"
132 "Content-Type: application/x-www-form-urlencoded\r\n"
133 "Content-Length: 11\r\n"
137 grpc_slice_unref(slice);
140 int main(int argc, char** argv) {
141 grpc::testing::TestEnvironment env(argc, argv);
144 test_format_get_request();
145 test_format_post_request();
146 test_format_post_request_no_body();
147 test_format_post_request_content_type_override();