Skip to content

Commit

Permalink
Improve docs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtekmach committed Aug 4, 2024
1 parent 2c450a7 commit 2c10fca
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/mint/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,9 @@ defmodule Mint.HTTP do
Contrary to `recv/3`, this function does not return partial responses on errors. Use
`recv/3` for full control.
This function only handles responses for the given `request_ref`. Do not use it when making
concurrent requests as responses other than for `request_ref` are ignored.
## Examples
iex> {:ok, conn} = Mint.HTTP.connect(:https, "httpbin.org", 443, mode: :passive)
Expand All @@ -922,9 +925,9 @@ defmodule Mint.HTTP do
headers: [{binary(), binary()}],
body: binary()
}
def recv_response(conn, ref, timeout)
when is_reference(ref) and Util.is_timeout(timeout) do
recv_response([], {nil, [], ""}, conn, ref, timeout)
def recv_response(conn, request_ref, timeout)
when is_reference(request_ref) and Util.is_timeout(timeout) do
recv_response([], {nil, [], ""}, conn, request_ref, timeout)
end

defp recv_response(
Expand Down
26 changes: 26 additions & 0 deletions test/mint/http1/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,32 @@ defmodule Mint.HTTP1Test do
}
end

test "handles trailers", %{port: port, server_ref: server_ref} do
assert {:ok, conn} = HTTP1.connect(:http, "localhost", port, mode: :passive)
assert_receive {^server_ref, server_socket}

{:ok, conn, ref} = HTTP1.request(conn, "GET", "/", [], nil)

:ok = :gen_tcp.send(server_socket, "HTTP/1.1 200 OK\r\n")
:ok = :gen_tcp.send(server_socket, "transfer-encoding: chunked\r\n")
:ok = :gen_tcp.send(server_socket, "trailer: x-trailer\r\n\r\n")
:ok = :gen_tcp.send(server_socket, "5\r\nhello\r\n")
:ok = :gen_tcp.send(server_socket, "5\r\nworld\r\n0\r\n")
:ok = :gen_tcp.send(server_socket, "x-trailer: foo\r\n\r\n")

assert {:ok, _conn, response} = Mint.HTTP.recv_response(conn, ref, 100)

assert response == %{
body: "helloworld",
headers: [
{"transfer-encoding", "chunked"},
{"trailer", "x-trailer"},
{"x-trailer", "foo"}
],
status: 200
}
end

test "handles errors", %{port: port, server_ref: server_ref} do
assert {:ok, conn} = HTTP1.connect(:http, "localhost", port, mode: :passive)
assert_receive {^server_ref, server_socket}
Expand Down
49 changes: 49 additions & 0 deletions test/mint/http2/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2206,6 +2206,55 @@ defmodule Mint.HTTP2Test do
assert HTTP2.open?(conn)
end

test "handles trailers", %{conn: conn} do
{conn, ref} = open_request(conn)

assert_recv_frames [headers(stream_id: stream_id)]

<<trailer_hbf1::1-bytes, trailer_hbf2::binary>> =
server_encode_headers([{"x-trailer", "foo"}])

data =
server_encode_frames([
headers(
stream_id: stream_id,
hbf:
server_encode_headers([
{":status", "200"},
{"content-type", "text/plain"}
]),
flags: set_flags(:headers, [:end_headers])
),
data(
stream_id: stream_id,
data: "helloworld",
flags: set_flags(:data, [])
),
headers(
stream_id: stream_id,
hbf: trailer_hbf1,
flags: set_flags(:headers, [:end_stream])
),
continuation(
stream_id: stream_id,
hbf: trailer_hbf2,
flags: set_flags(:continuation, [:end_headers])
)
])

:ok = :ssl.send(server_get_socket(), data)

assert {:ok, _conn, response} = Mint.HTTP.recv_response(conn, ref, 100)

assert response == %{
body: "helloworld",
headers: [{"content-type", "text/plain"}, {"x-trailer", "foo"}],
status: 200
}

assert HTTP2.open?(conn)
end

test "handles errors", %{conn: conn} do
{conn, ref} = open_request(conn)
assert_recv_frames [headers(stream_id: _stream_id)]
Expand Down

0 comments on commit 2c10fca

Please sign in to comment.