我有一个语音备忘上传功能。我正在写测试,作为测试的一部分,我为更大的文件写了以下测试,我想让它抛出413实体过大的错误:
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
好的,所以我设法通过以下方式解决了我的问题:
-
将测试中的断言更改为以下内容:
assert_error_sent(413, fn -> conn |> post chatroom_chatroom_path conn, :send_voice_note, chatroom_data.id, body: voice_note_file end
-
并在我的
error_view.ex
:中实现Plug.Exception
defimpl Plug.Exception, for: Plug.Conn.InvalidQueryError do def status(_exception), do: 413 end