Skip to content

Commit 2c10fca

Browse files
committed
Improve docs and tests
1 parent 2c450a7 commit 2c10fca

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

lib/mint/http.ex

+6-3
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,9 @@ defmodule Mint.HTTP do
903903
Contrary to `recv/3`, this function does not return partial responses on errors. Use
904904
`recv/3` for full control.
905905
906+
This function only handles responses for the given `request_ref`. Do not use it when making
907+
concurrent requests as responses other than for `request_ref` are ignored.
908+
906909
## Examples
907910
908911
iex> {:ok, conn} = Mint.HTTP.connect(:https, "httpbin.org", 443, mode: :passive)
@@ -922,9 +925,9 @@ defmodule Mint.HTTP do
922925
headers: [{binary(), binary()}],
923926
body: binary()
924927
}
925-
def recv_response(conn, ref, timeout)
926-
when is_reference(ref) and Util.is_timeout(timeout) do
927-
recv_response([], {nil, [], ""}, conn, ref, timeout)
928+
def recv_response(conn, request_ref, timeout)
929+
when is_reference(request_ref) and Util.is_timeout(timeout) do
930+
recv_response([], {nil, [], ""}, conn, request_ref, timeout)
928931
end
929932

930933
defp recv_response(

test/mint/http1/conn_test.exs

+26
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,32 @@ defmodule Mint.HTTP1Test do
482482
}
483483
end
484484

485+
test "handles trailers", %{port: port, server_ref: server_ref} do
486+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", port, mode: :passive)
487+
assert_receive {^server_ref, server_socket}
488+
489+
{:ok, conn, ref} = HTTP1.request(conn, "GET", "/", [], nil)
490+
491+
:ok = :gen_tcp.send(server_socket, "HTTP/1.1 200 OK\r\n")
492+
:ok = :gen_tcp.send(server_socket, "transfer-encoding: chunked\r\n")
493+
:ok = :gen_tcp.send(server_socket, "trailer: x-trailer\r\n\r\n")
494+
:ok = :gen_tcp.send(server_socket, "5\r\nhello\r\n")
495+
:ok = :gen_tcp.send(server_socket, "5\r\nworld\r\n0\r\n")
496+
:ok = :gen_tcp.send(server_socket, "x-trailer: foo\r\n\r\n")
497+
498+
assert {:ok, _conn, response} = Mint.HTTP.recv_response(conn, ref, 100)
499+
500+
assert response == %{
501+
body: "helloworld",
502+
headers: [
503+
{"transfer-encoding", "chunked"},
504+
{"trailer", "x-trailer"},
505+
{"x-trailer", "foo"}
506+
],
507+
status: 200
508+
}
509+
end
510+
485511
test "handles errors", %{port: port, server_ref: server_ref} do
486512
assert {:ok, conn} = HTTP1.connect(:http, "localhost", port, mode: :passive)
487513
assert_receive {^server_ref, server_socket}

test/mint/http2/conn_test.exs

+49
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,55 @@ defmodule Mint.HTTP2Test do
22062206
assert HTTP2.open?(conn)
22072207
end
22082208

2209+
test "handles trailers", %{conn: conn} do
2210+
{conn, ref} = open_request(conn)
2211+
2212+
assert_recv_frames [headers(stream_id: stream_id)]
2213+
2214+
<<trailer_hbf1::1-bytes, trailer_hbf2::binary>> =
2215+
server_encode_headers([{"x-trailer", "foo"}])
2216+
2217+
data =
2218+
server_encode_frames([
2219+
headers(
2220+
stream_id: stream_id,
2221+
hbf:
2222+
server_encode_headers([
2223+
{":status", "200"},
2224+
{"content-type", "text/plain"}
2225+
]),
2226+
flags: set_flags(:headers, [:end_headers])
2227+
),
2228+
data(
2229+
stream_id: stream_id,
2230+
data: "helloworld",
2231+
flags: set_flags(:data, [])
2232+
),
2233+
headers(
2234+
stream_id: stream_id,
2235+
hbf: trailer_hbf1,
2236+
flags: set_flags(:headers, [:end_stream])
2237+
),
2238+
continuation(
2239+
stream_id: stream_id,
2240+
hbf: trailer_hbf2,
2241+
flags: set_flags(:continuation, [:end_headers])
2242+
)
2243+
])
2244+
2245+
:ok = :ssl.send(server_get_socket(), data)
2246+
2247+
assert {:ok, _conn, response} = Mint.HTTP.recv_response(conn, ref, 100)
2248+
2249+
assert response == %{
2250+
body: "helloworld",
2251+
headers: [{"content-type", "text/plain"}, {"x-trailer", "foo"}],
2252+
status: 200
2253+
}
2254+
2255+
assert HTTP2.open?(conn)
2256+
end
2257+
22092258
test "handles errors", %{conn: conn} do
22102259
{conn, ref} = open_request(conn)
22112260
assert_recv_frames [headers(stream_id: _stream_id)]

0 commit comments

Comments
 (0)