0

我有一个语音笔记上传功能。我正在编写测试,作为其中的一部分,我为较大的文件编写了以下测试,我希望它抛出 413 Entity too large 错误:

test "send VN fail when too large", %{conn: conn} do
  Couchdb.Connector.Storage.storage_down(CouchdbHelperFunctions.db_props(@create_user_data.username))

  {:ok, user_data} = UserManager.create_user(@create_user_data)
  {:ok, token, _} = UserManager.authenticate(@create_user_data.username, @create_user_data.password)

  conn = put_req_header(conn, "authorization", "Bearer " <> token)
  {:ok, chatroom_data} = ChatroomManager.create_chatroom(user_data.id, "test")
  {:ok, voice_note_file} = File.read("test/4051.mp3")

  conn = put_req_header(conn, "content-type", "application/octet-stream")

  response =
    conn
    |> post chatroom_chatroom_path conn, :send_voice_note, chatroom_data.id, body: voice_note_file

  assert response.status == 413
end

测试失败了:

 1) test send_voice_note/2 send VN fail when too large (BackendWeb.ChatroomControllerTest)
 test/backend_web/controllers/chatroom_controller_test.exs:233
 ** (Plug.Conn.InvalidQueryError) maximum query string length is 1000000, got a query with 1242763 bytes
 code: |> post chatroom_chatroom_path conn, :send_voice_note, chatroom_data.id, body: voice_note_file
 stacktrace:
   (plug) lib/plug/conn.ex:845: Plug.Conn.fetch_query_params/2
   (plug) lib/plug/parsers.ex:256: Plug.Parsers.call/2
   (backend) lib/backend_web/endpoint.ex:1: BackendWeb.Endpoint.plug_builder_call/2
   (backend) lib/backend_web/endpoint.ex:1: BackendWeb.Endpoint.call/2
   (phoenix) lib/phoenix/test/conn_test.ex:224: Phoenix.ConnTest.dispatch/5
   test/backend_web/controllers/chatroom_controller_test.exs:245: (test)

我还尝试将其添加到我error_view.ex的列表中,希望它能够做出响应,但没有成功。如果我在错误的地方添加它有什么想法吗?

defimpl Plug.Exception, for: Plug.Conn.InvalidQueryError do
  def status(_exception), do: 413
end
4

1 回答 1

1

好的,所以我设法通过以下方式解决了我的问题:

  1. 将我的测试中的断言更改为以下内容:

    assert_error_sent(413, fn ->
      conn
      |> post chatroom_chatroom_path conn, :send_voice_note, chatroom_data.id, body: voice_note_file    
    end
    
  2. Plug.Exception在我的实施error_view.ex

    defimpl Plug.Exception, for: Plug.Conn.InvalidQueryError do
      def status(_exception), do: 413
    end
    
于 2018-11-04T15:47:44.647 回答