There are three important concepts in this wrapper: request matcher, response hander, and resource.
Request matcher: sits in the when clause, used to match against requests that server received, once request matched, moco server will respond.
Response handler: sits in the respond clause, used to define which should be responded to client once request matched.
Resource: Any thing which can be matched against or any thing which can be send back to client could be considered as a resource.
Current moco support two global configurations: file root and context.
import org.treppo.mocoscala.dsl.Moco._
server(port) configs {
fileRoot("src/test/resources")
}
server(port) configs {
context("/hello")
}
match by URI
server(port) when {
uri("/hello")
}
match URI by Regex
server(port) when {
uri matched "/hello.+"
}
chain multiple matchers with same response
server(port) when {
uri("/hello") and method("post")
} respond {
text("world")
}
chain multiple matchers with different response
server(port) when {
method("get")
} respond {
text("get")
} when {
method("post")
} respond {
text("post")
}
server(port) when {
method("get")
}
match by exact body text
server(port) when {
text("foo")
}
match body text by regex
server(port) when {
text matched "hello.+"
}
match content using a file
server(port) when {
file(getClass.getResource("/foo.request").getPath)
}
match by exact value
server(port) when {
query("foo") === "bar"
}
match value by regex
server(port) when {
query("foo") matched ".+bar"
}
match by exact header value
server(port) when {
header("Content-Type") === "application/json"
}
match header values by regex
server(port) when {
header("Content-Type") matched ".+json"
}
server(port) when {
version("HTTP/1.0")
}
match by exact value
server(port) when {
cookie("foo") === "bar"
}
match cookie value by regex:
server(port) when {
cookie("foo") matched ".+bar"
}
match by exact form value
server(port) when {
form("foo") === "bar"
}
match value by regex
server(port) when {
form("foo") matched "ba.+"
}
match by exact xml body
server(port) when {
xml("<body>something</body>")
}
match content using a file
server(port) when {
xml(file(getClass.getResource("/foo_request.xml").getPath))
}
match by exact xpath value
server(port) when {
xpath("/request/parameters/id/text()") === "foo"
}
match xpath value by regex
server(port) when {
xpath("/request/parameters/id/text()") matched "fo.+"
}
server(port) when {
json("{\"foo\": \"bar\"}")
}
match content using a file
server(port) when {
json(file(getClass.getResource("/foo_request.json").getPath))
}
match by exact json path value
server(port) when {
jsonPath("$.foo") === "bar"
}
match json value by regex
server(port) when {
jsonPath("$.foo") matched ".+bar"
}
respond {
text("foo")
}
respond {
file("foo.req")
}
respond {
headers("Content-Type" -> "json", "Accept" -> "html")
}
respond {
cookie("foo" -> "bar")
}
respond {
status 200
}
respond {
version("HTTP/1.0")
}
Respond with a specified url, just like a proxy.
respond {
proxy("http://example.com")
}
Proxy also supports failover
respond {
proxy("http://example.com") {
failover("failover.json")
}
}
Supports playback saving remote request and response into local file.
respond {
proxy("http://example.com") {
playback("playback.json")
}
}
Proxy also support proxying a batch of URLs in the same context
server(port) when {
method("GET") and uri matched "/proxy/.*"
} respond {
proxy {
from("/proxy") to "http://localhost:9090/target"
}
}
You can simply redirect a request to a different location:
server(port) when {
uri("/redirect")
} respond {
redirectTo("/target")
}
You can setup an attachment as response
respond {
attachment("filename", file("filepath"))
}
You can simulate a slow response:
import scala.concurrent.duration.DurationInt
respond {
latency(2.seconds)
}
You can simulate a sequence of responses:
respond {
seq("foo", "bar", "blah")
}
You can specify a subsequent action once the response was sent:
server(port) on {
complete{
get("http://another_site")
}
}
You can use the async api to fire events asynchronsously
server(port) on {
complete {
async {
get("http://another_site")
}
}
}
server(port) when {
uri("/not-exits")
} respond {
status(400) and text("BAD REQUEST")
}
server(port) when {
method("get")
} respond {
text("get")
} when {
method("post")
} respond {
text("post")
}