GithubHelp home page GithubHelp logo

isabella232 / play-mockws Goto Github PK

View Code? Open in Web Editor NEW

This project forked from leanovate/play-mockws

0.0 0.0 0.0 362 KB

Mock WS client for Play Framework

License: MIT License

Shell 2.24% Scala 97.76%

play-mockws's Introduction

Table of Contents

play-mockws

Build Status Codacy Badge Coverage Status Maven Central

Play MockWS is a mock WS client for Play Framework.

If:

  • you write an application in Scala with the Play Framework
  • the application makes HTTP calls to external web services with the WS client
  • you want to test your implementation

then you can use play-mockws to simulate HTTP requests to external web services in your tests.

Release Notes

see RELEASE-NOTES.md

Example

// simulation of a GET request to http://dns/url
val ws = MockWS {
  case (GET, "http://dns/url") => Action { Ok("http response") }
}

await(ws.url("http://dns/url").get()).body == "http response"

Adding play-mockws to your project

Add MockWS as test dependency in the build.sbt:

  • for Play 2.8.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.8.0" % Test
  • for Play 2.7.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.7.1" % Test
  • for Play 2.6.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.6.6" % Test
  • for Play 2.5.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.5.2" % Test
  • for Play 2.4.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.4.2" % Test
  • for Play 2.3.x:
libraryDependencies += "de.leanovate.play-mockws" %% "play-mockws" % "2.3.2" % Test

And use it:

import mockws.MockWS

val ws = MockWS { ... }

Usage

General usage

From the 2.6 version, it is recommended that your tests either extend trait MockWSHelpers or import MockWSHelpers. MockWSHelpers provides an implicit Materializer you need when working with Play's Actions.

class MySpec extends FreeSpec with Matchers with MockWSHelpers with BeforeAndAfterAll {
  ...

  override def afterAll(): Unit = {
    shutdownHelpers()
  }
}

or

import mockws.MockWSHelpers._

A MockWS instance can be directly constructed with a partial function like this:

val ws = MockWS {
  case (GET, "/") => Action { Ok("homepage") }
  case (POST, "/users") => Action { request => Created((request.body.asJson.get \ "id").as[String]) }
  case (GET, "/users/24") => Action { NotFound("") }
}

The partial function binds 2 Strings, an HTTP method and the URL, to a Play Action.

For clarity, this partial function is aliased as MockWS.Routes

When calling MockWS.url(), if the HTTP method and the URL are found, the defined play action is evaluated.

Controlling the routes

If you want more control on the routes, for example to know whether a route was called or how many times, use the Route class for this.

Routes can be defined together with the standard function orElse.

val route1 = Route {
  case (GET, "/route1") => Action { Ok("") }
}
val route2 = Route {
  case (GET, "/route2") => Action { Ok("") }
}

val ws = MockWS(route1 orElse route2)

await(ws.url("/route1").get())

route1.called == true
route2.called == false

route1.timeCalled == 1
route2.timeCalled == 0

An example how to structure an implementation to test it with MockWS can be found here.

Other examples can be found in the tests.

Compatibility Matrix

  • MockWS 2.8.x is actually only compatible with Play 2.8.y., with Scala 2.13 or 2.12.
  • MockWS 2.6.x is actually only compatible with Play 2.6.y., with Scala 2.12 or 2.11.
  • MockWS 2.5.x is actually only compatible with Play 2.5.y., with Scala 2.11.
  • MockWS 2.4.x is actually only compatible with Play 2.4.y., with Scala 2.10 or 2.11.
  • MockWS 2.3.x is actually only compatible with Play 2.3.y., with Scala 2.10 or 2.11.

play-mockws's People

Contributors

yanns avatar avdv avatar scala-steward avatar domdorn avatar a-shkarupin avatar matsluni avatar oripwk avatar zyv avatar codacy-badger avatar alpha-cd-robot avatar kwark avatar bomgar avatar mattrussell-sky avatar htmldoug avatar jdanbrown avatar brbrown25 avatar nugs avatar agebhar1 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.