request_unittests.cc 5.25 KB
Newer Older
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
// Copyright (C) 2016 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/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) {
    setContextBasics("GET", "/isc/org", std::make_pair(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().first);
    EXPECT_EQ(1, request_.getHttpVersion().second);

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

TEST_F(HttpRequestTest, includeHeaders) {
    setContextBasics("POST", "/isc/org", std::make_pair(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().first);
    EXPECT_EQ(0, request_.getHttpVersion().second);

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

    setContextBasics("GET", "/isc/org", std::make_pair(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);
}

TEST_F(HttpRequestTest, requiredHttpVersion) {
    request_.requireHttpVersion(std::make_pair(1, 0));
    request_.requireHttpVersion(std::make_pair(1, 1));

    setContextBasics("POST", "/isc/org", std::make_pair(1, 0));
    EXPECT_NO_THROW(request_.create());

    setContextBasics("POST", "/isc/org", std::make_pair(1, 1));
    EXPECT_NO_THROW(request_.create());

    setContextBasics("POST", "/isc/org", std::make_pair(2, 0));
    EXPECT_THROW(request_.create(), HttpRequestError);
}

TEST_F(HttpRequestTest, requiredHeader) {
    request_.requireHeader("Content-Length");
    setContextBasics("POST", "/isc/org", std::make_pair(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());
}

TEST_F(HttpRequestTest, requiredHeaderValue) {
    request_.requireHeaderValue("Content-Type", "application/json");
    setContextBasics("POST", "/isc/org", std::make_pair(1, 0));
    addHeaderToContext("Content-Type", "text/html");

    ASSERT_THROW(request_.create(), HttpRequestError);

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

    EXPECT_NO_THROW(request_.create());
}

TEST_F(HttpRequestTest, notCreated) {
    setContextBasics("POST", "/isc/org", std::make_pair(1, 0));
    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\" }";

    setContextBasics("POST", "/isc/org", std::make_pair(1, 0));
    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());
}

}