com.github.tomakehurst.wiremock.WireMockServer Scala Examples

The following examples show how to use com.github.tomakehurst.wiremock.WireMockServer. 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: WireMockHelper.scala    From pertax-frontend   with Apache License 2.0 6 votes vote down vote up
package util

import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Suite}

trait WireMockHelper extends BeforeAndAfterAll with BeforeAndAfterEach {
  this: Suite =>

  protected val server: WireMockServer = new WireMockServer(wireMockConfig().dynamicPort())

  override def beforeAll(): Unit = {
    server.start()
    super.beforeAll()
  }

  override def beforeEach(): Unit = {
    server.resetAll()
    super.beforeEach()
  }

  override def afterAll(): Unit = {
    super.afterAll()
    server.stop()
  }
} 
Example 2
Source File: TestApplication.scala    From vat-api   with Apache License 2.0 5 votes vote down vote up
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 3
Source File: ClientTestBase.scala    From endpoints4s   with MIT License 5 votes vote down vote up
package endpoints4s.algebra.client

import java.net.ServerSocket

import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.core.WireMockConfiguration.options
import endpoints4s.algebra
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll}

import scala.concurrent.Future
import scala.concurrent.duration._
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

trait ClientTestBase[T <: algebra.Endpoints]
    extends AnyWordSpec
    with Matchers
    with ScalaFutures
    with BeforeAndAfterAll
    with BeforeAndAfter {

  override implicit def patienceConfig: PatienceConfig =
    PatienceConfig(15.seconds, 10.millisecond)

  val wiremockPort = findOpenPort
  val wireMockServer = new WireMockServer(options().port(wiremockPort))

  override def beforeAll(): Unit = wireMockServer.start()

  override def afterAll(): Unit = wireMockServer.stop()

  before {
    wireMockServer.resetAll()
  }

  def findOpenPort: Int = {
    val socket = new ServerSocket(0)
    try socket.getLocalPort
    finally if (socket != null) socket.close()
  }

  val client: T

  def call[Req, Resp](
      endpoint: client.Endpoint[Req, Resp],
      args: Req
  ): Future[Resp]

  def encodeUrl[A](url: client.Url[A])(a: A): String

} 
Example 4
Source File: WireMockSupport.scala    From self-assessment-api   with Apache License 2.0 5 votes vote down vote up
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 5
Source File: WiremockTestServer.scala    From http-verbs   with Apache License 2.0 5 votes vote down vote up
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 vote down vote up
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 vote down vote up
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 vote down vote up
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 vote down vote up
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()
  }
} 
Example 10
Source File: WireMockSupport.scala    From guardrail   with MIT License 5 votes vote down vote up
package helpers

import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.core.WireMockConfiguration._
import java.net.URI
import org.scalatest.{ BeforeAndAfterAll, Suite }

trait WireMockSupport extends BeforeAndAfterAll { self: Suite =>
  protected val wireMockServer       = new WireMockServer(wireMockConfig().bindAddress("localhost").dynamicPort())
  protected def wireMockBaseUrl: URI = new URI(s"http://localhost:${wireMockServer.port()}")

  override protected def beforeAll(): Unit = {
    super.beforeAll()
    wireMockServer.start()
  }

  override protected def afterAll(): Unit = {
    wireMockServer.stop()
    super.afterAll()
  }
}