dreamhead / moco

Easy Setup Stub Server
MIT License
4.34k stars 1.08k forks source link

Does MOCO support multipart/form ? #175

Open kinoooolu opened 7 years ago

kinoooolu commented 7 years ago

Hi Dreamhead As tittle, when I send multipart/form request, 400 bad request will be got... Any solution or workaround for this? Thanks!

kinoooolu commented 7 years ago

POST /application/deploy HTTP/1.1 Authorization: Basic YXBpOm1hbmFnZXI= Content-Length: 1780 Content-Type: multipart/form-data; boundary=ChnId1zKDLlln7wtZzoeIt6dmuBTcG_C Host: localhost:22222 Connection: Keep-Alive User-Agent: Apache-HttpClient/4.5.2 (Java/1.8.0_45) Accept-Encoding: gzip,deflate

19 Dec 2016 17:18:30 [nioEventLoopGroup-2-2] INFO Response return: HTTP/1.1 400
dreamhead commented 7 years ago

I'm not quite sure how you use form in your code. An example is as following:

server.post(eq(form("name"), "dreamhead")).response("foobar");

Refer to form for more information.

macdao commented 7 years ago

I'm trying to use JSON format to mock file upload (a Multipart request), but moco cannot support it.

The reason is Multipart request contains a generated boundary:

  {
    "description": "user_can_upload_file",
    "request": {
      "uri": "/api/files",
      "method": "POST",
      "headers": {
        "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryKgNrZfGWAQ59LuVd"
      },
      "file": "api/multipart.txt"
    },
    "response": {
      "status": 201,
      "headers": {
        "Location": "http://{host}:{port}/api/files/{file-id}"
      }
    }
  }

api/multipart.txt:

------WebKitFormBoundaryKgNrZfGWAQ59LuVd
Content-Disposition: form-data; name="file"; filename="hello.txt"
Content-Type: text/plain

hello
------WebKitFormBoundaryKgNrZfGWAQ59LuVd
Content-Disposition: form-data; name="name"

dreamhead
------WebKitFormBoundaryKgNrZfGWAQ59LuVd--