request_unittests.cc 9.64 KB
Newer Older
1
// Copyright (C) 2016-2017 Internet Systems Consortium, Inc. ("ISC")
2 3 4 5 6 7 8 9
//
// 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>
10
#include <http/date_time.h>
11
#include <http/http_header.h>
12
#include <http/http_types.h>
13 14 15 16 17 18 19 20 21 22
#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 {

23
/// @brief Test fixture class for @c HttpRequest class.
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
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());
    }

};
69 70

TEST_F(HttpRequestTest, minimal) {
71
    setContextBasics("GET", "/isc/org", HttpVersion(1, 1));
72 73 74 75
    ASSERT_NO_THROW(request_.create());

    EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod());
    EXPECT_EQ("/isc/org", request_.getUri());
76 77
    EXPECT_EQ(1, request_.getHttpVersion().major_);
    EXPECT_EQ(1, request_.getHttpVersion().minor_);
78 79

    EXPECT_THROW(request_.getHeaderValue("Content-Length"),
80
                 HttpMessageNonExistingHeader);
81 82 83
}

TEST_F(HttpRequestTest, includeHeaders) {
84
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
85 86 87 88 89 90
    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());
91 92
    EXPECT_EQ(1, request_.getHttpVersion().major_);
    EXPECT_EQ(0, request_.getHttpVersion().minor_);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

    std::string content_type;
    ASSERT_NO_THROW(content_type = request_.getHeaderValue("Content-Type"));
    EXPECT_EQ("application/json", content_type);

    uint64_t content_length;
    ASSERT_NO_THROW(
        content_length = request_.getHeaderValueAsUint64("Content-Length")
    );
    EXPECT_EQ(1024, content_length);
}

TEST_F(HttpRequestTest, requiredMethods) {
    request_.requireHttpMethod(HttpRequest::Method::HTTP_GET);
    request_.requireHttpMethod(HttpRequest::Method::HTTP_POST);

109
    setContextBasics("GET", "/isc/org", HttpVersion(1, 1));
110 111 112 113 114 115 116 117 118 119 120

    ASSERT_NO_THROW(request_.create());

    request_.context()->method_ = "POST";
    ASSERT_NO_THROW(request_.create());

    request_.context()->method_ = "PUT";
    EXPECT_THROW(request_.create(), HttpRequestError);
}

TEST_F(HttpRequestTest, requiredHttpVersion) {
121 122
    request_.requireHttpVersion(HttpVersion(1, 0));
    request_.requireHttpVersion(HttpVersion(1, 1));
123

124
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
125 126
    EXPECT_NO_THROW(request_.create());

127
    setContextBasics("POST", "/isc/org", HttpVersion(1, 1));
128 129
    EXPECT_NO_THROW(request_.create());

130
    setContextBasics("POST", "/isc/org", HttpVersion(2, 0));
131 132 133 134 135
    EXPECT_THROW(request_.create(), HttpRequestError);
}

TEST_F(HttpRequestTest, requiredHeader) {
    request_.requireHeader("Content-Length");
136
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
137 138 139 140 141 142 143 144 145 146 147 148

    ASSERT_THROW(request_.create(), HttpRequestError);

    addHeaderToContext("Content-Type", "application/json");
    ASSERT_THROW(request_.create(), HttpRequestError);

    addHeaderToContext("Content-Length", "2048");
    EXPECT_NO_THROW(request_.create());
}

TEST_F(HttpRequestTest, requiredHeaderValue) {
    request_.requireHeaderValue("Content-Type", "application/json");
149
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
150 151 152 153 154 155 156 157 158 159
    addHeaderToContext("Content-Type", "text/html");

    ASSERT_THROW(request_.create(), HttpRequestError);

    addHeaderToContext("Content-Type", "application/json");

    EXPECT_NO_THROW(request_.create());
}

TEST_F(HttpRequestTest, notCreated) {
160
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
161 162 163
    addHeaderToContext("Content-Type", "text/html");
    addHeaderToContext("Content-Length", "1024");

164
    EXPECT_THROW(static_cast<void>(request_.getMethod()), HttpMessageError);
165
    EXPECT_THROW(static_cast<void>(request_.getHttpVersion()),
166 167
                 HttpMessageError);
    EXPECT_THROW(static_cast<void>(request_.getUri()), HttpMessageError);
168
    EXPECT_THROW(static_cast<void>(request_.getHeaderValue("Content-Type")),
169
                 HttpMessageError);
170
    EXPECT_THROW(static_cast<void>(request_.getHeaderValueAsUint64("Content-Length")),
171 172
                 HttpMessageError);
    EXPECT_THROW(static_cast<void>(request_.getBody()), HttpMessageError);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188

    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()));
}

TEST_F(HttpRequestTest, getBody) {
    std::string json_body = "{ \"param1\": \"foo\" }";

189
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
    addHeaderToContext("Content-Length", json_body.length());

    request_.context()->body_ = json_body;

    ASSERT_NO_THROW(request_.finalize());

    EXPECT_EQ(json_body, request_.getBody());
}

TEST_F(HttpRequestTest, requiresBody) {
    ASSERT_FALSE(request_.requiresBody());
    request_.requireHeader("Content-Length");
    EXPECT_TRUE(request_.requiresBody());
}

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
TEST_F(HttpRequestTest, isPersistentHttp10) {
    // In HTTP 1.0 the connection is by default non-persistent.
    EXPECT_FALSE(isPersistent(HttpVersion(1, 0)));
}

TEST_F(HttpRequestTest, isPersistentHttp11) {
    // In HTTP 1.1 the connection is by default persistent.
    EXPECT_TRUE(isPersistent(HttpVersion(1, 1)));
}

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"))
    );
}

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"))
    );
}

231
TEST_F(HttpRequestTest, clientRequest) {
232
    request_.setDirection(HttpMessage::OUTBOUND);
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));

    // 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"
              "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>",
257
              request_.toString());
258 259 260 261 262 263 264 265 266 267 268 269 270 271
}

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",
272
              request_.toString());
273 274
}

275
}