1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
|
// Copyright (C) 2016-2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#include <config.h>
#include <http/request.h>
#include <http/date_time.h>
#include <http/http_header.h>
#include <http/http_types.h>
#include <http/tests/request_test.h>
#include <boost/lexical_cast.hpp>
#include <gtest/gtest.h>
#include <utility>
using namespace isc::http;
using namespace isc::http::test;
namespace {
/// @brief Test fixture class for @c HttpRequest class.
class HttpRequestTest : public HttpRequestTestBase<HttpRequest> {
public:
/// @brief Tests connection persistence for the given HTTP version
/// and header value.
///
/// This method creates a dummy HTTP request and sets the specified
/// version and header. Next, it returns the value if @c isPersistent
/// method for this request. The unit test verifies this value for
/// correctness.
///
/// @param http_version HTTP version.
/// @param http_header HTTP header to be included in the request. If
/// the header has an empty value, it is not included.
///
/// @return true if request indicates that connection is to be
/// persistent.
bool isPersistent(const HttpVersion& http_version,
const HttpHeader& http_header = HttpHeader("Connection")) {
try {
// We need to add some JSON body.
std::string json_body = "{ \"param1\": \"foo\" }";
// Set method, path, version and content length.
setContextBasics("POST", "/isc/org", http_version);
addHeaderToContext("Content-Length", json_body.length());
// If additional header has been specified (typically "Connection"),
// include it.
if (!http_header.getValue().empty()) {
addHeaderToContext(http_header.getName(), http_header.getValue());
}
// Attach JSON body.
request_->context()->body_ = json_body;
request_->create();
} catch (...) {
ADD_FAILURE() << "failed to create HTTP request while testing"
" connection persistence";
}
return (request_->isPersistent());
}
};
// This test verifies that a minimal request can be created.
TEST_F(HttpRequestTest, minimal) {
setContextBasics("GET", "/isc/org", HttpVersion(1, 1));
ASSERT_NO_THROW(request_->create());
EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_->getMethod());
EXPECT_EQ("/isc/org", request_->getUri());
EXPECT_EQ(1, request_->getHttpVersion().major_);
EXPECT_EQ(1, request_->getHttpVersion().minor_);
EXPECT_TRUE(request_->getRemote().empty());
request_->setRemote("127.0.0.1");
EXPECT_EQ("127.0.0.1", request_->getRemote());
EXPECT_THROW(request_->getHeaderValue("Content-Length"),
HttpMessageNonExistingHeader);
}
// This test verifies that empty Host header is included in the
// request if it is not explicitly specified.
TEST_F(HttpRequestTest, hostHeaderDefault) {
ASSERT_NO_THROW(request_.reset(new HttpRequest(HttpRequest::Method::HTTP_GET,
"/isc/org",
HttpVersion(1, 0))));
ASSERT_NO_THROW(request_->finalize());
EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_->getMethod());
EXPECT_EQ("/isc/org", request_->getUri());
EXPECT_EQ(1, request_->getHttpVersion().major_);
EXPECT_EQ(0, request_->getHttpVersion().minor_);
std::string host_hdr;
ASSERT_NO_THROW(host_hdr = request_->getHeaderValue("Host"));
EXPECT_TRUE(host_hdr.empty());
}
// This test verifies that it is possible to explicitly specify a
// Host header value while creating a request.
TEST_F(HttpRequestTest, hostHeaderCustom) {
ASSERT_NO_THROW(request_.reset(new HttpRequest(HttpRequest::Method::HTTP_GET,
"/isc/org",
HttpVersion(1, 1),
HostHttpHeader("www.example.org"))));
ASSERT_NO_THROW(request_->finalize());
EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_->getMethod());
EXPECT_EQ("/isc/org", request_->getUri());
EXPECT_EQ(1, request_->getHttpVersion().major_);
EXPECT_EQ(1, request_->getHttpVersion().minor_);
std::string host_hdr;
ASSERT_NO_THROW(host_hdr = request_->getHeaderValue("Host"));
EXPECT_EQ("www.example.org", host_hdr);
}
// This test verifies that headers can be included in a request.
TEST_F(HttpRequestTest, includeHeaders) {
setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", "1024");
addHeaderToContext("Content-Type", "application/json");
ASSERT_NO_THROW(request_->create());
EXPECT_EQ(HttpRequest::Method::HTTP_POST, request_->getMethod());
EXPECT_EQ("/isc/org", request_->getUri());
EXPECT_EQ(1, request_->getHttpVersion().major_);
EXPECT_EQ(0, request_->getHttpVersion().minor_);
std::string content_type;
ASSERT_NO_THROW(content_type = request_->getHeaderValue("Content-Type"));
EXPECT_EQ("application/json", content_type);
uint64_t content_length = 0;
ASSERT_NO_THROW(
content_length = request_->getHeaderValueAsUint64("Content-Length")
);
EXPECT_EQ(1024, content_length);
}
// This test verifies that it is possible to specify required
// methods for the request and that an error is thrown if the
// selected method doesn't match.
TEST_F(HttpRequestTest, requiredMethods) {
request_->requireHttpMethod(HttpRequest::Method::HTTP_GET);
request_->requireHttpMethod(HttpRequest::Method::HTTP_POST);
setContextBasics("GET", "/isc/org", HttpVersion(1, 1));
ASSERT_NO_THROW(request_->create());
request_->context()->method_ = "POST";
ASSERT_NO_THROW(request_->create());
request_->context()->method_ = "PUT";
EXPECT_THROW(request_->create(), HttpRequestError);
}
// This test verifies that it is possible to specify required
// HTTP version for the request and that an error is thrown if
// the selected HTTP version doesn't match.
TEST_F(HttpRequestTest, requiredHttpVersion) {
request_->requireHttpVersion(HttpVersion(1, 0));
request_->requireHttpVersion(HttpVersion(1, 1));
setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
EXPECT_NO_THROW(request_->create());
setContextBasics("POST", "/isc/org", HttpVersion(1, 1));
EXPECT_NO_THROW(request_->create());
setContextBasics("POST", "/isc/org", HttpVersion(2, 0));
EXPECT_THROW(request_->create(), HttpRequestError);
}
// This test verifies that it is possible to specify required
// HTTP headers for the request and that an error is thrown if
// the required header is not included.
TEST_F(HttpRequestTest, requiredHeader) {
request_->requireHeader("Content-Length");
setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
ASSERT_THROW(request_->create(), HttpRequestError);
addHeaderToContext("Content-Type", "application/json");
ASSERT_THROW(request_->create(), HttpRequestError);
addHeaderToContext("Content-Length", "2048");
EXPECT_NO_THROW(request_->create());
}
// This test verifies that it is possible to specify required
// HTTP header value for the request and that an error is thrown
// if the value doesn't match.
TEST_F(HttpRequestTest, requiredHeaderValue) {
request_->requireHeaderValue("Content-Type", "application/json");
setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Type", "text/html");
ASSERT_THROW(request_->create(), HttpRequestError);
addHeaderToContext("Content-Type", "application/json");
EXPECT_NO_THROW(request_->create());
}
// This test verifies that an error is thrown upon an attempt to
// fetch request properties before the request is finalized.
TEST_F(HttpRequestTest, notCreated) {
setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Type", "text/html");
addHeaderToContext("Content-Length", "1024");
EXPECT_THROW(static_cast<void>(request_->getMethod()), HttpMessageError);
EXPECT_THROW(static_cast<void>(request_->getHttpVersion()),
HttpMessageError);
EXPECT_THROW(static_cast<void>(request_->getUri()), HttpMessageError);
EXPECT_THROW(static_cast<void>(request_->getHeaderValue("Content-Type")),
HttpMessageError);
EXPECT_THROW(static_cast<void>(request_->getHeaderValueAsUint64("Content-Length")),
HttpMessageError);
EXPECT_THROW(static_cast<void>(request_->getBody()), HttpMessageError);
ASSERT_NO_THROW(request_->finalize());
EXPECT_NO_THROW(static_cast<void>(request_->getMethod()));
EXPECT_NO_THROW(static_cast<void>(request_->getHttpVersion()));
EXPECT_NO_THROW(static_cast<void>(request_->getUri()));
EXPECT_NO_THROW(static_cast<void>(request_->getHeaderValue("Content-Type")));
EXPECT_NO_THROW(
static_cast<void>(request_->getHeaderValueAsUint64("Content-Length"))
);
EXPECT_NO_THROW(static_cast<void>(request_->getBody()));
}
// This test verifies that it is possible to fetch the request
// body.
TEST_F(HttpRequestTest, getBody) {
std::string json_body = "{ \"param1\": \"foo\" }";
setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", json_body.length());
request_->context()->body_ = json_body;
ASSERT_NO_THROW(request_->finalize());
EXPECT_EQ(json_body, request_->getBody());
}
// This test verifies the behavior of the requiresBody function.
TEST_F(HttpRequestTest, requiresBody) {
ASSERT_FALSE(request_->requiresBody());
request_->requireHeader("Content-Length");
EXPECT_TRUE(request_->requiresBody());
}
// This test verifies that HTTP/1.0 connections are not persistent
// by default.
TEST_F(HttpRequestTest, isPersistentHttp10) {
// In HTTP 1.0 the connection is by default non-persistent.
EXPECT_FALSE(isPersistent(HttpVersion(1, 0)));
}
// This test verifies that HTTP/1.1 connections are persistent
// by default.
TEST_F(HttpRequestTest, isPersistentHttp11) {
// In HTTP 1.1 the connection is by default persistent.
EXPECT_TRUE(isPersistent(HttpVersion(1, 1)));
}
// This test verifies that HTTP/1.0 connection becomes persistent
// when keep-alive value of the Connection header is included.
TEST_F(HttpRequestTest, isPersistentHttp10KeepAlive) {
// In HTTP 1.0 the client indicates that the connection is desired to be
// persistent by including "Connection: keep-alive" header.
EXPECT_TRUE(
isPersistent(HttpVersion(1, 0), HttpHeader("Connection", "Keep-alive"))
);
}
// This test verifies that HTTP/1.1 connection is closed when the
// close value of the Connection header is included.
TEST_F(HttpRequestTest, isPersistentHttp11Close) {
// In HTTP 1.1 the client would include "Connection: close" header if it
// desires to close the connection.
EXPECT_FALSE(
isPersistent(HttpVersion(1, 1), HttpHeader("Connection", "close"))
);
}
// This test verifies the contents of the HTTP outbound request.
TEST_F(HttpRequestTest, clientRequest) {
ASSERT_NO_THROW(
request_.reset(new HttpRequest(HttpRequest::Method::HTTP_POST,
"/isc/org",
HttpVersion(1, 0),
HostHttpHeader("www.example.org")));
);
// Capture current date and time.
HttpDateTime date_time;
// Add headers.
request_->context()->headers_.push_back(HttpHeaderContext("Date", date_time.rfc1123Format()));
request_->context()->headers_.push_back(HttpHeaderContext("Content-Type", "text/html"));
request_->context()->headers_.push_back(HttpHeaderContext("Accept", "text/html"));
// Add a body.
request_->context()->body_ = "<html></html>";
// Commit and validate the data.
ASSERT_NO_THROW(request_->finalize());
// Check that the HTTP request in the textual format is correct. Note that
// it should include "Content-Length", even though we haven't explicitly set
// this header. It is dynamically computed from the body size.
EXPECT_EQ("POST /isc/org HTTP/1.0\r\n"
"Host: www.example.org\r\n"
"Accept: text/html\r\n"
"Content-Length: 13\r\n"
"Content-Type: text/html\r\n"
"Date: " + date_time.rfc1123Format() + "\r\n"
"\r\n"
"<html></html>",
request_->toString());
}
// This test verifies the contents of the HTTP outbound request
// which lacks body.
TEST_F(HttpRequestTest, clientRequestNoBody) {
setContextBasics("GET", "/isc/org", HttpVersion(1, 1));
// Add headers.
request_->context()->headers_.push_back(HttpHeaderContext("Content-Type", "text/html"));
// Commit and validate the data.
ASSERT_NO_THROW(request_->finalize());
// Check that the HTTP request in the textual format is correct. Note that
// there should be no Content-Length included, because the body is empty.
EXPECT_EQ("GET /isc/org HTTP/1.1\r\n"
"Content-Type: text/html\r\n"
"\r\n",
request_->toString());
}
// This test verifies the first line of the HTTP request.
TEST_F(HttpRequestTest, toBriefString) {
// Create the request.
setContextBasics("POST", "/isc/org", HttpVersion(1, 1));
// Add headers.
request_->context()->headers_.push_back(HttpHeaderContext("Content-Type", "application/json"));
// Must be finalized before can be used.
ASSERT_NO_THROW(request_->finalize());
// Check that the brief string is correct.
EXPECT_EQ("POST /isc/org HTTP/1.1", request_->toBriefString());
}
// This test verifies that no basic HTTP authentication is supported.
TEST_F(HttpRequestTest, noBasicAuth) {
ASSERT_NO_THROW(request_.reset(new HttpRequest(HttpRequest::Method::HTTP_GET,
"/isc/org",
HttpVersion(1, 1),
HostHttpHeader("www.example.org"))));
ASSERT_NO_THROW(request_->finalize());
ASSERT_THROW(request_->getHeader("Authorization"),
HttpMessageNonExistingHeader);
}
// This test verifies that basic HTTP authentication works as expected.
TEST_F(HttpRequestTest, basicAuth) {
BasicHttpAuthPtr basic_auth;
EXPECT_NO_THROW(basic_auth.reset(new BasicHttpAuth("foo", "bar")));
ASSERT_TRUE(basic_auth);
ASSERT_NO_THROW(request_.reset(new HttpRequest(HttpRequest::Method::HTTP_GET,
"/isc/org",
HttpVersion(1, 1),
HostHttpHeader("www.example.org"),
basic_auth)));
ASSERT_NO_THROW(request_->finalize());
std::string value;
EXPECT_NO_THROW(value = request_->getHeaderValue("Authorization"));
EXPECT_EQ(value, "Basic " + basic_auth->getCredential());
}
/// This test verifies that access parameters are handled as expected.
TEST_F(HttpRequestTest, parameters) {
setContextBasics("GET", "/isc/org", HttpVersion(1, 1));
ASSERT_NO_THROW(request_->create());
EXPECT_TRUE(request_->getRemote().empty());
EXPECT_FALSE(request_->getTls());
EXPECT_TRUE(request_->getSubject().empty());
EXPECT_TRUE(request_->getIssuer().empty());
EXPECT_TRUE(request_->getBasicAuth().empty());
EXPECT_TRUE(request_->getCustom().empty());
request_->setRemote("my-remote");
request_->setTls(true);
request_->setSubject("my-subject");
request_->setIssuer("my-issuer");
request_->setBasicAuth("foo");
request_->setCustom("bar");
EXPECT_EQ("my-remote", request_->getRemote());
EXPECT_TRUE(request_->getTls());
EXPECT_EQ("my-subject", request_->getSubject());
EXPECT_EQ("my-issuer", request_->getIssuer());
EXPECT_EQ("foo", request_->getBasicAuth());
EXPECT_EQ("bar", request_->getCustom());
}
}
|