Skip to content

Latest commit

 

History

History
369 lines (301 loc) · 4.83 KB

api.md

File metadata and controls

369 lines (301 loc) · 4.83 KB

Concept

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.

Config Apis:

Current moco support two global configurations: file root and context.

file root
server configs {
  fileRoot("src/test/resources")
}
context
server configs {
  context("/hello")
}

Matcher Apis:

Uri

match by uri

when {
    uri("/hello")
}

or match by regex

when {
    uri matched "/hello.+"
}
matcher-method
when {
    method("get")
}
Text

by value

when {
    text("foo")
}

or by regex

when {
    text matched "hello.+"
}
File
when {
    file("foo.req")
}
Version
when {
    version("HTTP/1.0")
}
Header

match by value

when {
    header("Content-Type") === "application/json"
}

or by regex

when {
    header("Content-Type") matched ".+json"
}
Query

match by value

when {
     query("foo") === "bar"
}

or by regex:

when {
     query("foo") matched ".+bar"
}
Cookie

match by value

when {
    cookie("foo") === "bar"
}

or by regex:

when {
    cookie("foo") matched ".+bar"
}
Form

you can do exact match by form value

when {
  form("foo") === "bar"
}

or by match value with regex

when {
  form("foo") matched "ba.+"
}
Xml
when {
  xml("<body>something</body>")
}
Xpath

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.+"
}
Json
when {
  json("{\"foo\": \"bar\"}")
}
Jsonpath

similar to xpath.

Response Apis:

Text
then {
    text("foo")
}
File
then {
    file("foo.req")
}
Header
then {
    headers("Content-Type" -> "json", "Accept" -> "html")
}
Cookie
then {
    cookie("foo" -> "bar")
}
Status
then {
    status 200
}
Version
then {
    version("HTTP/1.0")
}
Proxy Apis
Single URL

We can response with a specified url, just like a proxy.

then {
  proxy("http://example.com")
}
Failover

Proxy also support failover

then {
  proxy("http://example.com") {
    failover("failover.json")
  }
}
Playback

We also supports playback with save remote request and resonse into local file.

then {
  proxy("http://example.com") {
    playback("playback.json")
  }
}
Batch URLs

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")
  }
}
Redirect Api:

You can simply redirect a request to a different location:

when {
  uri("/redirect")
} then {
  redirectTo("/target")
}
Attachment

You can setup a attachment as response

then {
  attachment("filename", file("filepath"))
}
Latency

You can simulate a slow response:

then {
  //you need to import scala.concurrent.duration.Duration to have this syntax sugar
  latency(2 seconds)
}
Sequence

You can simulate a sequence of response:

then {
  seq("foo", "bar", "blah")
}
Event

You can specify a subsequent action once the response was sent:

server on {
    complete{
      get("http"//another_site)
    }
}
Asynchronous

You can use async api to fire event asynchronsously

server on {
    complete {
      async {
        get("http"//another_site)
      }
    }
}

Advanced Usage

to enable advanced usage, you have to import conversions as follow:

import com.github.nicholasren.moco.dsl.Conversions._

Multiple matchers

when {
  uri("/hello") and method("post")
} then {
  text("world")
}

Multiple responses

when {
  uri("/not-exits")
} then {
  status(400) and text("BAD REQUEST")
}

Multiple behaviours

when {
  method("get")
} then {
  text("get")
} when {
  method("post")
} then {
  text("post")
}