-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #272 from arttsu/wiremock-support
Add WireMock container wrapper
- Loading branch information
Showing
8 changed files
with
145 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
modules/wiremock/src/main/scala/com/dimafeng/testcontainers/WireMockContainer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.dimafeng.testcontainers | ||
|
||
import org.wiremock.integrations.testcontainers.{ | ||
WireMockContainer => JavaWireMockContainer | ||
} | ||
import org.testcontainers.utility.DockerImageName | ||
|
||
class WireMockContainer(underlying: JavaWireMockContainer) | ||
extends SingleContainer[JavaWireMockContainer] { | ||
override val container: JavaWireMockContainer = underlying | ||
|
||
def getHost: String = container.getHost | ||
def getPort: Int = container.getPort | ||
def getBaseUrl: String = container.getBaseUrl | ||
def getUrl(path: String): String = container.getUrl(path) | ||
} | ||
|
||
object WireMockContainer { | ||
val defaultImage: String = "wiremock/wiremock" | ||
val defaultTag: String = "latest" | ||
val defaultDockerImageName: String = s"$defaultImage:$defaultTag" | ||
|
||
case class Def( | ||
dockerImageName: DockerImageName = | ||
DockerImageName.parse(defaultDockerImageName), | ||
builderFs: Seq[JavaWireMockContainer => JavaWireMockContainer] = | ||
Seq.empty[JavaWireMockContainer => JavaWireMockContainer] | ||
) extends ContainerDef { | ||
override type Container = WireMockContainer | ||
|
||
def withMappingFromResource(resourceName: String): Def = { | ||
copy(builderFs = | ||
builderFs :+ ((underlying: JavaWireMockContainer) => | ||
underlying.withMappingFromResource(resourceName) | ||
) | ||
) | ||
} | ||
|
||
def withFileFromResource(resourceName: String): Def = { | ||
copy(builderFs = | ||
builderFs :+ ((underlying: JavaWireMockContainer) => | ||
underlying.withFileFromResource(resourceName) | ||
) | ||
) | ||
} | ||
|
||
override protected def createContainer(): WireMockContainer = { | ||
val underlying = | ||
builderFs.foldLeft(new JavaWireMockContainer(dockerImageName))( | ||
(underlying, f) => f(underlying) | ||
) | ||
|
||
new WireMockContainer(underlying) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"result":"world"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"request": { | ||
"url": "/bar", | ||
"method": "GET" | ||
}, | ||
"response": { | ||
"status": 200, | ||
"bodyFileName": "bar_body.json", | ||
"headers": { | ||
"Content-Type": "application/json" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"request": { | ||
"url": "/foo", | ||
"method": "GET" | ||
}, | ||
"response": { | ||
"status": 200, | ||
"jsonBody": { | ||
"result": "hello" | ||
}, | ||
"headers": { | ||
"Content-Type": "application/json" | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
modules/wiremock/src/test/scala/com/dimafeng/testcontainers/WireMockSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.dimafeng.testcontainers | ||
|
||
import com.dimafeng.testcontainers.scalatest.TestContainersForAll | ||
import org.scalatest.flatspec.AnyFlatSpec | ||
import sttp.client3._ | ||
|
||
class WireMockSpec extends AnyFlatSpec with TestContainersForAll { | ||
override type Containers = WireMockContainer | ||
|
||
override def startContainers(): WireMockContainer = { | ||
WireMockContainer | ||
.Def() | ||
.withMappingFromResource("foo_mapping.json") | ||
.withMappingFromResource("bar_mapping.json") | ||
.withFileFromResource("bar_body.json") | ||
.start() | ||
} | ||
|
||
"WireMock container" should "start" in withContainers { container => | ||
val backend = HttpURLConnectionBackend() | ||
|
||
val response = basicRequest | ||
.get(uri"${container.getUrl("/__admin/health")}") | ||
.send(backend) | ||
|
||
assert(response.code.code == 200) | ||
} | ||
|
||
"WireMock container" should "support adding mappings and files" in withContainers { | ||
container => | ||
val backend = HttpURLConnectionBackend() | ||
|
||
val fooResponse = | ||
basicRequest.get(uri"${container.getUrl("/foo")}").send(backend) | ||
val barResponse = | ||
basicRequest.get(uri"${container.getUrl("/bar")}").send(backend) | ||
|
||
assert(fooResponse.body == Right("""{"result":"hello"}""")) | ||
assert(barResponse.body == Right("""{"result":"world"}\n""".replaceAllLiterally("\\n", "\n"))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters