-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes form submissions with empty file fields throwing exceptions
- Loading branch information
nk
committed
Nov 12, 2024
1 parent
fae83f8
commit c722ec2
Showing
6 changed files
with
96 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
26 changes: 26 additions & 0 deletions
26
example/multipartFormSubmission/app/src/MultipartFormSubmission.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package app | ||
|
||
object MultipartFormSubmission extends cask.MainRoutes { | ||
|
||
@cask.get("/") | ||
def index() = | ||
cask.model.Response( | ||
""" | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head></head> | ||
<body> | ||
<form action="/post" method="post" enctype="multipart/form-data"> | ||
<input type="file" id="somefile" name="somefile"> | ||
<button type="submit">Submit</button> | ||
</form> | ||
</body> | ||
</html> | ||
""", 200, Seq(("Content-Type", "text/html"))) | ||
|
||
@cask.postForm("/post") | ||
def post(somefile: cask.FormFile) = | ||
s"filename: ${somefile.fileName}" | ||
|
||
initialize() | ||
} |
38 changes: 38 additions & 0 deletions
38
example/multipartFormSubmission/app/test/src/ExampleTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package app | ||
import io.undertow.Undertow | ||
|
||
import utest._ | ||
|
||
object ExampleTests extends TestSuite{ | ||
def withServer[T](example: cask.main.Main)(f: String => T): T = { | ||
val server = Undertow.builder | ||
.addHttpListener(8081, "localhost") | ||
.setHandler(example.defaultHandler) | ||
.build | ||
server.start() | ||
val res = | ||
try f("http://localhost:8081") | ||
finally server.stop() | ||
res | ||
} | ||
|
||
val tests = Tests{ | ||
test("MultipartFormSubmission") - withServer(MultipartFormSubmission){ host => | ||
val classPath = System.getProperty("java.class.path", "."); | ||
val elements = classPath.split(System.getProperty("path.separator")); | ||
elements.filter(e => e.endsWith("/app/resources")).headOption.map(resourcePath => { | ||
val withFile = requests.post(s"$host/post", data = requests.MultiPart( | ||
requests.MultiItem("somefile", new java.io.File(s"$resourcePath/example.txt"), "example.txt"), | ||
)) | ||
withFile.text() ==> s"filename: example.txt" | ||
withFile.statusCode ==> 200 | ||
|
||
val withoutFile = requests.post(s"$host/post", data = requests.MultiPart( | ||
requests.MultiItem("somefile", Array[Byte]()), | ||
)) | ||
withoutFile.text() ==> s"filename: null" | ||
withoutFile.statusCode ==> 200 | ||
}).isDefined ==> true | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package build.example.multipartFormSubmission | ||
import mill._, scalalib._ | ||
|
||
object app extends Cross[AppModule](build.scalaVersions) | ||
trait AppModule extends CrossScalaModule{ | ||
|
||
def moduleDeps = Seq(build.cask(crossScalaVersion)) | ||
|
||
def ivyDeps = Agg[Dep]( | ||
) | ||
object test extends ScalaTests with TestModule.Utest{ | ||
|
||
def ivyDeps = Agg( | ||
ivy"com.lihaoyi::utest::0.8.4", | ||
ivy"com.lihaoyi::requests::0.9.0", | ||
) | ||
} | ||
} |