Skip to content

Commit

Permalink
add response tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cmazakas committed Feb 29, 2024
1 parent b911eee commit 24434dd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 37 deletions.
12 changes: 12 additions & 0 deletions include/boost/http_proto/response.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright (c) 2021 Vinnie Falco ([email protected])
// Copyright (c) 2024 Christian Mazakas
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -13,6 +14,7 @@
#include <boost/http_proto/detail/config.hpp>
#include <boost/http_proto/message_base.hpp>
#include <boost/http_proto/response_view.hpp>
#include <boost/http_proto/status.hpp>

namespace boost {
namespace http_proto {
Expand Down Expand Up @@ -90,6 +92,16 @@ class BOOST_SYMBOL_VISIBLE
http_proto::status sc,
http_proto::version v);

/** Constructor
*
* The start-line of the response will contain the standard
* text for the supplied status code and the HTTP version
* will be defaulted to 1.1.
*/
BOOST_HTTP_PROTO_DECL
response(
http_proto::status sc);

/** Return a read-only view to the response
*/
operator
Expand Down
12 changes: 11 additions & 1 deletion src/response.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright (c) 2021 Vinnie Falco ([email protected])
// Copyright (c) 2024 Christian Mazakas
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -9,7 +10,8 @@

#include <boost/http_proto/response.hpp>
#include <boost/http_proto/response_view.hpp>
#include "detail/copied_strings.hpp"
#include <boost/http_proto/version.hpp>

#include <utility>

namespace boost {
Expand Down Expand Up @@ -71,6 +73,14 @@ operator=(
return *this;
}

response::
response(
http_proto::status sc)
: response(
sc, http_proto::version::http_1_1)
{
}

response::
response(
http_proto::status sc,
Expand Down
81 changes: 45 additions & 36 deletions test/unit/response.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright (c) 2019 Vinnie Falco ([email protected])
// Copyright (c) 2024 Christian Mazakas
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -13,13 +14,13 @@
#include <boost/http_proto/response_view.hpp>
#include <boost/http_proto/field.hpp>

#include "test_helpers.hpp"
#include <boost/core/detail/string_view.hpp>

#include "test_suite.hpp"

namespace boost {
namespace http_proto {

#if 0

class response_test
{
public:
Expand All @@ -29,7 +30,7 @@ class response_test
response const& res,
status sc,
unsigned short si,
string_view rs,
core::string_view rs,
version v)
{
BOOST_TEST_EQ(res.version(), v);
Expand All @@ -47,6 +48,14 @@ class response_test
response res(status::ok);
check(res, status::ok, 200, "OK", version::http_1_1);
BOOST_TEST(res.capacity_in_bytes() == 0);
BOOST_TEST_EQ(res.buffer(), "HTTP/1.1 200 OK\r\n\r\n");
}

{
response res(status::ok, version::http_1_0);
check(res, status::ok, 200, "OK", version::http_1_0);
BOOST_TEST(res.capacity_in_bytes() > 0);
BOOST_TEST_EQ(res.buffer(), "HTTP/1.0 200 OK\r\n\r\n");
}

{
Expand All @@ -59,7 +68,7 @@ class response_test
{
response r1(status::ok);
response r2(status::ok);
BOOST_TEST(r1.string().data() == r2.string().data());
BOOST_TEST(r1.buffer().data() == r2.buffer().data());
BOOST_TEST(r1.capacity_in_bytes() == 0);
BOOST_TEST(r2.capacity_in_bytes() == 0);
}
Expand All @@ -68,9 +77,9 @@ class response_test
{
response r1(status::not_found);
response r2(status::not_found);
BOOST_TEST(r1.string().data() != r2.string().data());
BOOST_TEST(r1.capacity_in_bytes() >= 0);
BOOST_TEST(r2.capacity_in_bytes() >= 0);
BOOST_TEST(r1.buffer().data() != r2.buffer().data());
BOOST_TEST(r1.capacity_in_bytes() > 0);
BOOST_TEST(r2.capacity_in_bytes() > 0);
}
}

Expand All @@ -87,7 +96,7 @@ class response_test
response r1;
response r2;
BOOST_TEST(
r1.string().data() == r2.string().data());
r1.buffer().data() == r2.buffer().data());
BOOST_TEST(r1.capacity_in_bytes() == 0);
BOOST_TEST(r2.capacity_in_bytes() == 0);
}
Expand All @@ -101,7 +110,7 @@ class response_test
check(r1, status::ok, 200, "OK", version::http_1_1);
check(r2, status::ok, 200, "OK", version::http_1_1);
BOOST_TEST(
r1.string().data() == r2.string().data());
r1.buffer().data() == r2.buffer().data());
BOOST_TEST(r1.capacity_in_bytes() == 0);
BOOST_TEST(r2.capacity_in_bytes() == 0);
}
Expand All @@ -111,7 +120,7 @@ class response_test
check(r1, status::ok, 200, "OK", version::http_1_1);
check(r2, status::not_found, 404, "Not Found", version::http_1_0);
BOOST_TEST(
r1.string().data() != r2.string().data());
r1.buffer().data() != r2.buffer().data());
BOOST_TEST(r1.capacity_in_bytes() == 0);
BOOST_TEST(r2.capacity_in_bytes() != 0);
}
Expand All @@ -125,7 +134,7 @@ class response_test
check(r1, status::ok, 200, "OK", version::http_1_1);
check(r2, status::ok, 200, "OK", version::http_1_1);
BOOST_TEST(
r1.string().data() == r2.string().data());
r1.buffer().data() == r2.buffer().data());
BOOST_TEST(r1.capacity_in_bytes() == 0);
BOOST_TEST(r2.capacity_in_bytes() == 0);
}
Expand All @@ -135,9 +144,9 @@ class response_test
check(r1, status::not_found, 404, "Not Found", version::http_1_0);
check(r2, status::not_found, 404, "Not Found", version::http_1_0);
BOOST_TEST(
r1.string().data() != r2.string().data());
BOOST_TEST(r1.capacity_in_bytes() >= 0);
BOOST_TEST(r2.capacity_in_bytes() >= 0);
r1.buffer().data() != r2.buffer().data());
BOOST_TEST(r1.capacity_in_bytes() > 0);
BOOST_TEST(r2.capacity_in_bytes() > 0);
}
}

