Skip to content

Commit

Permalink
fix: handle statechage calls that do not return a body
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald Holshausen committed Jun 16, 2020
1 parent 4d67a8c commit d96859f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,16 @@ object DefaultStateChange : StateChange, KLogging() {
}

private fun parseJsonResponse(entity: HttpEntity?): Result<Map<String, Any?>, Exception> {
return if (entity != null && ContentType.get(entity).mimeType == ContentType.APPLICATION_JSON.mimeType) {
return if (entity != null) {
val contentType: ContentType? = ContentType.get(entity)
if (contentType != null && contentType.mimeType == ContentType.APPLICATION_JSON.mimeType) {
val body = EntityUtils.toString(entity)
Ok(Json.toMap(JsonParser.parseString(body)))
} else {
Ok(emptyMap())
}
} else {
Ok(emptyMap())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package au.com.dius.pact.provider
import au.com.dius.pact.core.model.Interaction
import au.com.dius.pact.core.model.ProviderState
import com.github.michaelbull.result.Ok
import org.apache.http.HttpEntity
import org.apache.http.ProtocolVersion
import org.apache.http.client.methods.CloseableHttpResponse
import org.apache.http.message.BasicStatusLine
import spock.lang.Specification

@SuppressWarnings('PrivateFieldCouldBeFinal')
Expand All @@ -12,7 +16,7 @@ class StateChangeSpec extends Specification {
private ProviderInfo providerInfo
private Closure consumer
private ProviderState state
private makeStateChangeRequestArgs
private makeStateChangeRequestArgs, stateChangeResponse
private Map consumerMap = [name: 'bob']
private ProviderClient mockProviderClient

Expand All @@ -22,10 +26,11 @@ class StateChangeSpec extends Specification {
consumer = { consumerMap as ConsumerInfo }
providerVerifier = new ProviderVerifier()
makeStateChangeRequestArgs = []
stateChangeResponse = null
mockProviderClient = Mock(ProviderClient) {
makeStateChangeRequest(_, _, _, _, _) >> { args ->
makeStateChangeRequestArgs << args
null
stateChangeResponse
}
makeRequest(_) >> [statusCode: 200, headers: [:], data: '{}', contentType: 'application/json']
}
Expand All @@ -40,7 +45,7 @@ class StateChangeSpec extends Specification {
mockProviderClient)

then:
result
result instanceof Ok
makeStateChangeRequestArgs == []
}

Expand All @@ -53,7 +58,7 @@ class StateChangeSpec extends Specification {
mockProviderClient)

then:
result
result instanceof Ok
makeStateChangeRequestArgs == []
}

Expand All @@ -66,7 +71,7 @@ class StateChangeSpec extends Specification {
mockProviderClient)

then:
result
result instanceof Ok
makeStateChangeRequestArgs == []
}

Expand All @@ -79,12 +84,30 @@ class StateChangeSpec extends Specification {
mockProviderClient)

then:
result
result instanceof Ok
makeStateChangeRequestArgs == [
[new URI('http://localhost:2000/hello'), state, true, true, false]
]
}

def 'Handle the case were the state change response has no body'() {
given:
consumerMap.stateChange = 'http://localhost:2000/hello'
def entity = [getContentType: { null }] as HttpEntity
stateChangeResponse = [
getEntity: { entity },
getStatusLine: { new BasicStatusLine(new ProtocolVersion('HTTP', 1, 1), 200, 'OK') },
close: { }
] as CloseableHttpResponse

when:
def result = DefaultStateChange.INSTANCE.stateChange(providerVerifier, state, providerInfo, consumer(), true,
mockProviderClient)

then:
result instanceof Ok
}

def 'if the state change is a closure, executes it with the state change as a parameter'() {
given:
def closureArgs = []
Expand All @@ -95,7 +118,7 @@ class StateChangeSpec extends Specification {
mockProviderClient)

then:
result
result instanceof Ok
makeStateChangeRequestArgs == []
closureArgs == [state]
}
Expand All @@ -109,7 +132,7 @@ class StateChangeSpec extends Specification {
mockProviderClient)

then:
result
result instanceof Ok
makeStateChangeRequestArgs == []
}

Expand Down

0 comments on commit d96859f

Please sign in to comment.