{"id":13695641,"url":"https://github.com/leanovate/play-mockws","last_synced_at":"2025-05-03T13:32:45.893Z","repository":{"id":19926678,"uuid":"23193156","full_name":"leanovate/play-mockws","owner":"leanovate","description":"Mock WS client for Play Framework","archived":false,"fork":false,"pushed_at":"2024-11-04T02:28:33.000Z","size":487,"stargazers_count":138,"open_issues_count":10,"forks_count":34,"subscribers_count":23,"default_branch":"master","last_synced_at":"2024-11-04T03:23:39.876Z","etag":null,"topics":["playframework","scala","ws-client"],"latest_commit_sha":null,"homepage":null,"language":"Scala","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leanovate.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-08-21T15:46:57.000Z","updated_at":"2024-10-20T18:16:13.000Z","dependencies_parsed_at":"2024-03-05T20:29:16.121Z","dependency_job_id":"31ef2461-d0c6-4212-81d7-441340e02019","html_url":"https://github.com/leanovate/play-mockws","commit_stats":null,"previous_names":[],"tags_count":45,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leanovate%2Fplay-mockws","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leanovate%2Fplay-mockws/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leanovate%2Fplay-mockws/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leanovate%2Fplay-mockws/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leanovate","download_url":"https://codeload.github.com/leanovate/play-mockws/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224364350,"owners_count":17299054,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["playframework","scala","ws-client"],"created_at":"2024-08-02T18:00:31.599Z","updated_at":"2024-11-12T23:30:42.970Z","avatar_url":"https://github.com/leanovate.png","language":"Scala","funding_links":[],"categories":["Libraries"],"sub_categories":["Testing"],"readme":"Play MockWS\n=================\n\n![CI Workflow](https://github.com/leanovate/play-mockws/actions/workflows/ci.yml/badge.svg)\n[![Maven Central](https://maven-badges.herokuapp.com/maven-central/de.leanovate.play-mockws/play-mockws-3-0_3/badge.svg)](https://maven-badges.herokuapp.com/maven-central/de.leanovate.play-mockws/play-mockws-3-0_3)\n\n* [Goal](#goal)\n* [Simple example](#example)\n* [Adding play-mockws to your project](#adding-play-mockws-to-your-project)\n  * [Compatibility matrix](#compatibility-matrix)\n  * [play-mockws 3+](#play-mockws-3)\n  * [play-mockws 2.x](#play-mockws-2x)\n* [Usage](#usage)\n  * [General usage](#general-usage) \n  * [Controlling the routes](#controlling-the-routes) \n* [Release notes](#release-notes)\n\n## Goal\n\nPlay MockWS is a mock WS client for Play Framework.\n\nIf:\n\n- you write an application in Scala with the [Play Framework](https://playframework.com/)\n- the application makes HTTP calls to external web services with\n  the [WS client](https://www.playframework.com/documentation/latest/ScalaWS)\n- you want to test your implementation\n\nthen you can use Play MockWS to simulate HTTP requests to external web services in your tests.\n\n## Example\n\n```scala\n// simulation of a GET request to http://dns/url\nval ws = MockWS {\n  case (GET, \"http://dns/url\") =\u003e Action {\n    Ok(\"http response\")\n  }\n}\n\nawait(ws.url(\"http://dns/url\").get()).body == \"http response\"\n```\n\n## Adding play-mockws to your project\n\nAdd MockWS as test dependency in the `build.sbt`.\n\n### Compatibility Matrix\n\n| play-mock-ws version | Play versions | Scala versions   |\n|----------------------|---------------|------------------|\n| 3.0.x                | 2.8, 2.9, 3.0 | 2.12, 2.13, 3.3  |\n| 2.9.x                | 2.9           | 2.13, 3.3        |\n| 2.8.x                | 2.8           | 2.12, 2.13       |\n| 2.7.1                | 2.7           | 2.11, 2.12, 2.13 |\n| 2.7.0                | 2.7           | 2.11, 2.12       |\n| 2.6.x                | 2.6           | 2.11, 2.12       |\n| 2.5.x                | 2.5           | 2.11             |\n| 2.4.x                | 2.4           | 2.10, 2.11       |\n| 2.3.x                | 2.3           | 2.10, 2.11       |\n\n### play-mockws 3+\n\n```scala\n// Play 3.0.x\nlibraryDependencies += \"de.leanovate.play-mockws\" %% \"play-mockws-3-0\" % \"3.0.1\" % Test\n\n// Play 2.9.x\nlibraryDependencies += \"de.leanovate.play-mockws\" %% \"play-mockws-2-9\" % \"3.0.1\" % Test\n\n// Play 2.8.x\nlibraryDependencies += \"de.leanovate.play-mockws\" %% \"play-mockws-2-8\" % \"3.0.1\" % Test\n```\n\n### play-mockws 2.x\n\nNote that before the version 3.x, play-mockws was following a different naming and versioning scheme:\n\n```scala\n// Play 2.9.x\nlibraryDependencies += \"de.leanovate.play-mockws\" %% \"play-mockws\" % \"2.9.0\" % Test\n\n// Play 2.8.x\nlibraryDependencies += \"de.leanovate.play-mockws\" %% \"play-mockws\" % \"2.8.0\" % Test\n\n// Play 2.7.x\nlibraryDependencies += \"de.leanovate.play-mockws\" %% \"play-mockws\" % \"2.7.1\" % Test\n\n// Play 2.6.x\nlibraryDependencies += \"de.leanovate.play-mockws\" %% \"play-mockws\" % \"2.6.6\" % Test\n\n// Play 2.5.x\nlibraryDependencies += \"de.leanovate.play-mockws\" %% \"play-mockws\" % \"2.5.2\" % Test\n\n// Play 2.4.x\nlibraryDependencies += \"de.leanovate.play-mockws\" %% \"play-mockws\" % \"2.4.2\" % Test\n\n// Play 2.3.x\nlibraryDependencies += \"de.leanovate.play-mockws\" %% \"play-mockws\" % \"2.3.2\" % Test\n```\n\n## Usage\n\n### General usage\n\nFrom the 2.6 version, it is recommended that your tests either extend trait MockWSHelpers or import MockWSHelpers.\nMockWSHelpers\nprovides an implicit Materializer you need when working with Play's Actions.\n\n```scala\nclass MySpec extends FreeSpec with Matchers with MockWSHelpers with BeforeAndAfterAll {\n...\n\n  override def afterAll(): Unit = {\n    shutdownHelpers()\n  }\n}\n```\n\nor\n\n```scala\nimport mockws.MockWSHelpers._\n```\n\nA `MockWS` instance can be directly constructed with a partial function like this:\n\n```scala\nval ws = MockWS {\n  case (GET, \"/\") =\u003e Action {\n    Ok(\"homepage\")\n  }\n  case (POST, \"/users\") =\u003e Action { request =\u003e Created((request.body.asJson.get \\ \"id\").as[String]) }\n  case (GET, \"/users/24\") =\u003e Action {\n    NotFound(\"\")\n  }\n}\n```\n\nThe partial function binds 2 Strings, an HTTP method and the URL, to a\nPlay [Action](https://www.playframework.com/documentation/latest/ScalaActions).\n\nFor clarity, this partial function is aliased as [MockWS.Routes](play-mockws/src/main/akka/mockws/MockWS.scala)\n\nWhen calling MockWS.url(), if the HTTP method and the URL are found, the defined play action is evaluated.\n\n### Controlling the routes\n\nIf you want more control on the routes, for example to know whether a route was called or how many times, use\nthe [Route](play-mockws/src/main/scala/mockws/Route.scala) class for this.\n\nRoutes can be defined together with the standard function `orElse`.\n\n```scala\nval route1 = Route {\n  case (GET, \"/route1\") =\u003e Action {\n    Ok(\"\")\n  }\n}\nval route2 = Route {\n  case (GET, \"/route2\") =\u003e Action {\n    Ok(\"\")\n  }\n}\n\nval ws = MockWS(route1 orElse route2)\n\nawait(ws.url(\"/route1\").get())\n\nroute1.called == true\nroute2.called == false\n\nroute1.timeCalled == 1\nroute2.timeCalled == 0\n```\n\nAn example how to structure an implementation to test it with MockWS can be\nfound [here](play-mockws/src/test/scala/mockws/Example.scala).\n\nOther examples can be found in the [tests](play-mockws/src/test/).\n\n## Release notes\n\nSee [RELEASE-NOTES.md](RELEASE-NOTES.md) or [GitHub releases](https://github.com/leanovate/play-mockws/releases).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleanovate%2Fplay-mockws","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleanovate%2Fplay-mockws","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleanovate%2Fplay-mockws/lists"}