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
12 changes: 12 additions & 0 deletions include/fastly/http/response.h
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,18 @@ class Response {
fastly::expected<std::optional<std::string>>
remove_header(std::string_view name);

/// Get the HTTP status code of the response.
///
/// # Examples
///
/// ```cpp
/// auto resp{fastly::Response::from_status(
/// fastly::http::StatusCode::NOT_FOUND)};
/// assert(resp.get_status() == fastly::http::StatusCode::NOT_FOUND);
/// assert(resp.get_status().as_code() == 404);
/// ```
StatusCode get_status();

/// Builder-style equivalent of `Response::set_status()`.
Response with_status(StatusCode status) &&;

Expand Down
2 changes: 1 addition & 1 deletion include/fastly/http/status_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class StatusCode {
/// The method validates the correctness of the supplied uint16_t. It must be
/// greater or equal to 100 and less than 1000, or this method will return
/// `std::nullopt`.
std::optional<StatusCode> from_code(uint16_t code);
static std::optional<StatusCode> from_code(uint16_t code);

/// Returns the `uint16_t` corresponding to this `StatusCode`.
uint16_t as_code();
Expand Down
4 changes: 4 additions & 0 deletions src/cpp/http/response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ Response::remove_header(std::string_view name) {
}
}

StatusCode Response::get_status() {
return StatusCode(this->res->get_status());
}

void Response::set_status(StatusCode status) {
this->res->set_status(status.as_code());
}
Expand Down
4 changes: 4 additions & 0 deletions src/http/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ impl Response {
.is_some()
}

pub fn get_status(&self) -> u16 {
self.0.get_status().as_u16()
}

pub fn set_status(&mut self, status: u16) {
self.0.set_status(status);
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ mod ffi {
out: Pin<&mut CxxString>,
mut err: Pin<&mut *mut FastlyError>,
) -> bool;
fn get_status(&self) -> u16;
fn set_status(&mut self, status: u16);
fn get_backend_name(&self, out: Pin<&mut CxxString>) -> bool;
fn get_backend(&self) -> *mut Backend;
Expand Down
48 changes: 48 additions & 0 deletions test/response.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <catch2/catch_test_macros.hpp>
#include <fastly/http/response.h>
#include <fastly/http/status_code.h>

using namespace fastly::http;

TEST_CASE("Response::get_status", "[response]") {
SECTION("new responses default to 200 OK") {
Response resp;
REQUIRE(resp.get_status().as_code() == 200);
REQUIRE(resp.get_status() == StatusCode::OK);
}

SECTION("round-trips from_status") {
REQUIRE(Response::from_status(404).get_status().as_code() == 404);
REQUIRE(Response::from_status(StatusCode::NOT_FOUND).get_status() ==
StatusCode::NOT_FOUND);
}

SECTION("reflects set_status") {
Response resp;
resp.set_status(StatusCode::IM_A_TEAPOT);
REQUIRE(resp.get_status().as_code() == 418);
}

SECTION("reflects with_status") {
auto resp{Response().with_status(StatusCode::SERVICE_UNAVAILABLE)};
REQUIRE(resp.get_status().as_code() == 503);
}

SECTION("interoperates with StatusCode helpers") {
REQUIRE(Response::from_status(204).get_status().is_success());
REQUIRE(Response::from_status(StatusCode::PERMANENT_REDIRECT)
.get_status()
.is_redirection());
REQUIRE(Response::from_status(500).get_status().is_server_error());
}
}

TEST_CASE("StatusCode::from_code", "[response]") {
REQUIRE(StatusCode::from_code(404) == std::optional(StatusCode::NOT_FOUND));
REQUIRE(StatusCode::from_code(99) == std::nullopt);
REQUIRE(StatusCode::from_code(1000) == std::nullopt);
}

// 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