[−][src]Struct wiremock::MockServer
An HTTP web-server running in the background to behave as one of your dependencies using Mock
s
for testing purposes.
Each instance of MockServer
is fully isolated: start
takes care of finding a random port
available on your local machine which is assigned to the new MockServer
.
Best practices
You should use one instance of MockServer
for each REST API that your application interacts
with and needs mocking for testing purposes.
To ensure full isolation and no cross-test interference, MockServer
s shouldn't be
shared between tests. Instead, MockServer
s should be created in the test where they are used.
You can register as many Mock
s as your scenario requires on a MockServer
.
Implementations
impl MockServer
[src]
pub async fn start() -> Self
[src]
Start a new instance of a MockServer
listening on a random port.
Each instance of MockServer
is fully isolated: start
takes care of finding a random port
available on your local machine which is assigned to the new MockServer
.
You should use one instance of MockServer
for each REST API that your application interacts
with and needs mocking for testing purposes.
Example:
use wiremock::{MockServer, Mock, ResponseTemplate}; use wiremock::matchers::method; #[async_std::main] async fn main() { // Arrange let mock_server_one = MockServer::start().await; let mock_server_two = MockServer::start().await; assert!(mock_server_one.address() != mock_server_two.address()); let mock = Mock::given(method("GET")).respond_with(ResponseTemplate::new(200)); // Registering the mock with the first mock server - it's now effective! // But it *won't* be used by the second mock server! mock_server_one.register(mock).await; // Act let status = surf::get(&mock_server_one.uri()) .await .unwrap() .status(); assert_eq!(status, 200); // This would have matched our mock, but we haven't registered it for `mock_server_two`! // Hence it returns a 404, the default response when no mocks matched on the mock server. let status = surf::get(&mock_server_two.uri()) .await .unwrap() .status(); assert_eq!(status, 404); }
pub async fn start_on(listener: TcpListener) -> Self
[src]
Start a new instance of a MockServer
listening on the
TcpListener
passed as argument.
Example:
use wiremock::MockServer; #[async_std::main] async fn main() { // Arrange let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); let expected_server_address = listener .local_addr() .expect("Failed to get server address."); let mock_server = MockServer::start_on(listener).await; assert_eq!(&expected_server_address, mock_server.address()); }
pub async fn register<'_>(&'_ self, mock: Mock)
[src]
Register a Mock
on an instance of MockServer
.
Be careful! Mock
s are not effective until they are mount
ed or register
ed on a MockServer
.
register
is an asynchronous method, make sure to .await
it!
Example:
use wiremock::{MockServer, Mock, ResponseTemplate}; use wiremock::matchers::method; #[async_std::main] async fn main() { // Arrange let mock_server = MockServer::start().await; let response = ResponseTemplate::new(200); let mock = Mock::given(method("GET")).respond_with(response.clone()); // Registering the mock with the mock server - it's now effective! mock_server.register(mock).await; // We won't register this mock instead. let unregistered_mock = Mock::given(method("GET")).respond_with(response); // Act let status = surf::get(&mock_server.uri()) .await .unwrap() .status(); assert_eq!(status, 200); // This would have matched `unregistered_mock`, but we haven't registered it! // Hence it returns a 404, the default response when no mocks matched on the mock server. let status = surf::post(&mock_server.uri()) .await .unwrap() .status(); assert_eq!(status, 404); }
pub async fn reset<'_>(&'_ self)
[src]
Drop all mounted Mock
s from an instance of MockServer
.
Example:
use wiremock::{MockServer, Mock, ResponseTemplate}; use wiremock::matchers::method; #[async_std::main] async fn main() { // Arrange let mock_server = MockServer::start().await; let response = ResponseTemplate::new(200); Mock::given(method("GET")).respond_with(response).mount(&mock_server).await; // Act let status = surf::get(&mock_server.uri()) .await .unwrap() .status(); assert_eq!(status, 200); // Reset the server mock_server.reset().await; // This would have matched our mock, but we have dropped it resetting the server! let status = surf::post(&mock_server.uri()) .await .unwrap() .status(); assert_eq!(status, 404); }
pub fn uri(&self) -> String
[src]
Return the base uri of this running instance of MockServer
, e.g. http://127.0.0.1:4372
.
Use this method to compose uris when interacting with this instance of MockServer
via
an HTTP client.
Example:
use wiremock::MockServer; #[async_std::main] async fn main() { // Arrange - no mocks mounted let mock_server = MockServer::start().await; // Act let uri = format!("{}/health_check", &mock_server.uri()); let status = surf::get(uri).await.unwrap().status(); // Assert - default response assert_eq!(status, 404); }
pub fn address(&self) -> &SocketAddr
[src]
Return the socket address of this running instance of MockServer
, e.g. 127.0.0.1:4372
.
Use this method to interact with the MockServer
using TcpStream
s.
Example:
use wiremock::MockServer; use std::net::TcpStream; #[async_std::main] async fn main() { // Act - the server is started let mock_server = MockServer::start().await; // Assert - we can connect to it assert!(TcpStream::connect(mock_server.address()).is_ok()); }
Trait Implementations
impl Drop for MockServer
[src]
Auto Trait Implementations
impl !RefUnwindSafe for MockServer
[src]
impl Send for MockServer
[src]
impl Sync for MockServer
[src]
impl Unpin for MockServer
[src]
impl !UnwindSafe for MockServer
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,
pub fn vzip(self) -> V
impl<T> WithSubscriber for T
[src]
pub fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
[src]
S: Into<Dispatch>,