[][src]Struct http_test_server::resource::Resource

pub struct Resource { /* fields omitted */ }

Responsible for configuring a resource and interacting with it.

Must be created through TestServer.

By default a resource's method is GET and response is 200 Ok with empty body.

use http_test_server::TestServer;
use http_test_server::http::{Method, Status};

let server = TestServer::new().unwrap();
let resource = server.create_resource("/i-am-a-resource");

resource
    .status(Status::PartialContent)
    .method(Method::POST)
    .body("All good!");

Methods

impl Resource[src]

pub fn status(&self, status_code: Status) -> &Resource[src]

Defines response's HTTP Status .

Refer to custom_status for Statuses not covered by Status.

resource.status(Status::PartialContent);

pub fn custom_status(&self, status_code: u16, description: &str) -> &Resource[src]

Defines a custom HTTP Status to response.

Use it to return HTTP statuses that are not covered by Status.

resource.custom_status(333, "Only Half Beast");

pub fn header(&self, header_name: &str, header_value: &str) -> &Resource[src]

Defines response headers.

Call it multiple times to add multiple headers. If a header is defined twice only the late value is returned.

resource
    .header("Content-Type", "application/json")
    .header("Connection", "Keep-Alive");

pub fn body(&self, content: &'static str) -> &Resource[src]

Defines response's body.

If the response is a stream this value will be sent straight after connection.

Calling multiple times will overwrite the value.

resource.body("this is important!");

pub fn method(&self, method: Method) -> &Resource[src]

Defines HTTP method.

A resource will only respond to one method, however multiple resources with same URL and different methods can be created.

use http_test_server::http::Method;
let resource_put = server.create_resource("/i-am-a-resource");
let resource_post = server.create_resource("/i-am-a-resource");

resource_put.method(Method::PUT);
resource_post.method(Method::POST);

pub fn delay(&self, delay: Duration) -> &Resource[src]

Defines delay to response after client connected

use std::time::Duration;

resource.delay(Duration::from_millis(500));

pub fn stream(&self) -> &Resource[src]

Set response as stream, this means clients won't be disconnected after body is sent and updates can be sent and received.

See also: send, send_line, stream_receiver.

let resource = server.create_resource("/stream");

resource.stream();

resource
    .send_line("some")
    .send_line("data")
    .close_open_connections();

pub fn send(&self, data: &str) -> &Resource[src]

Send data to all connected clients.

See also: send_line, stream.

let resource = server.create_resource("/stream");

resource.stream();

resource
    .send("some")
    .send(" data");

pub fn send_line(&self, data: &str) -> &Resource[src]

Send data to all connected clients. Same as send, but appends \n to data.

See also: stream

let resource = server.create_resource("/stream");

resource.stream();

resource
    .send_line("one line")
    .send_line("another line");

pub fn close_open_connections(&self)[src]

Close all connections with clients.

See also: stream

let resource = server.create_resource("/stream");

resource.stream();

resource.close_open_connections();

pub fn open_connections_count(&self) -> usize[src]

Number of clients connected to stream.

See also: stream

let resource = server.create_resource("/stream");

resource
    .stream()
    .close_open_connections();

assert_eq!(resource.open_connections_count(), 0);

pub fn stream_receiver(&self) -> Receiver<String>[src]

Receives data sent from clients through stream.

See also: stream

let resource = server.create_resource("/stream");
let receiver = resource.stream().stream_receiver();

let new_message = receiver.recv().unwrap();

for message in receiver.iter() {
    println!("Client message: {}", message);
}

pub fn request_count(&self) -> u32[src]

Number of requests received

assert_eq!(resource.request_count(), 0);

Trait Implementations

impl Clone for Resource[src]

fn clone(&self) -> Self[src]

Returns a Resource copy that shares state with other copies.

This is useful when working with same Resource across threads.

impl Debug for Resource[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[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> 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.