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: u16HTTP status code.
headers: Vec<(String, String)>Headers in the order received, names lowercase.
body: StringBody as text (invalid UTF-8 replaced); empty for HEAD, 204 and 304.
Implementations§
Source§impl Response
impl Response
Sourcepub fn header(&self, name: &str) -> Option<&str>
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"));Sourcepub fn location(&self) -> Option<&str>
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"));Sourcepub fn json<T: DeserializeOwned>(&self) -> T
pub fn json<T: DeserializeOwned>(&self) -> T
Sourcepub fn assert_status(&self, status: u16) -> &Self
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);Sourcepub fn assert_success(&self) -> &Self
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();Sourcepub fn assert_redirect_to(&self, location: &str) -> &Self
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");Sourcepub fn assert_contains(&self, text: &str) -> &Self
pub fn assert_contains(&self, text: &str) -> &Self
Asserts the body contains text (HTML-escaped as templates escape it: ' is ').
§Examples
let response = ocre::testing::Response { status: 200, headers: vec![], body: "<h1>Posts</h1>".into() };
response.assert_contains("<h1>Posts</h1>");Sourcepub fn assert_not_contains(&self, text: &str) -> &Self
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");Sourcepub fn assert_header(&self, name: &str, value: &str) -> &Self
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");