Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion modules/http/src/main/scala/io/eezo/http/Request.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,14 @@ final case class Request(
* Both [[form]] and the `_method` override ask it, and they have to agree: an override that
* fired on a body [[form]] refuses to read would let a request that is not a form submission at
* all choose its own verb.
*
* The media type is compared without its parameters and without regard to case, as RFC 9110
* says, so `Application/X-WWW-Form-Urlencoded; charset=UTF-8` is a form too.
*/
private[http] def isFormEncoded: Boolean =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR body says that the _method override now runs for a mixed case form type. No suite shows it: the mixed case values are only in RequestSuite, and that test reads request.form only.

I put the old case sensitive startsWith check back in withMethodOverride only, and MethodOverrideSuite, CsrfSuite and RequestSuite stayed green (43 of 43). Thus form and the override can disagree again and no test fails. The scaladoc here says that they have to agree.

Please add a test to MethodOverrideSuite, for example "a mixed case form type is form encoded for the override too". A POST with Content-Type: Application/X-WWW-Form-Urlencoded and _method=PUT in the body becomes a PUT. Do the same check with _method=PUT in the query string.

header("Content-Type").exists(_.startsWith("application/x-www-form-urlencoded"))
header("Content-Type").exists(
_.takeWhile(_ != ';').trim.equalsIgnoreCase("application/x-www-form-urlencoded")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No test exercises .trim. I removed it, and RequestSuite, FormSuite, MethodOverrideSuite and CsrfSuite stayed green (83 of 83).

RFC 9110 permits optional whitespace before ;, and that is the construct .trim reads. CODING_STANDARDS.md asks for one test for each construct of a format the code reads.

Please add this value to the Seq in the new RequestSuite test:

"application/x-www-form-urlencoded ; charset=UTF-8"

)

/** A path parameter, converted. Throws [[BadRequest]], which the boundary maps to a 400. */
def param[A](name: String)(using from: FromPath[A]): A = Request.param(pathParams, name)
Expand Down
14 changes: 14 additions & 0 deletions modules/http/src/test/scala/io/eezo/http/MethodOverrideSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ class MethodOverrideSuite extends munit.FunSuite {
)
}

test("a mixed case form type is form encoded for the override too") {
val contentType = "Application/X-WWW-Form-Urlencoded"
assertEquals(
Request.withMethodOverride(post("_method=PUT", contentType = contentType)).method,
Method.PUT
)
assertEquals(
Request
.withMethodOverride(post(query = Map("_method" -> Seq("PUT")), contentType = contentType))
.method,
Method.PUT
)
}

test("a POST is never downgraded to a safe verb, whatever the field says") {
Seq(Method.GET, Method.HEAD, Method.OPTIONS).foreach { safe =>
assertEquals(Request.withMethodOverride(post(s"_method=$safe")).method, Method.POST, safe)
Expand Down
24 changes: 24 additions & 0 deletions modules/http/src/test/scala/io/eezo/http/RequestSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ class RequestSuite extends munit.FunSuite {
assertEquals(req.form, Map.empty[String, Seq[String]])
}

test("the form media type is read without regard to case or parameters") {
for (
contentType <- Seq(
"Application/X-WWW-Form-Urlencoded",
"APPLICATION/X-WWW-FORM-URLENCODED; charset=UTF-8",
"application/x-www-form-urlencoded;charset=utf-8",
"application/x-www-form-urlencoded ; charset=UTF-8"
)
) {
val req = request(
method = Method.POST,
headers = Map("Content-Type" -> Seq(contentType)),
body = "a=1".getBytes("UTF-8")
)
assertEquals(req.form, Map("a" -> Seq("1")), clue(contentType))
}
val other = request(
method = Method.POST,
headers = Map("Content-Type" -> Seq("application/x-www-form-urlencodedx")),
body = "a=1".getBytes("UTF-8")
)
assertEquals(other.form, Map.empty[String, Seq[String]])
}

test("a field with no value decodes to the empty string") {
val req = request(
method = Method.POST,
Expand Down