Skip to content
Open
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
18 changes: 18 additions & 0 deletions src/cpp/http/response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ Response Response::from_status(StatusCode status) {
return res;
}

Response Response::see_other(std::string_view destination) {
Response res(fastly::sys::http::m_static_http_response_see_other(
static_cast<std::string>(destination)));
return res;
}

Response Response::redirect(std::string_view destination) {
Response res(fastly::sys::http::m_static_http_response_redirect(
static_cast<std::string>(destination)));
return res;
}

Response Response::temporary_redirect(std::string_view destination) {
Response res(fastly::sys::http::m_static_http_response_temporary_redirect(
static_cast<std::string>(destination)));
return res;
}

Response Response::with_body(Body body) && {
this->set_body(std::move(body));
return std::move(*this);
Expand Down
49 changes: 49 additions & 0 deletions test/response_redirect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <catch2/catch_test_macros.hpp>
#include <fastly/http/response.h>

using namespace fastly::http;

namespace {

// Returns the response's `Location` header, or std::nullopt if absent.
std::optional<std::string> location_of(Response &resp) {
auto header{resp.get_header("Location")};
if (!header.has_value() || !header->has_value()) {
return std::nullopt;
}
auto value{header->value().string()};
if (!value.has_value()) {
return std::nullopt;
}
return std::string(*value);
}

} // namespace

TEST_CASE("Response redirect constructors set the Location header",
"[response]") {
SECTION("Response::see_other") {
auto resp{Response::see_other("https://www.fastly.com")};
REQUIRE(location_of(resp) == std::optional("https://www.fastly.com"));
}

SECTION("Response::redirect") {
auto resp{Response::redirect("https://www.fastly.com")};
REQUIRE(location_of(resp) == std::optional("https://www.fastly.com"));
}

SECTION("Response::temporary_redirect") {
auto resp{Response::temporary_redirect("https://www.fastly.com")};
REQUIRE(location_of(resp) == std::optional("https://www.fastly.com"));
}

SECTION("accepts a std::string_view over a non-null-terminated buffer") {
std::string buf{"https://www.fastly.com/path-and-then-some"};
auto resp{Response::redirect(std::string_view(buf).substr(0, 22))};
REQUIRE(location_of(resp) == std::optional("https://www.fastly.com"));
}
}

// Required due to https://github.com/WebAssembly/wasi-libc/issues/485
#include <catch2/catch_session.hpp>
int main(int argc, char *argv[]) { return Catch::Session().run(argc, argv); }
Loading