|
| 1 | +package com.springer.samatra.extras.cats |
| 2 | + |
| 3 | +import cats.effect.IO |
| 4 | +import com.springer.samatra.extras.cats.IoResponses.{IoResponse, Timeout, fromIOWithTimeout} |
| 5 | +import com.springer.samatra.routing.Routings.{Controller, HttpResp, Routes} |
| 6 | +import com.springer.samatra.routing.StandardResponses.Implicits.{fromFile, fromString} |
| 7 | +import io.netty.handler.codec.http.DefaultHttpHeaders |
| 8 | +import org.asynchttpclient.Dsl.asyncHttpClient |
| 9 | +import org.asynchttpclient.Response |
| 10 | +import org.eclipse.jetty.server.handler.gzip.GzipHandler |
| 11 | +import org.eclipse.jetty.server.{Connector, Server, ServerConnector} |
| 12 | +import org.eclipse.jetty.servlet.{ServletContextHandler, ServletHolder} |
| 13 | +import org.scalatest.BeforeAndAfterAll |
| 14 | +import org.scalatest.funspec.AnyFunSpec |
| 15 | +import org.scalatest.matchers.should.Matchers.convertToAnyShouldWrapper |
| 16 | + |
| 17 | +import scala.concurrent.duration.DurationInt |
| 18 | +import scala.jdk.CollectionConverters.{CollectionHasAsScala, IterableHasAsJava} |
| 19 | +import cats.effect.unsafe.implicits.global |
| 20 | +import com.springer.samatra.routing.StandardResponses.{AddCookie, Halt, Redirect, WithCookies, WithHeaders} |
| 21 | + |
| 22 | +import java.nio.file.Paths |
| 23 | + |
| 24 | +class CatsEffectEndToEndTest extends AnyFunSpec with BeforeAndAfterAll { |
| 25 | + |
| 26 | + private val server = new Server() { |
| 27 | + addConnector(new ServerConnector(this) { |
| 28 | + setPort(0) |
| 29 | + }) |
| 30 | + } |
| 31 | + |
| 32 | + private val handler = new GzipHandler() |
| 33 | + handler.setHandler(new ServletContextHandler() { |
| 34 | + setContextPath("/test") |
| 35 | + |
| 36 | + addServlet(new ServletHolder( |
| 37 | + Routes(new Controller { |
| 38 | + |
| 39 | + get("/relative") { req => |
| 40 | + IO.sleep(100.millis).map { _ => |
| 41 | + req.relativePath |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + get("/morethanone/:type") { req => |
| 46 | + IO[HttpResp] { |
| 47 | + req.captured("type") match { |
| 48 | + case "redirect" => Redirect("/getandpost") |
| 49 | + case "string" => "String" |
| 50 | + case "Error" => Halt(500, Some(new RuntimeException("error"))) |
| 51 | + case "NotFound" => Halt(404) |
| 52 | + case "file" => WithHeaders("Content-Type" -> "application/xml") { |
| 53 | + Paths.get("build.sbt") |
| 54 | + } |
| 55 | + case "headers" => WithHeaders("hi" -> "there")("body") |
| 56 | + case "cookies" => |
| 57 | + WithCookies(AddCookie("cookie", "tasty"))("body") |
| 58 | + case "securedcookies" => |
| 59 | + WithCookies(AddCookie("cookie", "tasty", httpOnly = true))("body") |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + //use explicits |
| 65 | + get("/timeout") { _ => |
| 66 | + fromIOWithTimeout(IO.sleep(100.second).map { _ => |
| 67 | + "unexpceted" |
| 68 | + })(Timeout(200)) |
| 69 | + } |
| 70 | + |
| 71 | + })), "/cats/*") |
| 72 | + }) |
| 73 | + |
| 74 | + server.setHandler(handler) |
| 75 | + |
| 76 | + it("has correct relative path after async") { |
| 77 | + get("/test/cats/relative").getResponseBody shouldBe "/relative" |
| 78 | + } |
| 79 | + |
| 80 | + it("HEAD should return 200, 302, 404 and 500 error codes") { |
| 81 | + head("/test/cats/morethanone/Error").getStatusCode shouldBe 500 |
| 82 | + head("/test/cats/morethanone/NotFound").getStatusCode shouldBe 404 |
| 83 | + head("/test/cats/morethanone/redirect").getStatusCode shouldBe 302 |
| 84 | + head("/test/cats/morethanone/string").getStatusCode shouldBe 200 |
| 85 | + head("/test/cats/morethanone/headers").getHeader("hi") shouldBe "there" |
| 86 | + |
| 87 | + val cookies = head("/test/cats/morethanone/cookies").getCookies |
| 88 | + val cookie = cookies.asScala.collectFirst { |
| 89 | + case c if c.name() == "cookie" => c.value() |
| 90 | + } |
| 91 | + |
| 92 | + cookie shouldBe Some("tasty") |
| 93 | + } |
| 94 | + |
| 95 | + it("can timeout") { |
| 96 | + val res = get("/test/cats/timeout") |
| 97 | + res.getStatusCode shouldBe 500 |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + def head(path: String): Response = asyncHttpClient.prepareHead(s"$host$path").execute().get() |
| 102 | + |
| 103 | + def get(path: String, headers: Map[String, Seq[String]] = Map.empty): Response = { |
| 104 | + val hs = new DefaultHttpHeaders() |
| 105 | + headers.foreach { case (k, v) => hs.add(k, v.asJava) } |
| 106 | + |
| 107 | + asyncHttpClient.prepareGet(s"$host$path") |
| 108 | + .setHeaders(hs) |
| 109 | + .execute().get() |
| 110 | + } |
| 111 | + |
| 112 | + override protected def beforeAll(): Unit = { |
| 113 | + server.start() |
| 114 | + val connectors: Array[Connector] = server.getConnectors |
| 115 | + val port: Int = connectors(0).asInstanceOf[ServerConnector].getLocalPort |
| 116 | + |
| 117 | + host = s"http://localhost:$port" |
| 118 | + } |
| 119 | + |
| 120 | + override protected def afterAll(): Unit = server.stop() |
| 121 | + |
| 122 | + var host: String = _ |
| 123 | +} |
0 commit comments