unit-testing - 没有使用scalatra的Jetty的servlet错误的multipartconfig

标签 unit-testing rest embedded-jetty scala-2.10 scalatra

我正在尝试对上传调用进行单元测试,但以下代码却出现此错误:

@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]

这是我的单元测试,第一个成功,因此它正在查找我的Web服务:
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中运行此程序,我不确定发生了什么。

使用HttpPostMultipartEntity可以很好地工作,因此Eclipse或scalatra规范框架的工作方式似乎是一个问题。

任何想法可能出了什么问题吗?

我没有单独的web.xml。

正如我在build.sbt中所使用的那样,我只使用码头8.1.10:

"org.eclipse.jetty" % "jetty-webapp" % "8.1.10.v20130312" % "container"



最佳答案

这是我在使用ServletContextHandler而不是WebAppContext时发现的解决方案。从这里:https://bugs.eclipse.org/bugs/show_bug.cgi?id=395000

import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import javax.servlet.MultipartConfigElement;

public class WebServer {

    protected Server server;

    public static void main(String[] args) throws Exception {
        int port = 8080;
        Server server = new Server(port);

        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
        context.setContextPath("/");


        ServletHolder fileUploadServletHolder = new ServletHolder(new FileUploadServlet());
        fileUploadServletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement("data/tmp"));
        context.addServlet(fileUploadServletHolder, "/fileUpload");

        server.setHandler(context);
        server.start();
        server.join();
    }
}

关于unit-testing - 没有使用scalatra的Jetty的servlet错误的multipartconfig,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18020437/

相关文章:

java - 如何配置嵌入式 jetty 访问泽西资源?

java - 可以使用嵌入式 jetty 以编程方式设置上下文阀和 Realm 吗?

unit-testing - Aurelia 单元测试访问组件的 viewModel

java - 用于维护 Java 中的 Object 方法契约的 Automagic 单元测试?

ruby-on-rails - 如何利用 REST 将 Android 设备中的 GPS 数据发布到 Ruby on Rails 应用程序中?

javascript - 正确返回获取的 JSON,但无法将其放入 Chart.JS

embedded-jetty - 在嵌入式 Jetty 上启用 TLS-1.2

c# - .NET 工具 : Extract Interface and Implement Wrapper Class

java - 由于 java.lang.NoClassDefFoundError : jdk/internal/reflect/GeneratedSerializationConstructorAccessor1 无法运行 gradle 测试任务

angularjs - Angular 和 CORS