我正在尝试对上传调用进行单元测试,但以下代码出现此错误:
@MultipartConfig(maxFileSize = 3145728)
class WebServlet extends ScalatraServlet with FileUploadSupport {
override def isSizeConstraintException(e: Exception) = e match {
case se: ServletException if se.getMessage.contains("exceeds max filesize") ||
se.getMessage.startsWith("Request exceeds maxRequestSize") => true
case _ => false
}
error {
case e: SizeConstraintExceededException => RequestEntityTooLarge("too much!")
}
post("/uploadscript") {
val privateParam = try {params("private") != null && params("private").equals("true") } catch { case _ => false }
println("privateParam = " + privateParam)
val file = fileParams("file")
println(s"The size of the file is ${file.size}")
}
错误是:
java.lang.IllegalStateException: No multipart config for servlet
at org.eclipse.jetty.server.Request.getParts(Request.java:2064) ~[jetty-server-8.1.10.v20130312.jar:8.1.10.v20130312]
at org.scalatra.servlet.FileUploadSupport$class.getParts(FileUploadSupport.scala:133) ~[scalatra_2.10-2.2.1.jar:2.2.1]
at org.scalatra.servlet.FileUploadSupport$class.extractMultipartParams(FileUploadSupport.scala:108) ~[scalatra_2.10-2.2.1.jar:2.2.1]
at org.scalatra.servlet.FileUploadSupport$class.handle(FileUploadSupport.scala:79) ~[scalatra_2.10-2.2.1.jar:2.2.1]
at com.ui.WebServlet.handle(WebServlet.scala:32) ~[classes/:na]
这是我的单元测试,第一个成功了,所以它正在寻找我的网络服务:
class WebServletSpecification extends MutableScalatraSpec {
addServlet(classOf[WebServlet], "/*")
"GET /hello" should {
"return status 200" in {
get("/hello/testcomputer") {
status must_== 200
}
}
}
"POST /uploadscript" should {
"return status 200" in {
val scriptfile = "testfile"
val scriptname = "basescript"
post("/uploadscript", Map("private" -> "true"), Map("file" -> new File(scriptfile))) {
status must_== 200
}
}
}
}
我在 Eclipse 中运行它,我不确定发生了什么。
它可以很好地使用HttpPost
,MultipartEntity
因此它似乎是 Eclipse 或 scalatra 规范框架如何工作的问题。
知道可能出了什么问题吗?
我没有单独的 web.xml。
从我在 build.sbt 中使用的内容可以看出,我只使用了 jetty 8.1.10:
“org.eclipse.jetty”%“jetty-webapp”%“8.1.10.v20130312”%“容器”
,