[][src]Struct wiremock::ResponseTemplate

pub struct ResponseTemplate { /* fields omitted */ }

The blueprint for the response returned by a MockServer when a Mock matches on an incoming request.

Implementations

impl ResponseTemplate[src]

pub fn new<S>(s: S) -> Self where
    S: TryInto<StatusCode>,
    <S as TryInto<StatusCode>>::Error: Debug
[src]

Start building a ResponseTemplate specifying the status code of the response.

pub fn append_header<K, V>(self, key: K, value: V) -> Self where
    K: TryInto<HeaderName>,
    <K as TryInto<HeaderName>>::Error: Debug,
    V: TryInto<HeaderValue>,
    <V as TryInto<HeaderValue>>::Error: Debug
[src]

Append a header value to list of headers with key as header name.

Unlike insert_header, this function will not override the contents of a header:

  • if there are no header values with key as header name, it will insert one;
  • if there are already some values with key as header name, it will append to the existing list.

pub fn insert_header<K, V>(self, key: K, value: V) -> Self where
    K: TryInto<HeaderName>,
    <K as TryInto<HeaderName>>::Error: Debug,
    V: TryInto<HeaderValue>,
    <V as TryInto<HeaderValue>>::Error: Debug
[src]

Insert a header value with key as header name.

This function will override the contents of a header:

  • if there are no header values with key as header name, it will insert one;
  • if there are already some values with key as header name, it will drop them and start a new list of header values, containing only value.

pub fn set_body<B>(self, body: B) -> Self where
    B: TryInto<Vec<u8>>,
    <B as TryInto<Vec<u8>>>::Error: Debug
[src]

👎 Deprecated since 0.1.1:

Please use set_body_bytes instead.

Set the response body with bytes.

It sets "Content-Type" to "application/octet-stream".

pub fn set_body_bytes<B>(self, body: B) -> Self where
    B: TryInto<Vec<u8>>,
    <B as TryInto<Vec<u8>>>::Error: Debug
[src]

Set the response body with bytes.

It sets "Content-Type" to "application/octet-stream".

To set a body with bytes but a different "Content-Type" set_body_raw can be used.

pub fn set_body_json<B: Serialize>(self, body: B) -> Self[src]

Set the response body from a JSON-serializable value.

It sets "Content-Type" to "application/json".

pub fn set_body_string<T>(self, body: T) -> Self where
    T: TryInto<String>,
    <T as TryInto<String>>::Error: Debug
[src]

Set the response body to a string.

It sets "Content-Type" to "text/plain".

pub fn set_body_raw<B>(self, body: B, mime: &str) -> Self where
    B: TryInto<Vec<u8>>,
    <B as TryInto<Vec<u8>>>::Error: Debug
[src]

Set a raw response body. The mime type needs to be set because the raw body could be of any type.

Example:

use surf::mime;
use wiremock::{MockServer, Mock, ResponseTemplate};
use wiremock::matchers::method;

mod external {
    // This could be a method of a struct that is
    // implemented in another crate and the struct
    // does not implement Serialize.
    pub fn body() -> Vec<u8>{
        r#"{"hello": "world"}"#.as_bytes().to_owned()
    }
}

#[async_std::main]
async fn main() {
    // Arrange
    let mock_server = MockServer::start().await;
    let template = ResponseTemplate::new(200).set_body_raw(
        external::body(),
        "application/json"
    );
    Mock::given(method("GET"))
        .respond_with(template)
        .mount(&mock_server)
        .await;

    // Act
    let mut res = surf::get(&mock_server.uri())
        .await
        .unwrap();
    let body = res.body_string()
        .await
        .unwrap();

    // Assert
    assert_eq!(body, r#"{"hello": "world"}"#);
    assert_eq!(res.mime(), Some(mime::APPLICATION_JSON));
}

pub fn set_delay(self, delay: Duration) -> Self[src]

By default the [MockServer] tries to fulfill incoming requests as fast as possible.

You can use set_delay to introduce an artificial delay to simulate the behaviour of a real server with a non-neglibible latency.

In particular, you can use it to test the behaviour of your timeout policies.

Example:

use wiremock::{MockServer, Mock, ResponseTemplate};
use wiremock::matchers::method;
use std::time::Duration;
use async_std::prelude::FutureExt;

#[async_std::main]
async fn main() {
    // Arrange
    let mock_server = MockServer::start().await;
    let delay = Duration::from_secs(1);
    let template = ResponseTemplate::new(200).set_delay(delay.clone());
    Mock::given(method("GET"))
        .respond_with(template)
        .mount(&mock_server)
        .await;

    // Act
    let mut res = surf::get(&mock_server.uri())
        // Shorter than the time taken by the MockServer
        // to return a response
        .timeout(delay / 3)
        .await;

    // Assert - Timeout error!
    assert!(res.is_err());
}

Trait Implementations

impl Clone for ResponseTemplate[src]

impl Debug for ResponseTemplate[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> AsAny for T where
    T: Any
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Message for T where
    T: Any + Send + Sync + Debug
[src]

impl<T> State for T where
    T: Send + Sync + 'static, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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