Skip to content

Commit

Permalink
Adds tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mabasic committed Nov 4, 2022
1 parent 0eb7f07 commit 1736396
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tags
.bloop/
metals.sbt
.vscode
null/

# npm
node_modules/
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package org.http4s
package scalatags

import _root_.scalatags.generic.Frag
import _root_.scalatags.Text.all.doctype
import _root_.scalatags.generic.Frag
import org.http4s.Charset.`UTF-8`
import org.http4s.headers.`Content-Type`

Expand Down
35 changes: 35 additions & 0 deletions scalatags/src/test/scala/org/http4s/scalatags/ScalatagsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,42 @@ class ScalatagsSuite extends CatsEffectSuite {

private def testBody() = {
import Text.all

all.div()(
all.p()(all.raw("this is my testBody"))
)
}

private def testDoctypeBody() = {
import Text.all._
import Text.tags2.title
import Text.all.doctype

doctype("html")(
html(
lang := "en",
head(
title("http4s-scalatags")
),
body(this.testBody()),
)
)
}

test("TypedTag encoder should return Content-Type text/html with proper charset") {
testCharsets.map { implicit cs =>
val headers = EntityEncoder[IO, Text.TypedTag[String]].headers
assertEquals(headers.get[`Content-Type`], Some(`Content-Type`(MediaType.text.html, Some(cs))))
}
}

test("Doctype encoder should return Content-Type text/html with proper charset") {
testCharsets.map { implicit cs =>
val headers = EntityEncoder[IO, Text.all.doctype].headers
assertEquals(headers.get[`Content-Type`], Some(`Content-Type`(MediaType.text.html, Some(cs))))
}
}

test("TypedTag encoder should render the body") {
implicit val cs: Charset = Charset.`UTF-8`
val resp = Response[IO](Ok).withEntity(testBody())
Expand All @@ -59,4 +83,15 @@ class ScalatagsSuite extends CatsEffectSuite {
.assertEquals(Right("<div><p>this is my testBody</p></div>"))
}

test("Doctype encoder should render the body") {
implicit val cs: Charset = Charset.`UTF-8`

val resp = Response[IO](Ok).withEntity(testDoctypeBody())

EntityDecoder
.text[IO]
.decode(resp, strict = false)
.value
.assertEquals(Right("<!DOCTYPE html><html lang=\"en\"><head><title>http4s-scalatags</title></head><body><div><p>this is my testBody</p></div></body></html>"))
}
}

0 comments on commit 1736396

Please sign in to comment.