Skip to content

Commit

Permalink
feat: update test cases to use WireMock with Testcontainers and Custo…
Browse files Browse the repository at this point in the history
…mHostNameResolver

- fixes aim42#331
- Updated all URLs in test cases to use WireMock with Testcontainers.
- Integrated CustomHostNameResolver to ensure tests run without internet.
- Updated `mappings.json` with mappings for all test URLs.
- Remove test for recognizing internet connectivity.
  • Loading branch information
RehanChalana committed Oct 12, 2024
1 parent 1a5af1e commit 6aab113
Show file tree
Hide file tree
Showing 2 changed files with 284 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ import org.aim42.htmlsanitycheck.html.HtmlPage
import org.aim42.htmlsanitycheck.test.dns.CustomHostNameResolver
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

import java.lang.reflect.Field
import java.lang.reflect.Proxy


// see end-of-file for license information


Expand Down Expand Up @@ -81,16 +77,6 @@ class BrokenHttpLinksCheckerSpec extends Specification {
throw new RuntimeException("Failed to register custom DNS resolver", e);
}
}
/**
* checking for internet connectivity is a somewhat brittle - as there's no such thing as "the internet"
* (the checker will most likely use google.com as a proxy for "internet"
*/
// todo: test that properly
@IgnoreIf({ !System.getenv('GITHUB_ACTIONS') })
def "recognize if there is internet connectivity"() {
expect: "if there is no internet connection, testing should fail"
Web.isInternetConnectionAvailable()
}

def "empty page has no errors"() {
given: "an empty HTML page"
Expand All @@ -112,7 +98,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="http://${CustomHostNameResolver.WIREMOCK_HOST}:$port/google">google</a>
<a href="http://google.com:$port">google</a>
$HtmlConst.HTML_END """

htmlPage = new HtmlPage(HTML)
Expand Down Expand Up @@ -147,17 +133,17 @@ class BrokenHttpLinksCheckerSpec extends Specification {
collector.nrOfProblems() == 0

where:
goodUrl << ["https://junit.org/junit4/javadoc/latest/org/junit/Before.html",
"https://plumelib.org/plume-util/api/org/plumelib/util/DeterministicObject.html",
"https://people.csail.mit.edu/cpacheco/publications/randoop-case-study-abstract.html"
goodUrl << ["http://junit.org:$port/junit4",
"http://plumelib.org:$port/plume-util",
"http://people.csail.mit.edu:$port/cpacheco"
]
}


def "single bad link is identified as problem"() {

given: "an HTML page with a single (bad) link"
String badhref = "https://arc42.org/ui98jfuhenu87djch"
String badhref = "http://arc42.org:$port/ui98jfuhenu87djch"
String HTML = """$HtmlConst.HTML_HEAD
<a href=${badhref}>nonexisting arc42 link</a>
$HtmlConst.HTML_END """
Expand All @@ -177,10 +163,10 @@ class BrokenHttpLinksCheckerSpec extends Specification {
* where HEAD requests are always answered with 405 instead of 200...
*/

//@Ignore("test currently breaks. see issue-219")

def "amazon does not deliver 405 statuscode for links that really exist"() {
given: "an HTML page with a single (good) amazon link"
String goodAmazonLink = "https://www.amazon.com/dp/B01A2QL9SS"
String goodAmazonLink = "http://www.amazon.com:$port/dp/B01A2QL9SS"
String HTML = """$HtmlConst.HTML_HEAD
<a href=${goodAmazonLink}>Amazon</a>
$HtmlConst.HTML_END """
Expand Down Expand Up @@ -223,7 +209,7 @@ class BrokenHttpLinksCheckerSpec extends Specification {
def 'bad link #badLink is recognized as such'() {

given: "an HTML page with a single (broken) link"
String goodURL = "https://mock.codes/${badLink}"
String goodURL = "http://mock.codes$port/${badLink}"
String HTML = """$HtmlConst.HTML_HEAD
<a href=${goodURL}>${badLink}</a>
$HtmlConst.HTML_END """
Expand Down
278 changes: 276 additions & 2 deletions htmlSanityCheck-core/src/test/resources/mappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,289 @@
"mappings" : [
{
"request": {
"method": "HEAD",
"urlPattern": "/google"
"method": "GET_OR_HEAD",
"urlPattern": "/(|junit4|plume-util|cpacheco)"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET",
"urlPattern": "/dp/B01A2QL9SS"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "HEAD",
"urlPattern": "/dp/B01A2QL9SS"
},
"response": {
"status": 405,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/ui98jfuhenu87djch"
},
"response": {
"status": 400,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/400"
},
"response": {
"status": 400,
"headers": {
"Content-Type": "text/plain"
}
}
},{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "401"
},
"response": {
"status": 401,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/401"
},
"response": {
"status": 401,
"headers": {
"Content-Type": "text/plain"
}
}
},{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/402"
},
"response": {
"status": 402,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/403"
},
"response": {
"status": 403,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/404"
},
"response": {
"status": 404,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/405"
},
"response": {
"status": 405,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/406"
},
"response": {
"status": 406,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/408"
},
"response": {
"status": 408,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/409"
},
"response": {
"status": 409,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/410"
},
"response": {
"status": 410,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/429"
},
"response": {
"status": 429,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/431"
},
"response": {
"status": 431,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/500"
},
"response": {
"status": 500,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/501"
},
"response": {
"status": 501,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/502"
},
"response": {
"status": 502,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/504"
},
"response": {
"status": 504,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/505"
},
"response": {
"status": 505,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/506"
},
"response": {
"status": 506,
"headers": {
"Content-Type": "text/plain"
}
}
},
{
"request": {
"method": "GET_OR_HEAD",
"urlPattern": "/507"
},
"response": {
"status": 507,
"headers": {
"Content-Type": "text/plain"
}
}
}
]
}

0 comments on commit 6aab113

Please sign in to comment.