-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(pact-jvm-server): Converted PactSession to Kotlin
- Loading branch information
1 parent
d154527
commit 0234216
Showing
13 changed files
with
185 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
pact-jvm-server/src/main/kotlin/au/com/dius/pact/server/PactSession.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package au.com.dius.pact.server | ||
|
||
import au.com.dius.pact.core.matchers.FullRequestMatch | ||
import au.com.dius.pact.core.matchers.PartialRequestMatch | ||
import au.com.dius.pact.core.matchers.RequestMatching | ||
import au.com.dius.pact.core.matchers.RequestMismatch | ||
import au.com.dius.pact.core.model.IResponse | ||
import au.com.dius.pact.core.model.Interaction | ||
import au.com.dius.pact.core.model.OptionalBody | ||
import au.com.dius.pact.core.model.Pact | ||
import au.com.dius.pact.core.model.Request | ||
import au.com.dius.pact.core.model.Response | ||
import org.apache.commons.text.StringEscapeUtils | ||
|
||
data class PactSession( | ||
val expected: Pact?, | ||
val results: PactSessionResults | ||
) { | ||
fun receiveRequest(req: Request): Pair<IResponse, PactSession> { | ||
val invalidResponse = invalidRequest(req) | ||
|
||
return if (expected != null) { | ||
val matcher = RequestMatching(expected) | ||
when (val result = matcher.matchInteraction(req)) { | ||
is FullRequestMatch -> | ||
(result.interaction.asSynchronousRequestResponse()!!.response to recordMatched(result.interaction)) | ||
|
||
is PartialRequestMatch -> | ||
(invalidResponse to recordAlmostMatched(result)) | ||
|
||
is RequestMismatch -> | ||
(invalidResponse to recordUnexpected(req)) | ||
} | ||
} else { | ||
invalidResponse to this | ||
} | ||
} | ||
|
||
fun recordUnexpected(req: Request) = this.copy(results = results.addUnexpected(req)) | ||
|
||
fun recordAlmostMatched(partial: PartialRequestMatch) = this.copy(results = results.addAlmostMatched(partial)) | ||
|
||
fun recordMatched(interaction: Interaction) = this.copy(results = results.addMatched(interaction)) | ||
|
||
fun remainingResults() = if (expected != null) | ||
results.addMissing((expected.interactions - results.matched.toSet()).asIterable()) | ||
else results | ||
|
||
companion object { | ||
val CrossSiteHeaders = mapOf("Access-Control-Allow-Origin" to listOf("*")) | ||
@JvmStatic | ||
val empty = PactSession(null, PactSessionResults.empty) | ||
|
||
@JvmStatic | ||
fun forPact(pact: Pact) = PactSession(pact, PactSessionResults.empty) | ||
|
||
@JvmStatic | ||
fun invalidRequest(req: Request): IResponse { | ||
val headers = CrossSiteHeaders + mapOf( | ||
"Content-Type" to listOf("application/json"), | ||
"X-Pact-Unexpected-Request" to listOf("1") | ||
) | ||
val body = "{ \"error\": \"Unexpected request : " + StringEscapeUtils.escapeJson(req.toString()) + "\" }" | ||
return Response(500, headers.toMutableMap(), OptionalBody.body(body.toByteArray())) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 0 additions & 53 deletions
53
pact-jvm-server/src/main/scala/au/com/dius/pact/server/PactSession.scala
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.