Skip to content

Commit

Permalink
Enable cask.UnknownQueryParams and cask.RemainingPathSegments for for…
Browse files Browse the repository at this point in the history
…m endpoints (#110)
  • Loading branch information
lihaoyi authored Jan 4, 2024
1 parent addb2d3 commit e7c8373
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cask/src/cask/endpoints/FormEndpoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ object FormReader{
implicit def paramFormReader[T: QueryParamReader]: FormReader[T] = new FormReader[T]{
def arity = implicitly[QueryParamReader[T]].arity

override def unknownQueryParams: Boolean = implicitly[QueryParamReader[T]].unknownQueryParams

override def remainingPathSegments: Boolean = implicitly[QueryParamReader[T]].remainingPathSegments
def read(ctx: Request, label: String, input: Seq[FormEntry]) = {
implicitly[QueryParamReader[T]].read(ctx, label, if (input == null) null else input.map(_.valueOrFileName))
}
Expand Down
8 changes: 8 additions & 0 deletions example/formJsonPost/app/src/FormJsonPost.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,13 @@ object FormJsonPost extends cask.MainRoutes{
"OK " + value1 + " " + value2 + " " + params.value + " " + segments.value
}

@cask.postForm("/form-extra")
def formEndpointExtra(value1: cask.FormValue,
value2: Seq[Int],
params: cask.QueryParams,
segments: cask.RemainingPathSegments) = {
"OK " + value1 + " " + value2 + " " + params.value + " " + segments.value
}

initialize()
}
17 changes: 17 additions & 0 deletions example/formJsonPost/app/test/src/ExampleTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ object ExampleTests extends TestSuite{
s"$host/json-extra/omg/wtf/bbq?iam=cow&hearme=moo",
data = """{"value1": true, "value2": [3]}"""
)

val text6 = response6.text()
assert(
text6 == "\"OK true List(3) Map(hearme -> ArraySeq(moo), iam -> ArraySeq(cow)) List(omg, wtf, bbq)\"" ||
text6 == "\"OK true Vector(3) Map(hearme -> WrappedArray(moo), iam -> WrappedArray(cow)) List(omg, wtf, bbq)\""
)

val response7 = requests.post(
s"$host/form-extra/omg/wtf/bbq?iam=cow&hearme=moo",
data = Seq("value1" -> "hello", "value2" -> "1", "value2" -> "2")
)

val text7 = response7.text()
assert(
text7 == "OK FormValue(hello,null) List(1, 2) Map(hearme -> ArraySeq(moo), iam -> ArraySeq(cow)) List(omg, wtf, bbq)" ||
text7 == "OK FormValue(hello,null) List(1, 2) Map(hearme -> WrappedArray(moo), iam -> WrappedArray(cow)) List(omg, wtf, bbq)"
)
}
}
}

0 comments on commit e7c8373

Please sign in to comment.