Struct http_test_server::TestServer
source · pub struct TestServer { /* private fields */ }
Expand description
Controls the listener life cycle and creates new resources
Implementations§
source§impl TestServer
impl TestServer
sourcepub fn new() -> Result<TestServer, Error>
pub fn new() -> Result<TestServer, Error>
Creates a listener that is bounded to a free port in localhost. Listener is closed when the value is dropped.
Any request for non-defined resources will return 404.
let server = TestServer::new().unwrap();
sourcepub fn new_with_port(port: u16) -> Result<TestServer, Error>
pub fn new_with_port(port: u16) -> Result<TestServer, Error>
Same behaviour as new
, but tries to bound to given port instead of looking for a free one.
let server = TestServer::new_with_port(8080).unwrap();
sourcepub fn port(&self) -> u16
pub fn port(&self) -> u16
Returns associated port number.
let server = TestServer::new().unwrap();
assert!(server.port() > 0);
sourcepub fn close(&self)
pub fn close(&self)
Closes listener. Server stops receiving connections. Do nothing if listener is already closed.
In most the cases this method is not required as the listener is automatically closed when the value is dropped.
let server = TestServer::new().unwrap();
server.close();
sourcepub fn create_resource(&self, uri: &str) -> Resource
pub fn create_resource(&self, uri: &str) -> Resource
Creates a new resource. By default resources answer “200 Ok”.
Check Resource
for all possible configurations.
let server = TestServer::new().unwrap();
let resource = server.create_resource("/user/settings");
sourcepub fn requests(&self) -> Receiver<Request>
pub fn requests(&self) -> Receiver<Request>
Retrieves information on new requests.
let server = TestServer::new().unwrap();
for request in server.requests().iter() {
assert_eq!(request.url, "/endpoint");
assert_eq!(request.method, "GET");
assert_eq!(request.headers.get("Content-Type").unwrap(), "text");
}
Trait Implementations§
Auto Trait Implementations§
impl RefUnwindSafe for TestServer
impl Send for TestServer
impl Sync for TestServer
impl Unpin for TestServer
impl UnwindSafe for TestServer
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more