Skip to content

Commit

Permalink
Merge pull request #202 from heaven-born/master
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmsamson authored Jun 9, 2023
2 parents 2d35de1 + d1d45a5 commit 5d26ea0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ final case class RuntimeApiLive(environment: LambdaEnvironment) extends RuntimeA
val conn = url.openConnection().asInstanceOf[HttpURLConnection]
conn.setRequestMethod("POST")
conn.setRequestProperty("Content-Type", "application/json")
conn.setFixedLengthStreamingMode(invocationResponse.payload.length())
val payload = invocationResponse.payload
val payloadBytes = payload.getBytes("UTF-8")
conn.setFixedLengthStreamingMode(payloadBytes.length)
conn.setDoOutput(true)

ZIO.attempt {
val outputStream = conn.getOutputStream()
outputStream.write(invocationResponse.payload.getBytes())
outputStream.write(payloadBytes)
try conn.getInputStream().close()
catch { case _: Throwable => () } // Reusing connection
}
Expand Down
32 changes: 32 additions & 0 deletions lambda/src/test/scala/zio/lambda/internal/RuntimeApiLiveSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package zio.lambda.internal

import zio._
import zio.test.Assertion._
import zio.test._

import java.net.{ConnectException, ServerSocket}

object RuntimeApiLiveSpec extends ZIOSpecDefault {

val serverSocket = new ServerSocket(8085)

override def spec =
suite("RuntimeApiLiveSpec spec")(
test("sendInvocationResponse should not throw any error when unicode string is provided") {

check(Gen.string(Gen.unicodeChar)) { unicodeString =>
val env = LambdaEnvironment("localhost:8085", "", "", 0, "", "", "", "")
val resp = InvocationResponse("id", unicodeString)
val runtime = new RuntimeApiLive(env)
for {
_ <- ZIO.attempt {
serverSocket.accept().close()
}.fork
res <- runtime.sendInvocationResponse(resp).retryWhile(_.isInstanceOf[ConnectException])
} yield assert(res)(isUnit)
}

}
)

}

0 comments on commit 5d26ea0

Please sign in to comment.