Struct gotham::test::TestServer
[−]
[src]
pub struct TestServer<S> { /* fields omitted */ }
The TestServer
type, which is used as a harness when writing test cases for Hyper services
(which Gotham's Router
is). An instance of TestServer
is run single-threaded and
asynchronous, and only accessible by a client returned from the TestServer
.
Examples
use gotham::test::TestServer; let mut test_server = TestServer::new(|| Ok(MyService::new())).unwrap(); let uri = "http://localhost/".parse().unwrap(); let client_addr = "127.0.0.1:15100".parse().unwrap(); let future = test_server.client(client_addr).unwrap().get(uri); let response = test_server.run_request(future).unwrap(); assert_eq!(response.status(), StatusCode::Accepted);
Methods
impl<S> TestServer<S> where
S: NewService<Request = Request, Response = Response, Error = Error>,
S::Instance: 'static,
[src]
S: NewService<Request = Request, Response = Response, Error = Error>,
S::Instance: 'static,
fn new(new_service: S) -> Result<TestServer<S>, Error>
[src]
Creates a TestServer
instance for the service spawned by new_service
. This server has
the same guarantee given by hyper::server::Http::bind
, that a new service will be spawned
for each connection.
fn timeout(self, t: u64) -> TestServer<S>
[src]
Sets the request timeout to t
seconds and returns a new TestServer
. The default timeout
value is 10 seconds.
fn client(&self, client_addr: SocketAddr) -> Result<Client<TestConnect>>
[src]
Returns a client connected to the TestServer
. The transport is handled internally, and
the server will see client_addr
as the source address for the connection. The
client_addr
can be any value, and need not be contactable.
fn run_request<F>(&mut self, f: F) -> Result<F::Item, TestRequestError> where
F: Future<Error = Error>,
[src]
F: Future<Error = Error>,
Runs the event loop until the response future is completed.
If the future came from a different instance of TestServer
, the event loop will run until
the timeout is triggered.
fn read_body(&mut self, response: Response) -> Result<Vec<u8>>
[src]
Runs the event loop until the response body has been fully read. An Ok(_)
response holds
a buffer containing all bytes of the response body.