Skip to content

Latest commit

 

History

History
417 lines (334 loc) · 5.92 KB

api.md

File metadata and controls

417 lines (334 loc) · 5.92 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 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.

Config Apis:

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

file root
import org.treppo.mocoscala.dsl.Moco._

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

Request matchers

URI matchers

match by URI

server(port) when {
  uri("/hello")
}

match URI by Regex

server(port) when {
  uri matched "/hello.+"
}

Multiple matchers

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")
}
Request method matcher
server(port) when {
  method("get")
}
Content matchers

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)
}
Query parameter matchers

match by exact value

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

match value by regex

server(port) when {
   query("foo") matched ".+bar"
}
Header matchers

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"
}
HTTP version matcher
server(port) when {
  version("HTTP/1.0")
}
Cookie matchers

match by exact value

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

match cookie value by regex:

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

match by exact form value

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

match value by regex

server(port) when {
  form("foo") matched "ba.+"
}
XML body matchers

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))
}
Xpath matchers

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

match content using a file

server(port) when {
  json(file(getClass.getResource("/foo_request.json").getPath))
}
Jsonpath matchers

match by exact json path value

server(port) when {
  jsonPath("$.foo") === "bar"
}

match json value by regex

server(port) when {
  jsonPath("$.foo") matched ".+bar"
}

Response Apis:

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

Respond with a specified url, just like a proxy.

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

Proxy also supports failover

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

Supports playback saving remote request and response into local file.

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

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

You can simply redirect a request to a different location:

server(port) when {
  uri("/redirect")
} respond {
  redirectTo("/target")
}
Attachment

You can setup an attachment as response

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

You can simulate a slow response:

import scala.concurrent.duration.DurationInt

respond {
  latency(2.seconds)
}
Sequence

You can simulate a sequence of responses:

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

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

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

You can use the async api to fire events asynchronsously

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

Multiple responses

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

Multiple behaviours

server(port) when {
  method("get")
} respond {
  text("get")
} when {
  method("post")
} respond {
  text("post")
}