Commit 3bcb33f2 authored by Marcin Siodelski's avatar Marcin Siodelski

[5088] HttpVersion is now a struct rather than a pair.

This is a result of the review.
parent 17a4fb25
// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
// //
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // License, v. 2.0. If a copy of the MPL was not distributed with this
...@@ -7,11 +7,40 @@ ...@@ -7,11 +7,40 @@
#ifndef HTTP_TYPES_H #ifndef HTTP_TYPES_H
#define HTTP_TYPES_H #define HTTP_TYPES_H
#include <utility>
/// @brief HTTP protocol version. /// @brief HTTP protocol version.
/// struct HttpVersion {
/// First value is a major version, second value is a minor version. unsigned major_; ///< Major HTTP version.
typedef std::pair<unsigned, unsigned> HttpVersion; unsigned minor_; ///< Minor HTTP version.
/// @brief Constructor.
///
/// @param major Major HTTP version.
/// @param minor Minor HTTP version.
explicit HttpVersion(const unsigned major, const unsigned minor)
: major_(major), minor_(minor) {
}
/// @brief Operator less.
///
/// @param rhs Version to compare to.
bool operator<(const HttpVersion& rhs) const {
return ((major_ < rhs.major_) ||
((major_ == rhs.major_) && (minor_ < rhs.minor_)));
}
/// @brief Operator equal.
///
/// @param rhs Version to compare to.
bool operator==(const HttpVersion& rhs) const {
return ((major_ == rhs.major_) && (minor_ == rhs.minor_));
}
/// @brief Operator not equal.
///
/// @param rhs Version to compare to.
bool operator!=(const HttpVersion& rhs) const {
return (!operator==(rhs));
}
};
#endif #endif
// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
// //
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // License, v. 2.0. If a copy of the MPL was not distributed with this
...@@ -66,8 +66,8 @@ HttpRequest::create() { ...@@ -66,8 +66,8 @@ HttpRequest::create() {
} }
// Check if the HTTP version is allowed for this request. // Check if the HTTP version is allowed for this request.
if (!inRequiredSet(std::make_pair(context_->http_version_major_, if (!inRequiredSet(HttpVersion(context_->http_version_major_,
context_->http_version_minor_), context_->http_version_minor_),
required_versions_)) { required_versions_)) {
isc_throw(BadValue, "use of HTTP version " isc_throw(BadValue, "use of HTTP version "
<< context_->http_version_major_ << "." << context_->http_version_major_ << "."
...@@ -144,11 +144,11 @@ HttpRequest::getUri() const { ...@@ -144,11 +144,11 @@ HttpRequest::getUri() const {
return (context_->uri_); return (context_->uri_);
} }
HttpRequest::HttpVersion HttpVersion
HttpRequest::getHttpVersion() const { HttpRequest::getHttpVersion() const {
checkCreated(); checkCreated();
return (std::make_pair(context_->http_version_major_, return (HttpVersion(context_->http_version_major_,
context_->http_version_minor_)); context_->http_version_minor_));
} }
std::string std::string
......
// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
// //
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // License, v. 2.0. If a copy of the MPL was not distributed with this
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#define HTTP_REQUEST_H #define HTTP_REQUEST_H
#include <exceptions/exceptions.h> #include <exceptions/exceptions.h>
#include <http/http_types.h>
#include <http/request_context.h> #include <http/request_context.h>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <map> #include <map>
...@@ -60,9 +61,6 @@ typedef boost::shared_ptr<const HttpRequest> ConstHttpRequestPtr; ...@@ -60,9 +61,6 @@ typedef boost::shared_ptr<const HttpRequest> ConstHttpRequestPtr;
class HttpRequest { class HttpRequest {
public: public:
/// @brief Type of HTTP version, including major and minor version number.
typedef std::pair<unsigned int, unsigned int> HttpVersion;
/// @brief HTTP methods. /// @brief HTTP methods.
enum class Method { enum class Method {
HTTP_GET, HTTP_GET,
......
// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
// //
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // License, v. 2.0. If a copy of the MPL was not distributed with this
...@@ -101,7 +101,7 @@ std::string ...@@ -101,7 +101,7 @@ std::string
HttpResponse::toString() const { HttpResponse::toString() const {
std::ostringstream s; std::ostringstream s;
// HTTP version number and status code. // HTTP version number and status code.
s << "HTTP/" << http_version_.first << "." << http_version_.second; s << "HTTP/" << http_version_.major_ << "." << http_version_.minor_;
s << " " << static_cast<uint16_t>(status_code_); s << " " << static_cast<uint16_t>(status_code_);
s << " " << statusCodeToString(status_code_) << crlf; s << " " << statusCodeToString(status_code_) << crlf;
......
// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
// //
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // License, v. 2.0. If a copy of the MPL was not distributed with this
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <config.h> #include <config.h>
#include <cc/data.h> #include <cc/data.h>
#include <http/http_types.h>
#include <http/post_request_json.h> #include <http/post_request_json.h>
#include <http/tests/request_test.h> #include <http/tests/request_test.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
...@@ -49,14 +50,14 @@ public: ...@@ -49,14 +50,14 @@ public:
// POST messages. // POST messages.
TEST_F(PostHttpRequestJsonTest, requiredPost) { TEST_F(PostHttpRequestJsonTest, requiredPost) {
// Use a GET method that is not supported. // Use a GET method that is not supported.
setContextBasics("GET", "/isc/org", std::make_pair(1, 0)); setContextBasics("GET", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Length", json_body_.length());
addHeaderToContext("Content-Type", "application/json"); addHeaderToContext("Content-Type", "application/json");
ASSERT_THROW(request_.create(), HttpRequestError); ASSERT_THROW(request_.create(), HttpRequestError);
// Now use POST. It should be accepted. // Now use POST. It should be accepted.
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Length", json_body_.length());
addHeaderToContext("Content-Type", "application/json"); addHeaderToContext("Content-Type", "application/json");
...@@ -67,14 +68,14 @@ TEST_F(PostHttpRequestJsonTest, requiredPost) { ...@@ -67,14 +68,14 @@ TEST_F(PostHttpRequestJsonTest, requiredPost) {
// header equal to "application/json". // header equal to "application/json".
TEST_F(PostHttpRequestJsonTest, requireContentTypeJson) { TEST_F(PostHttpRequestJsonTest, requireContentTypeJson) {
// Specify "Content-Type" other than "application/json". // Specify "Content-Type" other than "application/json".
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Length", json_body_.length());
addHeaderToContext("Content-Type", "text/html"); addHeaderToContext("Content-Type", "text/html");
ASSERT_THROW(request_.create(), HttpRequestError); ASSERT_THROW(request_.create(), HttpRequestError);
// This time specify correct "Content-Type". It should pass. // This time specify correct "Content-Type". It should pass.
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Length", json_body_.length());
addHeaderToContext("Content-Type", "application/json"); addHeaderToContext("Content-Type", "application/json");
...@@ -85,13 +86,13 @@ TEST_F(PostHttpRequestJsonTest, requireContentTypeJson) { ...@@ -85,13 +86,13 @@ TEST_F(PostHttpRequestJsonTest, requireContentTypeJson) {
// header. // header.
TEST_F(PostHttpRequestJsonTest, requireContentLength) { TEST_F(PostHttpRequestJsonTest, requireContentLength) {
// "Content-Length" is not specified initially. It should fail. // "Content-Length" is not specified initially. It should fail.
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Type", "text/html"); addHeaderToContext("Content-Type", "text/html");
ASSERT_THROW(request_.create(), HttpRequestError); ASSERT_THROW(request_.create(), HttpRequestError);
// Specify "Content-Length". It should pass. // Specify "Content-Length". It should pass.
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Length", json_body_.length());
addHeaderToContext("Content-Type", "application/json"); addHeaderToContext("Content-Type", "application/json");
} }
...@@ -100,7 +101,7 @@ TEST_F(PostHttpRequestJsonTest, requireContentLength) { ...@@ -100,7 +101,7 @@ TEST_F(PostHttpRequestJsonTest, requireContentLength) {
// HTTP request. // HTTP request.
TEST_F(PostHttpRequestJsonTest, getBodyAsJson) { TEST_F(PostHttpRequestJsonTest, getBodyAsJson) {
// Create HTTP POST request with JSON body. // Create HTTP POST request with JSON body.
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Length", json_body_.length());
addHeaderToContext("Content-Type", "application/json"); addHeaderToContext("Content-Type", "application/json");
setBody(); setBody();
...@@ -129,7 +130,7 @@ TEST_F(PostHttpRequestJsonTest, getBodyAsJson) { ...@@ -129,7 +130,7 @@ TEST_F(PostHttpRequestJsonTest, getBodyAsJson) {
// This test verifies that an attempt to parse/retrieve malformed // This test verifies that an attempt to parse/retrieve malformed
// JSON structure will cause an exception. // JSON structure will cause an exception.
TEST_F(PostHttpRequestJsonTest, getBodyAsJsonMalformed) { TEST_F(PostHttpRequestJsonTest, getBodyAsJsonMalformed) {
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Length", json_body_.length());
addHeaderToContext("Content-Type", "application/json"); addHeaderToContext("Content-Type", "application/json");
// No colon before 123. // No colon before 123.
...@@ -141,7 +142,7 @@ TEST_F(PostHttpRequestJsonTest, getBodyAsJsonMalformed) { ...@@ -141,7 +142,7 @@ TEST_F(PostHttpRequestJsonTest, getBodyAsJsonMalformed) {
// This test verifies that NULL pointer is returned when trying to // This test verifies that NULL pointer is returned when trying to
// retrieve root element of the empty JSON structure. // retrieve root element of the empty JSON structure.
TEST_F(PostHttpRequestJsonTest, getEmptyJsonBody) { TEST_F(PostHttpRequestJsonTest, getEmptyJsonBody) {
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Length", json_body_.length());
addHeaderToContext("Content-Type", "application/json"); addHeaderToContext("Content-Type", "application/json");
...@@ -153,7 +154,7 @@ TEST_F(PostHttpRequestJsonTest, getEmptyJsonBody) { ...@@ -153,7 +154,7 @@ TEST_F(PostHttpRequestJsonTest, getEmptyJsonBody) {
// This test verifies that the specific JSON element can be retrieved. // This test verifies that the specific JSON element can be retrieved.
TEST_F(PostHttpRequestJsonTest, getJsonElement) { TEST_F(PostHttpRequestJsonTest, getJsonElement) {
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", json_body_.length()); addHeaderToContext("Content-Length", json_body_.length());
addHeaderToContext("Content-Type", "application/json"); addHeaderToContext("Content-Type", "application/json");
setBody(); setBody();
......
// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
// //
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // License, v. 2.0. If a copy of the MPL was not distributed with this
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <config.h> #include <config.h>
#include <cc/data.h> #include <cc/data.h>
#include <http/http_types.h>
#include <http/request_parser.h> #include <http/request_parser.h>
#include <http/post_request_json.h> #include <http/post_request_json.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
...@@ -118,8 +119,8 @@ TEST_F(HttpRequestParserTest, postHttpRequestWithJson) { ...@@ -118,8 +119,8 @@ TEST_F(HttpRequestParserTest, postHttpRequestWithJson) {
EXPECT_EQ("/foo/bar", request.getUri()); EXPECT_EQ("/foo/bar", request.getUri());
EXPECT_EQ("application/json", request.getHeaderValue("Content-Type")); EXPECT_EQ("application/json", request.getHeaderValue("Content-Type"));
EXPECT_EQ(json.length(), request.getHeaderValueAsUint64("Content-Length")); EXPECT_EQ(json.length(), request.getHeaderValueAsUint64("Content-Length"));
EXPECT_EQ(1, request.getHttpVersion().first); EXPECT_EQ(1, request.getHttpVersion().major_);
EXPECT_EQ(0, request.getHttpVersion().second); EXPECT_EQ(0, request.getHttpVersion().minor_);
// Try to retrieve values carried in JSON payload. // Try to retrieve values carried in JSON payload.
ConstElementPtr json_element; ConstElementPtr json_element;
...@@ -190,8 +191,8 @@ TEST_F(HttpRequestParserTest, getLWS) { ...@@ -190,8 +191,8 @@ TEST_F(HttpRequestParserTest, getLWS) {
EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type")); EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type"));
EXPECT_EQ("Kea/1.2 Command Control Client", EXPECT_EQ("Kea/1.2 Command Control Client",
request_.getHeaderValue("User-Agent")); request_.getHeaderValue("User-Agent"));
EXPECT_EQ(1, request_.getHttpVersion().first); EXPECT_EQ(1, request_.getHttpVersion().major_);
EXPECT_EQ(1, request_.getHttpVersion().second); EXPECT_EQ(1, request_.getHttpVersion().minor_);
} }
// This test verifies that the HTTP request with no headers is // This test verifies that the HTTP request with no headers is
...@@ -204,8 +205,8 @@ TEST_F(HttpRequestParserTest, noHeaders) { ...@@ -204,8 +205,8 @@ TEST_F(HttpRequestParserTest, noHeaders) {
// Verify the values. // Verify the values.
EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod());
EXPECT_EQ("/foo/bar", request_.getUri()); EXPECT_EQ("/foo/bar", request_.getUri());
EXPECT_EQ(1, request_.getHttpVersion().first); EXPECT_EQ(1, request_.getHttpVersion().major_);
EXPECT_EQ(1, request_.getHttpVersion().second); EXPECT_EQ(1, request_.getHttpVersion().minor_);
} }
// This test verifies that the HTTP method can be specified in lower // This test verifies that the HTTP method can be specified in lower
...@@ -219,8 +220,8 @@ TEST_F(HttpRequestParserTest, getLowerCase) { ...@@ -219,8 +220,8 @@ TEST_F(HttpRequestParserTest, getLowerCase) {
EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod());
EXPECT_EQ("/foo/bar", request_.getUri()); EXPECT_EQ("/foo/bar", request_.getUri());
EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type")); EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type"));
EXPECT_EQ(1, request_.getHttpVersion().first); EXPECT_EQ(1, request_.getHttpVersion().major_);
EXPECT_EQ(1, request_.getHttpVersion().second); EXPECT_EQ(1, request_.getHttpVersion().minor_);
} }
// This test verifies that other value of the HTTP version can be // This test verifies that other value of the HTTP version can be
...@@ -234,8 +235,8 @@ TEST_F(HttpRequestParserTest, http20) { ...@@ -234,8 +235,8 @@ TEST_F(HttpRequestParserTest, http20) {
EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod());
EXPECT_EQ("/foo/bar", request_.getUri()); EXPECT_EQ("/foo/bar", request_.getUri());
EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type")); EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type"));
EXPECT_EQ(2, request_.getHttpVersion().first); EXPECT_EQ(2, request_.getHttpVersion().major_);
EXPECT_EQ(0, request_.getHttpVersion().second); EXPECT_EQ(0, request_.getHttpVersion().minor_);
} }
// This test verifies that the header with no whitespace between the // This test verifies that the header with no whitespace between the
...@@ -249,8 +250,8 @@ TEST_F(HttpRequestParserTest, noHeaderWhitespace) { ...@@ -249,8 +250,8 @@ TEST_F(HttpRequestParserTest, noHeaderWhitespace) {
EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod());
EXPECT_EQ("/foo/bar", request_.getUri()); EXPECT_EQ("/foo/bar", request_.getUri());
EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type")); EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type"));
EXPECT_EQ(1, request_.getHttpVersion().first); EXPECT_EQ(1, request_.getHttpVersion().major_);
EXPECT_EQ(0, request_.getHttpVersion().second); EXPECT_EQ(0, request_.getHttpVersion().minor_);
} }
// This test verifies that the header value preceded with multiple // This test verifies that the header value preceded with multiple
...@@ -264,8 +265,8 @@ TEST_F(HttpRequestParserTest, multipleLeadingHeaderWhitespaces) { ...@@ -264,8 +265,8 @@ TEST_F(HttpRequestParserTest, multipleLeadingHeaderWhitespaces) {
EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod());
EXPECT_EQ("/foo/bar", request_.getUri()); EXPECT_EQ("/foo/bar", request_.getUri());
EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type")); EXPECT_EQ("text/html", request_.getHeaderValue("Content-Type"));
EXPECT_EQ(1, request_.getHttpVersion().first); EXPECT_EQ(1, request_.getHttpVersion().major_);
EXPECT_EQ(0, request_.getHttpVersion().second); EXPECT_EQ(0, request_.getHttpVersion().minor_);
} }
// This test verifies that error is reported when unsupported HTTP // This test verifies that error is reported when unsupported HTTP
......
// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
// //
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // License, v. 2.0. If a copy of the MPL was not distributed with this
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#ifndef HTTP_REQUEST_TEST_H #ifndef HTTP_REQUEST_TEST_H
#define HTTP_REQUEST_TEST_H #define HTTP_REQUEST_TEST_H
#include <http/http_types.h>
#include <http/request.h> #include <http/request.h>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <gtest/gtest.h> #include <gtest/gtest.h>
...@@ -50,11 +51,11 @@ public: ...@@ -50,11 +51,11 @@ public:
/// @param version A pair of values of which the first is the major HTTP /// @param version A pair of values of which the first is the major HTTP
/// version and the second is the minor HTTP version. /// version and the second is the minor HTTP version.
void setContextBasics(const std::string& method, const std::string& uri, void setContextBasics(const std::string& method, const std::string& uri,
const std::pair<unsigned int, unsigned int>& version) { const HttpVersion& version) {
request_.context()->method_ = method; request_.context()->method_ = method;
request_.context()->uri_ = uri; request_.context()->uri_ = uri;
request_.context()->http_version_major_ = version.first; request_.context()->http_version_major_ = version.major_;
request_.context()->http_version_minor_ = version.second; request_.context()->http_version_minor_ = version.minor_;
} }
/// @brief Adds HTTP header to the context. /// @brief Adds HTTP header to the context.
......
// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC") // Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
// //
// This Source Code Form is subject to the terms of the Mozilla Public // 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 // License, v. 2.0. If a copy of the MPL was not distributed with this
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <config.h> #include <config.h>
#include <http/request.h> #include <http/request.h>
#include <http/http_types.h>
#include <http/tests/request_test.h> #include <http/tests/request_test.h>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <gtest/gtest.h> #include <gtest/gtest.h>
...@@ -20,28 +21,28 @@ namespace { ...@@ -20,28 +21,28 @@ namespace {
typedef HttpRequestTestBase<HttpRequest> HttpRequestTest; typedef HttpRequestTestBase<HttpRequest> HttpRequestTest;
TEST_F(HttpRequestTest, minimal) { TEST_F(HttpRequestTest, minimal) {
setContextBasics("GET", "/isc/org", std::make_pair(1, 1)); setContextBasics("GET", "/isc/org", HttpVersion(1, 1));
ASSERT_NO_THROW(request_.create()); ASSERT_NO_THROW(request_.create());
EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod()); EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod());
EXPECT_EQ("/isc/org", request_.getUri()); EXPECT_EQ("/isc/org", request_.getUri());
EXPECT_EQ(1, request_.getHttpVersion().first); EXPECT_EQ(1, request_.getHttpVersion().major_);
EXPECT_EQ(1, request_.getHttpVersion().second); EXPECT_EQ(1, request_.getHttpVersion().minor_);
EXPECT_THROW(request_.getHeaderValue("Content-Length"), EXPECT_THROW(request_.getHeaderValue("Content-Length"),
HttpRequestNonExistingHeader); HttpRequestNonExistingHeader);
} }
TEST_F(HttpRequestTest, includeHeaders) { TEST_F(HttpRequestTest, includeHeaders) {
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Length", "1024"); addHeaderToContext("Content-Length", "1024");
addHeaderToContext("Content-Type", "application/json"); addHeaderToContext("Content-Type", "application/json");
ASSERT_NO_THROW(request_.create()); ASSERT_NO_THROW(request_.create());
EXPECT_EQ(HttpRequest::Method::HTTP_POST, request_.getMethod()); EXPECT_EQ(HttpRequest::Method::HTTP_POST, request_.getMethod());
EXPECT_EQ("/isc/org", request_.getUri()); EXPECT_EQ("/isc/org", request_.getUri());
EXPECT_EQ(1, request_.getHttpVersion().first); EXPECT_EQ(1, request_.getHttpVersion().major_);
EXPECT_EQ(0, request_.getHttpVersion().second); EXPECT_EQ(0, request_.getHttpVersion().minor_);
std::string content_type; std::string content_type;
ASSERT_NO_THROW(content_type = request_.getHeaderValue("Content-Type")); ASSERT_NO_THROW(content_type = request_.getHeaderValue("Content-Type"));
...@@ -58,7 +59,7 @@ TEST_F(HttpRequestTest, requiredMethods) { ...@@ -58,7 +59,7 @@ TEST_F(HttpRequestTest, requiredMethods) {
request_.requireHttpMethod(HttpRequest::Method::HTTP_GET); request_.requireHttpMethod(HttpRequest::Method::HTTP_GET);
request_.requireHttpMethod(HttpRequest::Method::HTTP_POST); request_.requireHttpMethod(HttpRequest::Method::HTTP_POST);
setContextBasics("GET", "/isc/org", std::make_pair(1, 1)); setContextBasics("GET", "/isc/org", HttpVersion(1, 1));
ASSERT_NO_THROW(request_.create()); ASSERT_NO_THROW(request_.create());
...@@ -70,22 +71,22 @@ TEST_F(HttpRequestTest, requiredMethods) { ...@@ -70,22 +71,22 @@ TEST_F(HttpRequestTest, requiredMethods) {
} }
TEST_F(HttpRequestTest, requiredHttpVersion) { TEST_F(HttpRequestTest, requiredHttpVersion) {
request_.requireHttpVersion(std::make_pair(1, 0)); request_.requireHttpVersion(HttpVersion(1, 0));
request_.requireHttpVersion(std::make_pair(1, 1)); request_.requireHttpVersion(HttpVersion(1, 1));
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
EXPECT_NO_THROW(request_.create()); EXPECT_NO_THROW(request_.create());
setContextBasics("POST", "/isc/org", std::make_pair(1, 1)); setContextBasics("POST", "/isc/org", HttpVersion(1, 1));
EXPECT_NO_THROW(request_.create()); EXPECT_NO_THROW(request_.create());
setContextBasics("POST", "/isc/org", std::make_pair(2, 0)); setContextBasics("POST", "/isc/org", HttpVersion(2, 0));
EXPECT_THROW(request_.create(), HttpRequestError); EXPECT_THROW(request_.create(), HttpRequestError);
} }
TEST_F(HttpRequestTest, requiredHeader) { TEST_F(HttpRequestTest, requiredHeader) {
request_.requireHeader("Content-Length"); request_.requireHeader("Content-Length");
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
ASSERT_THROW(request_.create(), HttpRequestError); ASSERT_THROW(request_.create(), HttpRequestError);
...@@ -98,7 +99,7 @@ TEST_F(HttpRequestTest, requiredHeader) { ...@@ -98,7 +99,7 @@ TEST_F(HttpRequestTest, requiredHeader) {
TEST_F(HttpRequestTest, requiredHeaderValue) { TEST_F(HttpRequestTest, requiredHeaderValue) {
request_.requireHeaderValue("Content-Type", "application/json"); request_.requireHeaderValue("Content-Type", "application/json");
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Type", "text/html"); addHeaderToContext("Content-Type", "text/html");
ASSERT_THROW(request_.create(), HttpRequestError); ASSERT_THROW(request_.create(), HttpRequestError);
...@@ -109,7 +110,7 @@ TEST_F(HttpRequestTest, requiredHeaderValue) { ...@@ -109,7 +110,7 @@ TEST_F(HttpRequestTest, requiredHeaderValue) {
} }
TEST_F(HttpRequestTest, notCreated) { TEST_F(HttpRequestTest, notCreated) {
setContextBasics("POST", "/isc/org", std::make_pair(1, 0)); setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
addHeaderToContext("Content-Type", "text/html"); addHeaderToContext("Content-Type", "text/html");
addHeaderToContext("Content-Length", "1024"); addHeaderToContext("Content-Length", "1024");
...@@ -138,7 +139,7 @@ TEST_F(HttpRequestTest, notCreated) { ...@@ -138,7 +139,7 @@ TEST_F(HttpRequestTest, notCreated) {