Skip to content

Commit

Permalink
Add some Response string interpolators
Browse files Browse the repository at this point in the history
  • Loading branch information
alterationx10 committed Dec 6, 2024
1 parent 351c69d commit 475add2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 =>

Expand All @@ -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."
)

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@ case class Response[A](

object Response {

extension (sc: StringContext) {

/** A string interpolator for creating a text/html response.
*
* {{{
* html"""
* <h1>Hello, $name!</h1>
* """
* }}}
*/
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.
Expand Down

0 comments on commit 475add2

Please sign in to comment.