unit-testing - 处理大型查询错误并发送 413 响应
问题描述
我有一个语音笔记上传功能。我正在编写测试,作为其中的一部分,我为较大的文件编写了以下测试,我希望它抛出 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
解决方案
好的,所以我设法通过以下方式解决了我的问题:
将我的测试中的断言更改为以下内容:
assert_error_sent(413, fn -> conn |> post chatroom_chatroom_path conn, :send_voice_note, chatroom_data.id, body: voice_note_file end
并
Plug.Exception
在我的实施error_view.ex
:defimpl Plug.Exception, for: Plug.Conn.InvalidQueryError do def status(_exception), do: 413 end
推荐阅读
- python - 绘制熊猫 DataframeGroupbySeries 的 HIST
- javascript - 电子没办法换驱动?
- spring - 如果注解包含参数,建议不会执行
- google-chrome - Firefox Native addon(win7):如何在给定 Windows HWND 的情况下获取 window.id
- javascript - Chrome 扩展:如何在 2021 年安装类似 choex 的 ES6 npm 脚本?
- laravel - 如何在 Laravel 6 中隐藏 url 中的 id?
- ruby-on-rails - 安装 ruby-opencv gem 时出错,找不到 opencv_calib3d
- php - 如何在 Xampp 中将一个包从 github 安装到普通 PHP 中
- swift - 当我想过滤数据时,领域中的错误属性名称无效,找不到属性
- r - 计算 R 中数据帧中每一行的前导零和尾随零