Skip to main content

Response

Struct Response 

Source
pub struct Response {
    pub status: u16,
    pub headers: Vec<(String, String)>,
    pub body: String,
}
Expand description

A response in a request test, with chainable assertions (Rails’ assert_response, assert_redirected_to, assert_match).

Assertions panic with the status and the start of the body, and return the response so they chain.

§Examples

let mut client = ocre::testing::Client::new();
client.get("/posts").assert_status(200).assert_contains("<h1>Posts</h1>");

Fields§

§status: u16

HTTP status code.

§headers: Vec<(String, String)>

Headers in the order received, names lowercase.

§body: String

Body as text (invalid UTF-8 replaced); empty for HEAD, 204 and 304.

Implementations§

Source§

impl Response

Source

pub fn header(&self, name: &str) -> Option<&str>

The first value of the header name (any case).

§Examples
let response = ocre::testing::Response { status: 200, headers: vec![("content-type".into(), "text/html".into())], body: String::new() };
assert_eq!(response.header("Content-Type"), Some("text/html"));
Source

pub fn location(&self) -> Option<&str>

The Location header of a redirect.

§Examples
let response = ocre::testing::Response { status: 303, headers: vec![("location".into(), "/posts/1".into())], body: String::new() };
assert_eq!(response.location(), Some("/posts/1"));
Source

pub fn json<T: DeserializeOwned>(&self) -> T

The body parsed as JSON (Rails’ response.parsed_body).

§Panics

When the body is not JSON of type T.

§Examples
let response = ocre::testing::Response { status: 200, headers: vec![], body: r#"{"id":1}"#.into() };
assert_eq!(response.json::<ocre::serde_json::Value>()["id"], 1);
Source

pub fn assert_status(&self, status: u16) -> &Self

Asserts the status code (Rails’ assert_response 201).

§Examples
let response = ocre::testing::Response { status: 404, headers: vec![], body: String::new() };
response.assert_status(404);
Source

pub fn assert_success(&self) -> &Self

Asserts a 2xx status (Rails’ assert_response :success).

§Examples
let response = ocre::testing::Response { status: 204, headers: vec![], body: String::new() };
response.assert_success();
Source

pub fn assert_redirect_to(&self, location: &str) -> &Self

Asserts a 3xx redirect to location (Rails’ assert_redirected_to).

§Examples
let response = ocre::testing::Response { status: 303, headers: vec![("location".into(), "/posts/1".into())], body: String::new() };
response.assert_redirect_to("/posts/1");
Source

pub fn assert_contains(&self, text: &str) -> &Self

Asserts the body contains text (HTML-escaped as templates escape it: ' is &#39;).

§Examples
let response = ocre::testing::Response { status: 200, headers: vec![], body: "<h1>Posts</h1>".into() };
response.assert_contains("<h1>Posts</h1>");
Source

pub fn assert_not_contains(&self, text: &str) -> &Self

Asserts the body does not contain text.

§Examples
let response = ocre::testing::Response { status: 200, headers: vec![], body: "<h1>Posts</h1>".into() };
response.assert_not_contains("Error");
Source

pub fn assert_header(&self, name: &str, value: &str) -> &Self

Asserts the header name has value.

§Examples
let response = ocre::testing::Response { status: 200, headers: vec![("content-type".into(), "application/json".into())], body: String::new() };
response.assert_header("Content-Type", "application/json");

Trait Implementations§

Source§

impl Clone for Response

Source§

fn clone(&self) -> Response

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Response

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<S, T> Upcast<T> for S
where T: UpcastFrom<S> + ?Sized, S: ?Sized,

Source§

fn upcast(&self) -> &T
where Self: ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider ref type within the Wasm bindgen generics type system. Read more
Source§

fn upcast_into(self) -> T
where Self: Sized + ErasableGeneric, T: Sized + ErasableGeneric<Repr = Self::Repr>,

Perform a zero-cost type-safe upcast to a wider type within the Wasm bindgen generics type system. Read more
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V