request_unittests.cc 5.25 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/http_types.h>
11 12 13 14 15 16 17 18 19 20 21 22 23
#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 {

typedef HttpRequestTestBase<HttpRequest> HttpRequestTest;

TEST_F(HttpRequestTest, minimal) {
24
    setContextBasics("GET", "/isc/org", HttpVersion(1, 1));
25 26 27 28
    ASSERT_NO_THROW(request_.create());

    EXPECT_EQ(HttpRequest::Method::HTTP_GET, request_.getMethod());
    EXPECT_EQ("/isc/org", request_.getUri());
29 30
    EXPECT_EQ(1, request_.getHttpVersion().major_);
    EXPECT_EQ(1, request_.getHttpVersion().minor_);
31 32 33 34 35 36

    EXPECT_THROW(request_.getHeaderValue("Content-Length"),
                 HttpRequestNonExistingHeader);
}

TEST_F(HttpRequestTest, includeHeaders) {
37
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
38 39 40 41 42 43
    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());
44 45
    EXPECT_EQ(1, request_.getHttpVersion().major_);
    EXPECT_EQ(0, request_.getHttpVersion().minor_);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

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

62
    setContextBasics("GET", "/isc/org", HttpVersion(1, 1));
63 64 65 66 67 68 69 70 71 72 73

    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) {
74 75
    request_.requireHttpVersion(HttpVersion(1, 0));
    request_.requireHttpVersion(HttpVersion(1, 1));
76

77
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
78 79
    EXPECT_NO_THROW(request_.create());

80
    setContextBasics("POST", "/isc/org", HttpVersion(1, 1));
81 82
    EXPECT_NO_THROW(request_.create());

83
    setContextBasics("POST", "/isc/org", HttpVersion(2, 0));
84 85 86 87 88
    EXPECT_THROW(request_.create(), HttpRequestError);
}

TEST_F(HttpRequestTest, requiredHeader) {
    request_.requireHeader("Content-Length");
89
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
90 91 92 93 94 95 96 97 98 99 100 101

    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");
102
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
103 104 105 106 107 108 109 110 111 112
    addHeaderToContext("Content-Type", "text/html");

    ASSERT_THROW(request_.create(), HttpRequestError);

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

    EXPECT_NO_THROW(request_.create());
}

TEST_F(HttpRequestTest, notCreated) {
113
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
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
    addHeaderToContext("Content-Type", "text/html");
    addHeaderToContext("Content-Length", "1024");

    EXPECT_THROW(static_cast<void>(request_.getMethod()), HttpRequestError);
    EXPECT_THROW(static_cast<void>(request_.getHttpVersion()),
                 HttpRequestError);
    EXPECT_THROW(static_cast<void>(request_.getUri()), HttpRequestError);
    EXPECT_THROW(static_cast<void>(request_.getHeaderValue("Content-Type")),
                 HttpRequestError);
    EXPECT_THROW(static_cast<void>(request_.getHeaderValueAsUint64("Content-Length")),
                 HttpRequestError);
    EXPECT_THROW(static_cast<void>(request_.getBody()), HttpRequestError);

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

142
    setContextBasics("POST", "/isc/org", HttpVersion(1, 0));
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    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());
}

}