std.net.http.test.Server
type pub ServerAn HTTP server for generating mock responses and recording unmocked requests.
Static methods
new
Show source codeHide source code
fn pub static new(test: mut Test, builder: fn (mut Server)) -> RunningServer {
let srv = Self(
id: 0,
matchers: recover Matchers.new,
responses: recover Map.new,
locations: [],
)
builder.call(srv)
srv.start(test)
}fn pub static new(test: mut Test, builder: fn (mut Server)) -> RunningServerReturns and starts a new Server.
The test argument is the currently running std.test.Test. This is used
for recording any failures (e.g. unmatched requests).
The builder argument is a closure used to populate the Server with mock
responses.
Instance methods
delete
Show source codeHide source code
fn pub mut delete(path: String) -> Mock {
request(Method.Delete, path)
}fn pub mut delete(path: String) -> MockReturns a Mock for a DELETE request.
Refer to the documentation of Server.request for more details.
get
Show source codeHide source code
fn pub mut get(path: String) -> Mock {
request(Method.Get, path)
}fn pub mut get(path: String) -> MockReturns a Mock for a GET request.
Refer to the documentation of Server.request for more details.
head
Show source codeHide source code
fn pub mut head(path: String) -> Mock {
request(Method.Head, path)
}fn pub mut head(path: String) -> MockReturns a Mock for a HEAD request.
Refer to the documentation of Server.request for more details.
post
Show source codeHide source code
fn pub mut post(path: String) -> Mock {
request(Method.Post, path)
}fn pub mut post(path: String) -> MockReturns a Mock for a POST request.
Refer to the documentation of Server.request for more details.
put
Show source codeHide source code
fn pub mut put(path: String) -> Mock {
request(Method.Put, path)
}fn pub mut put(path: String) -> MockReturns a Mock for a PUT request.
Refer to the documentation of Server.request for more details.
request
Show source codeHide source code
fn pub mut request(method: Method, path: String) -> Mock {
Mock.new(self, method, path)
}fn pub mut request(method: Method, path: String) -> MockReturns a Mock for the given request method and path.
The path argument is the relative request path, excluding any query
string parameters.
Panics
This method panics if the path argument is not a valid path (i.e. it
contains characters that must be percent encoded but aren't).
Examples
This defines a mock GET request:
import std.net.http (Method)
import std.net.http.server (Response)
import std.net.http.test (Server)
import std.test (Tests)
type async Main {
fn async main {
let tests = Tests.new
tests.test('Example test', fn (t) {
let _server = Server.new(t, fn (srv) {
srv.request(Method.Get, '/').then(fn { Response.new.string('hello') })
})
})
tests.run
}
}
For common request methods there are also dedicated methods on this type:
import std.net.http.server (Response)
import std.net.http.test (Server)
import std.test (Tests)
type async Main {
fn async main {
let tests = Tests.new
tests.test('Example test', fn (t) {
let _server = Server.new(t, fn (srv) {
srv.get('/').then(fn { Response.new.string('hello') })
})
})
tests.run
}
}