Skip to content

Commit

Permalink
state change server returns params in response body
Browse files Browse the repository at this point in the history
  • Loading branch information
jbwheatley committed May 25, 2022
1 parent c4bc3b9 commit fca0ec5
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions shared/src/main/scala/pact4s/StateChanger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private[pact4s] object StateChanger {

object RootHandler extends HttpHandler {
def handle(t: HttpExchange): Unit = {
var responseBody = ""
Try {
val parser = Json.createParser(t.getRequestBody)
parser.next()
Expand All @@ -75,13 +76,16 @@ private[pact4s] object StateChanger {
key -> fixedValue
}.toMap)
.getOrElse(Map.empty)
// should return the params in the response body to be used with the generators
responseBody = "{" + params.map { case (k, v) => s""""$k": "$v"""" }.mkString(",") + "}"
(obj.getString("state"), params)
}.toOption.map { case (s, ps) => ProviderState(s, ps) }.flatMap(stateChange.lift).getOrElse(())
sendResponse(t)
sendResponse(t, responseBody)
}

private def sendResponse(t: HttpExchange): Unit = {
val response = "Ack!"
private def sendResponse(t: HttpExchange, body: String): Unit = {
val response = body
t.getResponseHeaders.set("Content-Type", "application/json")
t.sendResponseHeaders(200, response.length().toLong)
val os = t.getResponseBody
os.write(response.getBytes)
Expand Down

0 comments on commit fca0ec5

Please sign in to comment.