From 9d95f97b524675f5e00ab2028471d089f681c390 Mon Sep 17 00:00:00 2001 From: Christian Mazakas Date: Thu, 29 Feb 2024 13:27:56 -0800 Subject: [PATCH] add response tests --- include/boost/http_proto/response.hpp | 13 +++++ src/response.cpp | 12 +++- test/unit/response.cpp | 81 +++++++++++++++------------ 3 files changed, 69 insertions(+), 37 deletions(-) diff --git a/include/boost/http_proto/response.hpp b/include/boost/http_proto/response.hpp index 8a4a3d07..db7bad9f 100644 --- a/include/boost/http_proto/response.hpp +++ b/include/boost/http_proto/response.hpp @@ -1,5 +1,6 @@ // // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com) +// 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) @@ -13,6 +14,7 @@ #include #include #include +#include namespace boost { namespace http_proto { @@ -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( + http_proto::status sc); + /** Return a read-only view to the response */ operator diff --git a/src/response.cpp b/src/response.cpp index a0ee9799..0ca21a48 100644 --- a/src/response.cpp +++ b/src/response.cpp @@ -1,5 +1,6 @@ // // Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com) +// 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) @@ -9,7 +10,8 @@ #include #include -#include "detail/copied_strings.hpp" +#include + #include namespace boost { @@ -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, diff --git a/test/unit/response.cpp b/test/unit/response.cpp index 3e3c408b..141e3d99 100644 --- a/test/unit/response.cpp +++ b/test/unit/response.cpp @@ -1,5 +1,6 @@ // // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com) +// 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) @@ -13,13 +14,13 @@ #include #include -#include "test_helpers.hpp" +#include + +#include "test_suite.hpp" namespace boost { namespace http_proto { -#if 0 - class response_test { public: @@ -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); @@ -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"); } { @@ -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); } @@ -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); } } @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } } @@ -158,24 +167,25 @@ 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"); @@ -183,15 +193,16 @@ class response_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"); @@ -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); @@ -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()); } } } @@ -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); @@ -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); @@ -296,8 +308,5 @@ TEST_SUITE( response_test, "boost.http_proto.response"); -#endif - } // http_proto } // boost -