Expand All @@ -158,40 +167,42 @@ class response_test
check(r1, status::not_found, 404, "Not Found", version::http_1_0);
check(r2, status::not_found, 404, "Not Found", version::http_1_0);
BOOST_TEST(
r1.string().data() != r2.string().data());
BOOST_TEST(r1.capacity_in_bytes() >= 0);
BOOST_TEST(r2.capacity_in_bytes() >= 0);
r1.buffer().data() != r2.buffer().data());
BOOST_TEST(r1.capacity_in_bytes() > 0);
BOOST_TEST(r2.capacity_in_bytes() > 0);
}

//----------------------------------------

// response(response_view const&)
{
string_view const s =
core::string_view const s =
"HTTP/1.0 404 Not Found\r\n"
"Server: test\r\n"
"\r\n";
response_view rv = response(s);
response r(s);
response_view rv = r;
response res(rv);
check(res, status::not_found, 404, "Not Found", version::http_1_0);
BOOST_TEST_EQ(res.string(), s);
BOOST_TEST(res.string().data() != s.data());
BOOST_TEST_EQ(res.buffer(), s);
BOOST_TEST(res.buffer().data() != s.data());
BOOST_TEST(res.begin()->id == field::server);
BOOST_TEST(res.begin()->name == "Server");
BOOST_TEST(res.begin()->value == "test");
}

// operator=(response_view const&)
{
string_view const s =
core::string_view const s =
"HTTP/1.1 101 Switching Protocols\r\n"
"Server: test\r\n"
"\r\n";
response_view rv = make_response(s);
response r(s);
response_view rv = r;
response res(status::not_found);
res = rv;
BOOST_TEST_EQ(res.string(), s);
BOOST_TEST(res.string().data() != s.data());
BOOST_TEST_EQ(res.buffer(), s);
BOOST_TEST(res.buffer().data() != s.data());
check(res, status::switching_protocols, 101, "Switching Protocols", version::http_1_1);
BOOST_TEST(res.begin()->id == field::server);
BOOST_TEST(res.begin()->name == "Server");
Expand All @@ -209,8 +220,8 @@ class response_test
BOOST_TEST_EQ(rv.status(), status::ok);
BOOST_TEST_EQ(rv.status_int(), 200);
BOOST_TEST_EQ(rv.reason(), "OK");
BOOST_TEST_EQ(rv.string(), "HTTP/1.1 200 OK\r\n\r\n");
BOOST_TEST(rv.string().data() == res.string().data());
BOOST_TEST_EQ(rv.buffer(), "HTTP/1.1 200 OK\r\n\r\n");
BOOST_TEST(rv.buffer().data() == res.buffer().data());
}
{
response res(status::not_found, version::http_1_0);
Expand All @@ -219,8 +230,8 @@ class response_test
BOOST_TEST_EQ(rv.status(), status::not_found);
BOOST_TEST_EQ(rv.status_int(), 404);
BOOST_TEST_EQ(rv.reason(), "Not Found");
BOOST_TEST_EQ(rv.string(), "HTTP/1.0 404 Not Found\r\n\r\n");
BOOST_TEST(rv.string().data() == res.string().data());
BOOST_TEST_EQ(rv.buffer(), "HTTP/1.0 404 Not Found\r\n\r\n");
BOOST_TEST(rv.buffer().data() == res.buffer().data());
}
}
}
Expand All @@ -234,7 +245,7 @@ class response_test
response res;
BOOST_TEST(res.capacity_in_bytes() == 0);
res.clear();
BOOST_TEST(res.string() == "HTTP/1.1 200 OK\r\n\r\n");
BOOST_TEST(res.buffer() == "HTTP/1.1 200 OK\r\n\r\n");
}
{
response res(status::not_found, version::http_1_0);
Expand Down Expand Up @@ -263,12 +274,13 @@ class response_test
check(res, status::unknown, 199, "Huh", version::http_1_1);
}
{
string_view s =
core::string_view s =
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"Content-Length: 0\r\n"
"\r\n";
response_view rv = response(s);
response r(s);
response_view rv = r;
response res(rv);
check(res, status::ok, 200, "OK", version::http_1_1);
BOOST_TEST(res.size() == 2);
Expand Down Expand Up @@ -296,8 +308,5 @@ TEST_SUITE(
response_test,
"boost.http_proto.response");

#endif

} // http_proto
} // boost

0 comments on commit 24434dd

Please sign in to comment.