开发者

jetty ServletTester to POST a file?

Can I use jetty's ServletTester to send file by POST method? How? I tried to send raw request string:

 String reqString = "POST /avScan HTTP/1.1\r\n" +
                "Host: tester\r\n" +
                "\r\n" +
                "Content-Type: multipart/form-data; boundary=---------------------------121396480018623072891156223118\r\n" +
                "Content-Length: 63\r\n" +
                "-----------------------------121396480018623072891156223118\r\n" +
                "Content-Disposition: form-da开发者_运维百科ta; name=\"file\"; filename=\"test-file\"\r\n" +
                "Content-Type: application/octet-stream\r\n" +
                "Just some text file.\r\n" +
                "-----------------------------121396480018623072891156223118--\r\n";

ServletTester tester = new ServletTester();
tester.addServlet(FileReceiver.class, "/avScan");
tester.start();

HttpTester request = new HttpTester();
request.setContent(reqString);

String responseString = tester.getResponses(request.generate()); //Exception thrown
HttpTester response = new HttpTester();
response.parse(responseString);

However, I get java.lang.IllegalStateException: State==HEADER where I marked it in the code.

Can I test POST servlet receiving files with ServletTester? If not, what tool would you recommend?


From Jetty's MultipartFilterTest.java :

  HttpTester request = new HttpTester();
  String boundary="XyXyXy";
  request.setHeader("Content-Type","multipart/form-data; boundary="+boundary);
  String content = "--" + boundary + "\r\n"+
    "Content-Disposition: form-data; name=\"fileup\"; filename=\"test.upload\"\r\n"+
    "Content-Type: application/octet-stream\r\n\r\n"+
    "How now brown cow."+
    "\r\n--" + boundary + "-\r\n\r\n";

  request.setContent(content);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