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 then 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.
server configs {
fileRoot("src/test/resources")
}
server configs {
context("/hello")
}
match by uri
when {
uri("/hello")
}
or match by regex
when {
uri matched "/hello.+"
}
when {
method("get")
}
by value
when {
text("foo")
}
or by regex
when {
text matched "hello.+"
}
when {
file("foo.req")
}
when {
version("HTTP/1.0")
}
match by value
when {
header("Content-Type") === "application/json"
}
or by regex
when {
header("Content-Type") matched ".+json"
}
match by value
when {
query("foo") === "bar"
}
or by regex:
when {
query("foo") matched ".+bar"
}
match by value
when {
cookie("foo") === "bar"
}
or by regex:
when {
cookie("foo") matched ".+bar"
}
you can do exact match by form value
when {
form("foo") === "bar"
}
or by match value with regex
when {
form("foo") matched "ba.+"
}
when {
xml("<body>something</body>")
}
similarly, you can do exact match by value
when {
xpath("/request/parameters/id/text()") === "foo"
}
or match by regex
when {
xpath("/request/parameters/id/text()") matched "fo.+"
}
when {
json("{\"foo\": \"bar\"}")
}
similar to xpath.
then {
text("foo")
}
then {
file("foo.req")
}
then {
headers("Content-Type" -> "json", "Accept" -> "html")
}
then {
cookie("foo" -> "bar")
}
then {
status 200
}
then {
version("HTTP/1.0")
}
We can response with a specified url, just like a proxy.
then {
proxy("http://example.com")
}
Proxy also support failover
then {
proxy("http://example.com") {
failover("failover.json")
}
}
We also supports playback with save remote request and resonse into local file.
then {
proxy("http://example.com") {
playback("playback.json")
}
}
Proxy also support proxying a batch of URLs in the same context
when {
method("GET") and uri matched "/proxy/.*"
} then {
proxy {
from("/proxy") to ("http://localhost:9090/target")
}
}
You can simply redirect a request to a different location:
when {
uri("/redirect")
} then {
redirectTo("/target")
}
You can setup a attachment as response
then {
attachment("filename", file("filepath"))
}
You can simulate a slow response:
then {
//you need to import scala.concurrent.duration.Duration to have this syntax sugar
latency(2 seconds)
}
You can simulate a sequence of response:
then {
seq("foo", "bar", "blah")
}
You can specify a subsequent action once the response was sent:
server on {
complete{
get("http"//another_site)
}
}
You can use async api to fire event asynchronsously
server on {
complete {
async {
get("http"//another_site)
}
}
}
to enable advanced usage, you have to import conversions as follow:
import com.github.nicholasren.moco.dsl.Conversions._
when {
uri("/hello") and method("post")
} then {
text("world")
}
when {
uri("/not-exits")
} then {
status(400) and text("BAD REQUEST")
}
when {
method("get")
} then {
text("get")
} when {
method("post")
} then {
text("post")
}