scalatra/scalatra — pull request #192 by mnylen (2012-05-25 20:59:21+00:00)
Title: Use regular POST/PUT when using post/put overloaded with empty files and...

... params

Fix a small issue using post/put overloaded with the files map
in ScalatraTests. Whenever the user would use the overloaded
method and provide empty files map AND params map, it would
generate a multipart/form-data request with empty body.

This causes a problem with Jetty, which throws IOException with message
"Missing initial multi part boundary" whenever it tries to parse a
multipart request with zero parts, and thus indeed, a one not having
the initial boundary.

The changes in this commit fix the issue by handling the special
case of params.isEmpty && files.isEmpty by falling back to regular
submit unless the user explicitly specified "multipart/form-data"
for Content-Type header.

Having this change enables users to do something like this
without needing any special handling from their part:

def changeAvatar[A](userId: Int = 123, file: File = defaultFile)(f: => A): A = {
put("/users/" + userId + "/avatar",
Map[String, String](),
Map("file" -> file)) { f }
}

test("user can change avatar") {
changeAvatar {
assert(user avatar has changed)
}
}

test("changing avatar without providing file fails") {
changeAvatar(file = null) {
assert(avatar was not changed)
assert(status === 400)
}
}