com.github.tomakehurst.wiremock.client.WireMock Scala Examples
The following examples show how to use com.github.tomakehurst.wiremock.client.WireMock.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
Example 1
Source File: TestApplication.scala From vat-api with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.vatapi import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.client.WireMock._ import com.github.tomakehurst.wiremock.core.WireMockConfiguration._ import com.github.tomakehurst.wiremock.stubbing.StubMapping import org.scalamock.scalatest.MockFactory import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach} import play.api.http.Status._ import scala.concurrent.duration._ import scala.language.postfixOps trait TestApplication extends UnitSpec with BeforeAndAfterEach with BeforeAndAfterAll with MockFactory { override implicit val timeout: FiniteDuration = 100 seconds val mockPort = 22222 val mockHost = "localhost" protected val wiremockBaseUrl: String = s"http://$mockHost:$mockHost" private val wireMockServer = new WireMockServer(wireMockConfig().port(mockPort)) protected def baseBeforeAll(): StubMapping = { wireMockServer.stop() wireMockServer.start() WireMock.configureFor(mockHost, mockPort) // the below stub is here so that the application finds the registration endpoint which is called on startup stubFor(post(urlPathEqualTo("/registration")).willReturn(aResponse().withStatus(OK))) } override def beforeAll(): Unit = { super.beforeAll() baseBeforeAll() } override def afterAll(): Unit = { super.afterAll() wireMockServer.stop() } override def beforeEach(): Unit = { super.beforeEach() WireMock.reset() } }
Example 2
Source File: WireMockVerify.scala From self-assessment-api with Apache License 2.0 | 5 votes |
package support.wiremock import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.http.HttpHeader import com.github.tomakehurst.wiremock.matching.RequestPatternBuilder import com.github.tomakehurst.wiremock.verification.LoggedRequest import org.scalatest.Assertion import org.scalatest.matchers.should.Matchers._ import scala.collection.convert.ToScalaImplicits trait WireMockVerify extends ToScalaImplicits { private def allMocks: List[LoggedRequest] = WireMock.findAll(RequestPatternBuilder.allRequests()).toList def getMockFor(url: String): LoggedRequest = { // use equals as well as regex matching to account for query parameters separators not being able to be passed val mock: Option[LoggedRequest] = allMocks.find(y=> y.getUrl.equalsIgnoreCase(url) || y.getUrl.matches(url)) require(mock.isDefined, s"Trying to verify WireMock stubbing but none found for $url") mock.get } def verifyHeaderExists(mock: LoggedRequest, header: (String, String)): Assertion = { val (name, value) = header withClue(s"Trying to verify mock for ${mock.getUrl} has header $name, but does not\n") { mock.containsHeader(name) shouldBe true } val mockHeader = mock.header(name) withClue(s"Trying to verify mock for ${mock.getUrl} has header $name with value $value, but does not in ${getValues(mockHeader)}\n"){ mockHeader.containsValue(value) shouldBe true } } def getValues(header: HttpHeader): List[String] = { if(header.isPresent) header.values().toList else List.empty } }
Example 3
Source File: WireMockSupport.scala From self-assessment-api with Apache License 2.0 | 5 votes |
package support.wiremock import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Suite} trait WireMockConfig { val wiremockHost = "localhost" val wiremockPort = 11111 val wiremockUrl = s"http://$wiremockHost:$wiremockPort" } trait WireMockSupport extends WireMockConfig with BeforeAndAfterAll with BeforeAndAfterEach { _: Suite => private val wireMockServer = new WireMockServer(wireMockConfig().port(wiremockPort)) override protected def beforeAll(): Unit = { super.beforeAll() wireMockServer.start() WireMock.configureFor(wiremockHost, wiremockPort) } override protected def afterAll(): Unit = { wireMockServer.stop() super.afterAll() } override protected def beforeEach(): Unit = { super.beforeEach() WireMock.reset() } }
Example 4
Source File: WireMockHelpers.scala From self-assessment-api with Apache License 2.0 | 5 votes |
package support.wiremock import com.github.tomakehurst.wiremock.client.WireMock._ import com.github.tomakehurst.wiremock.client.{MappingBuilder, ResponseDefinitionBuilder, WireMock} import com.github.tomakehurst.wiremock.stubbing.StubMapping import play.api.libs.json.JsValue trait WireMockHelpers { def aResponse: ResponseDefinitionBuilder = WireMock.aResponse() def stubGet(url: String, status: Int, body: JsValue): StubMapping = { stubGet(url, status, Some(body.toString())) } def stubGet(url: String, status: Int, body: Option[String] = None): StubMapping = { stubFor(get(urlPathMatching(url)).willReturn( body.fold{ aResponse .withStatus(status) }{ aResponse .withStatus(status) .withBody(_) } )) } def partialStubGet(url: String): MappingBuilder = get(urlEqualTo(url)) def stubPost(url: String, status: Int, body: JsValue): StubMapping = { stubPost(url, status, Some(body.toString())) } def stubPost(url: String, status: Int, body: Option[String] = None): StubMapping = { stubFor(post(urlPathMatching(url)).willReturn( body.fold{ aResponse .withStatus(status) }{ aResponse .withStatus(status) .withBody(_) } )) } def partialStubPost(url: String): MappingBuilder = post(urlPathMatching(url)) def partialStubPut(url: String): MappingBuilder = put(urlPathMatching(url)) implicit class responseBuilderOps(response: ResponseDefinitionBuilder){ def withBody(json: JsValue): ResponseDefinitionBuilder = response.withBody(json.toString()) } }
Example 5
Source File: WiremockTestServer.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.examples.utils import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import org.scalatest.BeforeAndAfterEach import org.scalatest.wordspec.AnyWordSpecLike trait WiremockTestServer extends AnyWordSpecLike with BeforeAndAfterEach { val wireMockServer = new WireMockServer(20001) override protected def beforeEach(): Unit = { wireMockServer.start() WireMock.configureFor("localhost", 20001) } override protected def afterEach(): Unit = { wireMockServer.stop() } }
Example 6
Source File: wireMockEndpoints.scala From http-verbs with Apache License 2.0 | 5 votes |
package uk.gov.hmrc.play.http.ws import java.net.ServerSocket import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.core.WireMockConfiguration._ import scala.util.Try trait WireMockEndpoints { val host: String = "localhost" val endpointPort: Int = PortTester.findPort() val endpointMock = new WireMock(host, endpointPort) val endpointServer: WireMockServer = new WireMockServer(wireMockConfig().port(endpointPort)) val proxyPort: Int = PortTester.findPort(endpointPort) val proxyMock: WireMock = new WireMock(host, proxyPort) val proxyServer: WireMockServer = new WireMockServer(wireMockConfig().port(proxyPort)) def withServers(test: => Unit) { endpointServer.start() proxyServer.start() try { test } finally { Try(endpointServer.stop()) Try(proxyServer.stop()) } } } object PortTester { def findPort(excluded: Int*): Int = (6000 to 7000).find(port => !excluded.contains(port) && isFree(port)).getOrElse(throw new Exception("No free port")) private def isFree(port: Int): Boolean = { val triedSocket = Try { val serverSocket = new ServerSocket(port) Try(serverSocket.close()) serverSocket } triedSocket.isSuccess } }
Example 7
Source File: BaseWiremockSpec.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.tools.data.helpers import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig import org.scalatest._ trait BaseWiremockSpec extends AsyncFlatSpec with Matchers with BeforeAndAfterAll with BeforeAndAfterEach{ val host = "localhost" val wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()) override def beforeAll(): Unit = { wireMockServer.start() WireMock.configureFor(host, wireMockServer.port) super.beforeAll() } override def afterEach: Unit = { WireMock.reset() super.afterEach() } override protected def afterAll(): Unit = { wireMockServer.shutdownServer() super.afterAll() } }
Example 8
Source File: DownloaderSpec.scala From CM-Well with Apache License 2.0 | 5 votes |
package cmwell.tools.data.downloader.streams import akka.actor.ActorSystem import akka.http.scaladsl.model.StatusCodes import akka.stream.scaladsl._ import akka.stream.{ActorMaterializer, Materializer} import akka.util.ByteString import org.scalatest._ import org.scalatest.prop._ import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.client.WireMock._ import com.github.tomakehurst.wiremock.core.WireMockConfiguration._ import scala.concurrent.Await import scala.concurrent.duration._ import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks class DownloaderSpec extends PropSpec with ScalaCheckPropertyChecks with Matchers with BeforeAndAfterAll { implicit val system: ActorSystem = ActorSystem("reactive-tools-system") implicit val mat: Materializer = ActorMaterializer() val host = "localhost" val numUuidsPerRequest = 25 override protected def afterAll(): Unit = { system.terminate() super.afterAll() } property ("Download from uuids stream sends request blocks of uuids") { val uuids = Table( ("numUuids", "blocksToSend"), (1 , 1 ), (10 , 1 ), (20 , 1 ), (30 , 2 ), (60 , 3 ) ) forAll(uuids) { (numUuids: Int, expectedBlocksToSend: Int) => // setup wiremock val wireMockServer = new WireMockServer(wireMockConfig().dynamicPort()) wireMockServer.start() val port = wireMockServer.port WireMock.configureFor(host, port) // create sample uuids to download val data = for (x <- 1 to numUuids) yield s"uuid$x\n" // wiremock stubbing stubFor(post(urlPathMatching("/_out.*")) .willReturn(aResponse() .withBody("body") .withStatus(StatusCodes.OK.intValue))) // create uuid input-stream val in = Source.fromIterator(() => data.iterator) .map(ByteString.apply) .runWith(StreamConverters.asInputStream()) // download mock data Await.result ( Downloader.downloadFromUuidInputStream( baseUrl = s"$host:$port", numInfotonsPerRequest = numUuidsPerRequest, in = in) ,30.seconds ) // verifying val numBlocksSent = findAll(postRequestedFor((urlPathMatching("/_out.*")))).size // teardown wiremock wireMockServer.shutdown() wireMockServer.stop() while (wireMockServer.isRunning) {} wireMockServer.resetRequests() numBlocksSent should be (expectedBlocksToSend) } } }
Example 9
Source File: WireMockFixture.scala From kafka-serialization with Apache License 2.0 | 5 votes |
package com.ovoenergy.kafka.serialization.testkit import com.github.tomakehurst.wiremock.WireMockServer import com.github.tomakehurst.wiremock.client.WireMock import com.github.tomakehurst.wiremock.core.WireMockConfiguration import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Suite} trait WireMockFixture extends BeforeAndAfterAll with BeforeAndAfterEach { self: Suite => private lazy val wireMockServer: WireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort()) val wireMockHost: String = "localhost" def wireMockPort: Int = wireMockServer.port() def wireMockEndpoint: String = s"http://$wireMockHost:$wireMockPort" override protected def beforeAll(): Unit = { super.beforeAll() wireMockServer.start() WireMock.configureFor(wireMockPort) } override protected def afterAll(): Unit = { wireMockServer.shutdown() super.afterAll() } override protected def afterEach(): Unit = { resetWireMock() super.afterEach() } override protected def beforeEach(): Unit = { super.beforeEach() resetWireMock() } def resetWireMock(): Unit = { wireMockServer.resetMappings() wireMockServer.resetRequests() wireMockServer.resetScenarios() } }