DroidsOnRoids/mockwebserver-path-dispatcher

Name: mockwebserver-path-dispatcher

Owner: Droids On Roids

Description: A helper for dispatching MockWebServer responses

Created: 2017-06-14 20:31:36.0

Updated: 2018-03-30 15:09:51.0

Pushed: 2018-03-30 15:10:25.0

Homepage:

Size: 210

Language: Kotlin

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Javadocs Maven Central

MockWebServer path dispatcher

A helper for dispatching MockWebServer responses. It allows to easily mock responses with data stored in YAML files in resources/fixtures/ directory

Motivation
Example

Code with MockWebServer path dispatcher:

pathCondition() {
val dispatcher = FixtureDispatcher()
// match all URLs with path starting with /prefix/ e.g. http://example.test/prefix/
val factory = PathQueryConditionFactory("/prefix/")
// match all URLs with path ending with "suffix" and return response from fixtures/body_path.yaml
dispatcher.putResponse(factory.withPathSuffix("suffix"), "body_path")
dispatcher.putResponse(factory.withPathSuffix("another_suffix"), "json_object")
mockWebServer.setDispatcher(dispatcher)

Example YAML file at resources/fixtures/json_object.yaml:

usCode : 200
ers:
ontent-Type: application/json'
: >
{
  "test": null
}

Instead of defining body in yaml directly you can specify relative path to file with body:

usCode : 404
ers:
ontent-Type: text/plain'
ary: Accept-Encoding"
: body.txt

Code without MockWebServer path dispatcher:

bareMockWebServer() {
val dispatcher = object : Dispatcher() {
    override fun dispatch(request: RecordedRequest): MockResponse {
        val path = request.requestUrl.encodedPath()
        if (path == "/prefix/suffix") {
            return MockResponse()
                    .setResponseCode(404)
                    .addHeader("Content-Type", "text/plain")
                    .addHeader("Vary", "Accept-Encoding")
                    .setBody("""{"test"}""")
        } else if (path == "/prefix/another_suffix") {
            return MockResponse()
                    .setResponseCode(200)
                    .addHeader("Content-Type", "application/json")
                    .setBody("{\n  \"test\": null\n}")
        }
        throw IllegalArgumentException("Unexpected request: $request")
    }
}
mockWebServer.setDispatcher(dispatcher)

See more examples at FunctionalTest.kt

API

PathQueryConditionFactory - when you want to use common URL path prefix multiple times:

factory() {
val dispatcher = FixtureDispatcher()
val factory = PathQueryConditionFactory("/prefix/")
dispatcher.putResponse(factory.withPathSuffix("suffix"), "queryless_response")
// match all URLs with path ending with "suffix" and have "param" with any value as query parameter e.g. http://example.test/prefix/user/suffix?param
dispatcher.putResponse(factory.withPathSuffixAndQueryParameter("suffix", "param"), "response_with_query_parameter")
// match all URLs with path ending with "suffix" and have "param" with "value" as query parameter e.g. http://example.test/prefix/user/suffix?param=value
dispatcher.putResponse(factory.withPathSuffixAndQueryParameter("suffix", "param", "value"), "response_with_query_parameter_and_value")
mockWebServer.setDispatcher(dispatcher)

PathQueryCondition - when you want to match by path and optional query parameter:

pathQueryCondition() {
val dispatcher = FixtureDispatcher()
dispatcher.putResponse(PathQueryCondition("/prefix/suffix", "param", "value"), "response_with_query_parameter_and_value")
mockWebServer.setDispatcher(dispatcher)


HttpUrlCondition - when you want to match by some part of URL other than path or single query parameter:

httpUrlCondition() {
val dispatcher = FixtureDispatcher()
val condition = object : HttpUrlCondition() {
    override fun isUrlMatching(url: HttpUrl) = url.encodedUsername() == "foo"

    override fun compareTo(other: Condition) = 0
}
dispatcher.putResponse(condition , "response_for_foo")
mockWebServer.setDispatcher(dispatcher)    

Condition - when you want to match by non-URL parts of the request e.g. headers:

condition() {
val condition = object : Condition {
    override fun isRequestMatching(request: RecordedRequest)= request.getHeader("Content-Type") == "application/json"

    override fun compareTo(other: Condition) = 0
}
dispatcher.putResponse(condition , "json_response")   

Download

For unit tests:

Implementation 'pl.droidsonroids.testing:mockwebserver-path-dispatcher:1.1.0'

or for Android instrumentation tests:

oidTestImplementation 'pl.droidsonroids.testing:mockwebserver-path-dispatcher:1.1.0'
License

Library uses MIT License. See LICENSE file.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.