From 9dc29e10080ee8a8aedd12a71ca0f996157bad5a Mon Sep 17 00:00:00 2001 From: rehan Date: Fri, 11 Oct 2024 13:02:16 +0530 Subject: [PATCH] feat: introduce WireMock for testing - Added WireMockContainer to BrokenHttpLinksCheckerSpec for mocking HTTP responses. - progress towards fixing #331 - Configured WireMock to use mappings.json for predefined request-response mappings. --- gradle/libs.versions.toml | 4 ++++ htmlSanityCheck-core/build.gradle | 3 +++ .../check/BrokenHttpLinksCheckerSpec.groovy | 19 ++++++++++++++++++- .../src/test/resources/mappings.json | 16 ++++++++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 htmlSanityCheck-core/src/test/resources/mappings.json diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8452eb68..0935248e 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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' @@ -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' } diff --git a/htmlSanityCheck-core/build.gradle b/htmlSanityCheck-core/build.gradle index dc6c778b..108811d8 100644 --- a/htmlSanityCheck-core/build.gradle +++ b/htmlSanityCheck-core/build.gradle @@ -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 { diff --git a/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/check/BrokenHttpLinksCheckerSpec.groovy b/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/check/BrokenHttpLinksCheckerSpec.groovy index 540ffc83..7ffd956f 100644 --- a/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/check/BrokenHttpLinksCheckerSpec.groovy +++ b/htmlSanityCheck-core/src/test/groovy/org/aim42/htmlsanitycheck/check/BrokenHttpLinksCheckerSpec.groovy @@ -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 @@ -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 */ @@ -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 - google + google $HtmlConst.HTML_END """ htmlPage = new HtmlPage(HTML) diff --git a/htmlSanityCheck-core/src/test/resources/mappings.json b/htmlSanityCheck-core/src/test/resources/mappings.json new file mode 100644 index 00000000..6a6f2e80 --- /dev/null +++ b/htmlSanityCheck-core/src/test/resources/mappings.json @@ -0,0 +1,16 @@ +{ + "mappings" : [ + { + "request": { + "method": "HEAD", + "urlPattern": "/google" + }, + "response": { + "status": 200, + "headers": { + "Content-Type": "text/plain" + } + } + } + ] +} \ No newline at end of file