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

request.cpp coverage #64

Merged
merged 3 commits into from
Feb 27, 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
2 changes: 1 addition & 1 deletion src/detail/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ on_erase_expect()
return;
*/
// reset and re-insert
auto n = md.expect.count;
auto n = count;
auto const p = cbuf + prefix;
auto const* e = &tab()[0];
md.expect = {};
Expand Down
30 changes: 11 additions & 19 deletions src/request.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 @@ -10,7 +11,6 @@
#include <boost/http_proto/request.hpp>
#include <boost/http_proto/request_view.hpp>
#include "detail/copied_strings.hpp"
#include "detail/number_string.hpp"
#include <utility>

namespace boost {
Expand Down Expand Up @@ -112,7 +112,7 @@ set_expect_100_continue(bool b)
! h_.md.expect.is_100_continue);
auto it = find(field::expect);
BOOST_ASSERT(it != end());
erase(it);
set(it, "100-continue");
return;
}

Expand All @@ -122,25 +122,17 @@ set_expect_100_continue(bool b)
return;
}

if(b)
{
if(! h_.md.expect.ec.failed())
{
// remove all but one
raw_erase_n(
field::expect,
h_.md.expect.count - 1);
return;
}
BOOST_ASSERT(h_.md.expect.ec.failed());

erase(field::expect);
append(
field::expect,
"100-continue");
return;
}
auto nc = (b ? 1 : 0);
auto ne = h_.md.expect.count - nc;
if( b )
set(find(field::expect), "100-continue");

erase(field::expect);
raw_erase_n(field::expect, ne);
h_.md.expect.count = nc;
h_.md.expect.ec = {};
h_.md.expect.is_100_continue = b;
}

//------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions test/unit/fields_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1271,7 +1271,9 @@ struct fields_base_test
req.count(field::expect), 1);
},
"POST / HTTP/1.1\r\n"
"Content-Length: 1234\r\n"
"Expect: 100-continue\r\n"
"Connection: close\r\n"
"Expect: 404-not-found\r\n"
"\r\n");

Expand Down
188 changes: 182 additions & 6 deletions test/unit/request.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 @@ -10,10 +11,12 @@
// Test that header file is self-contained.
#include <boost/http_proto/request.hpp>

#include "test_helpers.hpp"
#include <boost/http_proto/request_view.hpp>

#include <utility>

#include "test_suite.hpp"

namespace boost {
namespace http_proto {

Expand Down Expand Up @@ -243,6 +246,57 @@ struct request_test
}
}

void
testViewConstructor()
{
{
request req;
BOOST_TEST_EQ(
req.buffer(),
"GET / HTTP/1.1\r\n\r\n");

request_view req_view(req);
request req2(req_view);

BOOST_TEST_EQ(
req2.buffer(),
"GET / HTTP/1.1\r\n\r\n");

// default-constructed recycles the same string literal
BOOST_TEST_EQ(
req2.buffer().data(),
req.buffer().data());

BOOST_TEST_EQ(
req2.buffer().data(),
req_view.buffer().data());

}

{
request req;
req.set_method("POST");
BOOST_TEST_EQ(
req.buffer(),
"POST / HTTP/1.1\r\n\r\n");

request_view req_view(req);
request req2(req_view);

BOOST_TEST_EQ(
req2.buffer(),
"POST / HTTP/1.1\r\n\r\n");

BOOST_TEST_NE(
req2.buffer().data(),
req.buffer().data());

BOOST_TEST_NE(
req2.buffer().data(),
req_view.buffer().data());
}
}

void
testObservers()
{
Expand Down Expand Up @@ -411,18 +465,140 @@ struct request_test
void
testExpect()
{
request req;
req.set_expect_100_continue(true);
BOOST_TEST(req.metadata().expect.is_100_continue == true);
req.set_expect_100_continue(false);
BOOST_TEST(req.metadata().expect.is_100_continue == false);
{
request req;
req.set_expect_100_continue(true);
BOOST_TEST(
req.metadata().expect.is_100_continue);

req.set_expect_100_continue(false);
BOOST_TEST(
!req.metadata().expect.is_100_continue);
}


{
request req;
req.set_expect_100_continue(false);
BOOST_TEST(
!req.metadata().expect.is_100_continue);
}

{
request req;
req.set_expect_100_continue(true);
BOOST_TEST(
req.metadata().expect.is_100_continue);

req.set_expect_100_continue(true);
BOOST_TEST(
req.metadata().expect.is_100_continue);
}

{
request req;
BOOST_TEST(
!req.metadata().expect.ec.failed());

req.set("Expect", "101-dalmations");
BOOST_TEST(
req.metadata().expect.ec.failed());

req.set_expect_100_continue(true);
BOOST_TEST(
!req.metadata().expect.ec.failed());
BOOST_TEST(
req.metadata().expect.is_100_continue);
}

{
request req;
req.append("Expect", "100-continue");
req.append("Expect", "101-dalmations");

BOOST_TEST(
req.metadata().expect.ec.failed());
BOOST_TEST_EQ(
req.count("Expect"), 2);

req.set_expect_100_continue(true);
BOOST_TEST(
!req.metadata().expect.ec.failed());
BOOST_TEST(
req.metadata().expect.is_100_continue);
BOOST_TEST_EQ(
req.count("Expect"), 1);
}

{
request req;
req.append("Expect", "100-continue");
req.append("Content-Length", "1234");
req.append("Expect", "100-continue");

BOOST_TEST(
req.metadata().expect.ec.failed());
BOOST_TEST_EQ(
req.count("Expect"), 2);

req.set_expect_100_continue(true);
BOOST_TEST(
!req.metadata().expect.ec.failed());
BOOST_TEST(
req.metadata().expect.is_100_continue);
BOOST_TEST_EQ(
req.count("Expect"), 1);
}

{
request req;
req.append("Expect", "404-not-found");
req.append("Content-Length", "1234");
req.append("Expect", "101-dalmations");

BOOST_TEST(
req.metadata().expect.ec.failed());
BOOST_TEST_EQ(
req.count("Expect"), 2);

req.set_expect_100_continue(true);
BOOST_TEST(
!req.metadata().expect.ec.failed());
BOOST_TEST(
req.metadata().expect.is_100_continue);
BOOST_TEST_EQ(
req.count("Expect"), 1);
}

{
request req;
req.append("Expect", "100-continue");
req.append("Content-Length", "1234");
req.append("Expect", "100-continue");

BOOST_TEST(
req.metadata().expect.ec.failed());
BOOST_TEST_EQ(
req.count("Expect"), 2);

req.set_expect_100_continue(false);
BOOST_TEST(
!req.metadata().expect.ec.failed());
BOOST_TEST(
!req.metadata().expect.is_100_continue);
BOOST_TEST_EQ(
req.count("Expect"), 0);
BOOST_TEST_EQ(
req.count("Content-Length"), 1);
}
}

void
run()
{
testHelpers();
testSpecial();
testViewConstructor();
testObservers();
testModifiers();
testExpect();
Expand Down
Loading