b0a90c4613b9979218b4db4defd7ef4c45bbfea5
[platform/upstream/grpc.git] / test / core / http / format_request_test.cc
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 <grpc/support/log.h>
24 #include "test/core/util/test_config.h"
25
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;
29   grpc_slice slice;
30
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;
35   req.http.hdrs = &hdr;
36
37   slice = grpc_httpcli_format_get_request(&req);
38
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
44                                      "\r\n"
45                                      "x-yz: abc\r\n"
46                                      "\r\n"));
47
48   grpc_slice_unref(slice);
49 }
50
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;
54   grpc_slice slice;
55   char body_bytes[] = "fake body";
56   size_t body_len = 9;
57
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;
62   req.http.hdrs = &hdr;
63
64   slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len);
65
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
71                                      "\r\n"
72                                      "x-yz: abc\r\n"
73                                      "Content-Type: text/plain\r\n"
74                                      "Content-Length: 9\r\n"
75                                      "\r\n"
76                                      "fake body"));
77
78   grpc_slice_unref(slice);
79 }
80
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;
84   grpc_slice slice;
85
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;
90   req.http.hdrs = &hdr;
91
92   slice = grpc_httpcli_format_post_request(&req, nullptr, 0);
93
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
99                                      "\r\n"
100                                      "x-yz: abc\r\n"
101                                      "\r\n"));
102
103   grpc_slice_unref(slice);
104 }
105
106 static void test_format_post_request_content_type_override(void) {
107   grpc_http_header hdrs[2];
108   grpc_httpcli_request req;
109   grpc_slice slice;
110   char body_bytes[] = "fake%20body";
111   size_t body_len = 11;
112
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;
122
123   slice = grpc_httpcli_format_post_request(&req, body_bytes, body_len);
124
125   GPR_ASSERT(0 == grpc_slice_str_cmp(
126                       slice,
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"
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 int main(int argc, char** argv) {
141   grpc::testing::TestEnvironment env(argc, argv);
142   grpc_init();
143
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();
148
149   grpc_shutdown();
150   return 0;
151 }