pub struct LocalServer {
pub uri: String,
pub address: String,
pub response: String,
}
Expand description
A local server that can be used to listen for requests.
§Examples
use drive_v3::LocalServer;
let local_server = LocalServer::new();
let received_request = local_server.wait_for_request().expect("failed to listen for requests");
// The local server is now running and waiting for a request,
// run "curl http://localhost:7878/my-request-url" to send one.
assert_eq!(received_request, "http://localhost:7878/my-request-url");
Fields§
§uri: String
URI that a website can make requests to in order for them to be received by the server. (default: “http://localhost:7878”)
address: String
Address where the server listens for requests. (default: “localhost:7878”)
response: String
Response returned by the server after receiving a request. (default: “request processed!”)
Implementations§
Source§impl LocalServer
impl LocalServer
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new LocalServer
with default values.
Sourcepub fn at<T: AsRef<str>>(self, address: T, port: usize) -> Self
pub fn at<T: AsRef<str>>(self, address: T, port: usize) -> Self
Sets the address
and port
values for a LocalServer
.
§Examples
use drive_v3::LocalServer;
let local_server = LocalServer::new().at("127.0.0.1", 8080);
assert_eq!(local_server.uri, "http://127.0.0.1:8080");
assert_eq!(local_server.address, "127.0.0.1:8080");
Sourcepub fn with_response<T: AsRef<str>>(self, response: T) -> Self
pub fn with_response<T: AsRef<str>>(self, response: T) -> Self
Sets the response
that the LocalServer
will return after successfully receiving a request.
§Examples
use drive_v3::LocalServer;
let local_server = LocalServer::new().with_response("this is my response");
assert_eq!( local_server.response, String::from("this is my response") )
Sourcepub fn wait_for_request(&self) -> Result<String>
pub fn wait_for_request(&self) -> Result<String>
Runs a LocalServer
and binds a listener to its address
, after receiving
a request the server will close and return the URI of the
received request.
§Examples
use drive_v3::LocalServer;
let local_server = LocalServer::new();
let received_request = local_server.wait_for_request()?;
// The local server is now running and waiting for a request,
// run "curl http://localhost:7878/my-request-url" to send one.
assert_eq!(received_request, "http://localhost:7878/my-request-url");
§Errors
- an
IO
error, if the server fails to to bind a TCP listener itsaddress
or if the incoming request is invalid. - a
LocalServer
error, if theLocalServer
failed to listen for incoming requests.
Trait Implementations§
Source§impl Debug for LocalServer
impl Debug for LocalServer
Source§impl Default for LocalServer
impl Default for LocalServer
Source§impl PartialEq for LocalServer
impl PartialEq for LocalServer
impl Eq for LocalServer
impl StructuralPartialEq for LocalServer
Auto Trait Implementations§
impl Freeze for LocalServer
impl RefUnwindSafe for LocalServer
impl Send for LocalServer
impl Sync for LocalServer
impl Unpin for LocalServer
impl UnwindSafe for LocalServer
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
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.