Skip to content

Commit

Permalink
feat: introduce WireMock for testing
Browse files Browse the repository at this point in the history
- Added WireMockContainer to BrokenHttpLinksCheckerSpec for mocking HTTP responses.
- progress towards fixing aim42#331
- Configured WireMock to use mappings.json for predefined request-response mappings.
  • Loading branch information
RehanChalana committed Oct 12, 2024
1 parent ed0e1a3 commit 9dc29e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[versions]
junit5-version = '5.11.2'
slf4j-version = '2.0.16'
wiremock-testcontainers-version = '1.0-alpha-14'
testcontainers-version = '1.20.2'

[libraries]
commons-validator = 'commons-validator:commons-validator:1.9.0'
Expand All @@ -10,6 +12,8 @@ lombok = 'org.projectlombok:lombok:1.18.34'
slf4j-api = { module = 'org.slf4j:slf4j-api', version.ref = 'slf4j-version' }
slf4j-nop = { module = 'org.slf4j:slf4j-nop', version.ref = 'slf4j-version' }
spock = 'org.spockframework:spock-bom:2.3-groovy-3.0'
wiremock-testcontainers = { module = 'org.wiremock.integrations.testcontainers:wiremock-testcontainers-module', version.ref = 'wiremock-testcontainers-version' }
testcontainers-junit-jupiter = { module = 'org.testcontainers:junit-jupiter', version.ref = 'testcontainers-version' }

[plugins]
gradle-versions = { id= 'com.github.ben-manes.versions', version = '0.51.0' }
Expand Down
3 changes: 3 additions & 0 deletions htmlSanityCheck-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ dependencies {

testImplementation platform("org.codehaus.groovy:groovy-bom:${GroovySystem.version}")
testImplementation 'org.codehaus.groovy:groovy-xml'

testImplementation 'org.wiremock.integrations.testcontainers:wiremock-testcontainers-module:1.0-alpha-14'
testImplementation "org.testcontainers:junit-jupiter:1.20.2"
}

gitProperties {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import org.aim42.htmlsanitycheck.collect.SingleCheckResults
import org.aim42.htmlsanitycheck.html.HtmlConst
import org.aim42.htmlsanitycheck.html.HtmlPage
import org.aim42.htmlsanitycheck.tools.Web
import org.wiremock.integrations.testcontainers.WireMockContainer
import spock.lang.Ignore
import spock.lang.IgnoreIf
import spock.lang.Shared
import spock.lang.Specification
import spock.lang.Unroll

Expand All @@ -20,8 +22,23 @@ class BrokenHttpLinksCheckerSpec extends Specification {
SingleCheckResults collector

private Configuration myConfig
static private int port

@Shared
WireMockContainer wireMockServer = new WireMockContainer("wiremock/wiremock:3.9.1-1")
.withMappingFromResource("mappings.json")
.withExposedPorts(8080)

/** executed once before all specs are executed **/
def setupSpec() {
wireMockServer.start()
port = wireMockServer.getMappedPort(8080)
}

/** executed once after all specs are executed **/
def cleanupSpec() {
wireMockServer.stop()
}

/* executed before every single spec */

Expand Down Expand Up @@ -63,7 +80,7 @@ class BrokenHttpLinksCheckerSpec extends Specification {
def "one syntactically correct http URL is ok"() {
given: "an HTML page with a single correct anchor/link"
String HTML = """$HtmlConst.HTML_HEAD
<a href="https://google.com">google</a>
<a href="http://localhost:$port/google">google</a>
$HtmlConst.HTML_END """

htmlPage = new HtmlPage(HTML)
Expand Down
16 changes: 16 additions & 0 deletions htmlSanityCheck-core/src/test/resources/mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"mappings" : [
{
"request": {
"method": "HEAD",
"urlPattern": "/google"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "text/plain"
}
}
}
]
}

0 comments on commit 9dc29e1

Please sign in to comment.