Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add response tests #67

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 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,17 @@ 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
explicit
response(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be explicit?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the http_proto::status is an enum class, I don't think it's strictly required here.

I tried to making it file with something like response res({200}); but I couldn't get it to compile in c++11 or c++20.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't hurt though, to communicate this to the reader

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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm.. I don't know how I feel about this. Upon construction using this signature, the message will be invalid out of the box, because it is missing the required "Server" field.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep in mind, changing this from version::http_1_1 to version::http_1_0 means that this constructor allocates now, and a bunch of the tests seem predicated on this not allocating so these will have to be adjusted.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1_0 is not a good default

: 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

Loading