From 475add299c09af3e144bafdc58cbc17056c54979 Mon Sep 17 00:00:00 2001 From: Mark Rudolph Date: Thu, 5 Dec 2024 22:58:23 -0500 Subject: [PATCH] Add some Response string interpolators --- .../spider/server/RequestHandler.test.scala | 53 ++++++++++++++++++- .../branch/spider/server/Response.scala | 28 ++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/branch/src/main/scala/dev/wishingtree/branch/spider/server/RequestHandler.test.scala b/branch/src/main/scala/dev/wishingtree/branch/spider/server/RequestHandler.test.scala index 38a9c86..8403927 100644 --- a/branch/src/main/scala/dev/wishingtree/branch/spider/server/RequestHandler.test.scala +++ b/branch/src/main/scala/dev/wishingtree/branch/spider/server/RequestHandler.test.scala @@ -1,8 +1,10 @@ package dev.wishingtree.branch.spider.server +import dev.wishingtree.branch.friday.http.JsonBodyHandler import dev.wishingtree.branch.spider.HttpMethod import dev.wishingtree.branch.spider.server.OpaqueSegments.* import dev.wishingtree.branch.spider.server.RequestHandler.given +import dev.wishingtree.branch.spider.server.Response.* import dev.wishingtree.branch.testkit.fixtures.HttpFixtureSuite import java.net.URI @@ -17,11 +19,31 @@ class RequestHandlerSpec extends HttpFixtureSuite { } } + val jsonHandler = new RequestHandler[Unit, String] { + override def handle(request: Request[Unit]): Response[String] = { + json""" + { + "message": "Aloha, World!" + } + """ + } + } + + val textHandler = new RequestHandler[Unit, String] { + override def handle(request: Request[Unit]): Response[String] = { + html""" + I am a text/html response. + """ + } + } + val alohaHandler: PartialFunction[(HttpMethod, Segments), RequestHandler[ Unit, String - ]] = { case HttpMethod.GET -> >> / "aloha" => - AlohaGreeter() + ]] = { + case HttpMethod.GET -> >> / "aloha" => AlohaGreeter() + case HttpMethod.GET -> >> / "jaloha" => jsonHandler + case HttpMethod.GET -> >> / "txt" => textHandler } httpFixture(alohaHandler).test("RequestHandler") { server => @@ -36,10 +58,37 @@ class RequestHandlerSpec extends HttpFixtureSuite { HttpResponse.BodyHandlers.ofString ) + case class Message(message: String) + val response2 = client.send( + HttpRequest.newBuilder + .uri( + URI.create(s"http://localhost:${server.getAddress.getPort}/jaloha") + ) + .build, + JsonBodyHandler.of[Message] + ) + + val response3 = client.send( + HttpRequest.newBuilder + .uri(URI.create(s"http://localhost:${server.getAddress.getPort}/txt")) + .build, + HttpResponse.BodyHandlers.ofString + ) + client.close() assertEquals(response.statusCode, 200) assertEquals(response.body, "Aloha") + + assertEquals(response2.statusCode, 200) + assertEquals(response2.body().get, Message("Aloha, World!")) + + assertEquals(response3.statusCode, 200) + assertEquals( + response3.body, + "I am a text/html response." + ) + } } diff --git a/branch/src/main/scala/dev/wishingtree/branch/spider/server/Response.scala b/branch/src/main/scala/dev/wishingtree/branch/spider/server/Response.scala index 653eb13..ea88506 100644 --- a/branch/src/main/scala/dev/wishingtree/branch/spider/server/Response.scala +++ b/branch/src/main/scala/dev/wishingtree/branch/spider/server/Response.scala @@ -13,6 +13,34 @@ case class Response[A]( object Response { + extension (sc: StringContext) { + + /** A string interpolator for creating a text/html response. + * + * {{{ + * html""" + *

Hello, $name!

+ * """ + * }}} + */ + def html(args: Any*): Response[String] = { + Response(sc.s(args*).trim, Map(ContentType.html.toHeader)) + } + + /** A string interpolator for creating an application/json response. + * + * {{{ + * json""" + * { + * "message": "Hello, $name!" + * } + * """ + * }}} + */ + def json(args: Any*): Response[String] = + Response(sc.s(args*).strip(), Map(ContentType.json.toHeader)) + } + extension [A](r: Response[A]) { /** Adds a header to the response.