Skip to content

Commit

Permalink
chore(pact-jvm-server): Add tests for ListServers handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rholshausen committed Oct 30, 2024
1 parent ca29ef0 commit 21c9978
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package au.com.dius.pact.server

import spock.lang.Specification
import static scala.collection.JavaConverters.mapAsScalaMap

class ListServersSpec extends Specification {
def 'empty state'() {
given:
def state = [:]

when:
def result = ListServers.apply(mapAsScalaMap(state).toMap())

then:
result.response().status == 200
result.response().body.valueAsString() == '{"ports": [], "paths": []}'
}

def 'with single Mock server'() {
given:
def state = [
'1234': Mock(StatefulMockProvider)
]

when:
def result = ListServers.apply(mapAsScalaMap(state).toMap())

then:
result.response().status == 200
result.response().body.valueAsString() == '{"ports": [1234], "paths": []}'
}

def 'with single Mock server with a path'() {
given:
def state = [
'/path': Mock(StatefulMockProvider)
]

when:
def result = ListServers.apply(mapAsScalaMap(state).toMap())

then:
result.response().status == 200
result.response().body.valueAsString() == '{"ports": [], "paths": ["/path"]}'
}

def 'with multiple Mock servers'() {
given:
def state = [
'1234': Mock(StatefulMockProvider),
'/path': Mock(StatefulMockProvider),
'8765': Mock(StatefulMockProvider),
'/other-path': Mock(StatefulMockProvider)
]

when:
def result = ListServers.apply(mapAsScalaMap(state).toMap())

then:
result.response().status == 200
result.response().body.valueAsString() == '{"ports": [1234, 8765], "paths": ["/path", "/other-path"]}'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import groovy.json.JsonSlurper
import org.apache.hc.client5.http.fluent.Request
import org.apache.hc.core5.http.ContentType
import org.apache.hc.core5.http.HttpResponse
import spock.lang.IgnoreIf
import spock.lang.Specification

import java.util.concurrent.TimeUnit

@IgnoreIf({ os.windows })
class MainSpec extends Specification {
def 'application command line args'() {
when:
Expand Down Expand Up @@ -71,7 +73,7 @@ class MainSpec extends Specification {
def result2 = getRoot('31311')

then:
result2 ==~ /\{"ports": \[\d+], "paths": \["any"]}/
result2 ==~ /\{"ports": \[\d+], "paths": \["\/data"]}/

when:
def mockJson = new JsonSlurper().parseText(result2)
Expand All @@ -80,6 +82,12 @@ class MainSpec extends Specification {
then:
result3.code == 204

when:
result3 = getDataByPath('31311')

then:
result3.code == 204

when:
def result4 = complete('31311', mockJson.ports[0])

Expand Down Expand Up @@ -112,7 +120,7 @@ class MainSpec extends Specification {
}

String createMock(String port, String pact) {
Request.post("http://127.0.0.1:$port/create?state=any&path=any")
Request.post("http://127.0.0.1:$port/create?state=any&path=%2Fdata")
.bodyString(pact, ContentType.APPLICATION_JSON)
.execute()
.returnContent()
Expand All @@ -132,4 +140,10 @@ class MainSpec extends Specification {
.execute()
.returnResponse()
}

HttpResponse getDataByPath(port) {
Request.get("http://127.0.0.1:$port/data")
.execute()
.returnResponse()
}
}

0 comments on commit 21c9978

Please sign in to comment.