Skip to content

Commit a07de1b

Browse files
authored
Merge pull request #99 from PSPDFKit-labs/replace-gun-with-mint
Replace gun with mint
2 parents 8e4b4d8 + ae0b25d commit a07de1b

File tree

3 files changed

+65
-77
lines changed

3 files changed

+65
-77
lines changed

mix.exs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ defmodule Bypass.Mixfile do
1111
elixir: "~> 1.6",
1212
description: description(),
1313
package: package(),
14-
deps: deps(Mix.env()),
14+
deps: deps(),
1515
docs: docs()
1616
]
1717
end
@@ -28,30 +28,18 @@ defmodule Bypass.Mixfile do
2828
[
2929
{:plug_cowboy, "~> 1.0 or ~> 2.0"},
3030
{:plug, "~> 1.7"},
31+
{:cowlib, "~> 1.0.1"},
32+
{:ranch, "~> 1.3.2"},
3133
{:ex_doc, "> 0.0.0", only: :dev},
32-
{:espec, "~> 1.6", only: [:dev, :test]}
34+
{:espec, "~> 1.6", only: [:dev, :test]},
35+
{:mint, "~> 1.1", only: :test}
3336
]
3437
end
3538

3639
defp env do
3740
[enable_debug_log: false]
3841
end
3942

40-
# We need to work around the fact that gun would pull in cowlib/ranch from git, while cowboy/plug
41-
# depend on them from hex. In order to resolve this we need to override those dependencies. But
42-
# since you can't publish to hex with overriden dependencies this ugly hack only pulls the
43-
# dependencies in when in the test env.
44-
defp deps(:test) do
45-
deps() ++
46-
[
47-
{:cowlib, "~> 1.0.1", override: true},
48-
{:ranch, "~> 1.2.0", override: true},
49-
{:gun, github: "PSPDFKit-labs/gun", only: :test}
50-
]
51-
end
52-
53-
defp deps(_), do: deps()
54-
5543
defp docs do
5644
[
5745
main: "Bypass",

mix.lock

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
%{
2-
"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
3-
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], [], "hexpm"},
4-
"earmark": {:hex, :earmark, "1.3.0", "17f0c38eaafb4800f746b457313af4b2442a8c2405b49c645768680f900be603", [:mix], [], "hexpm"},
5-
"espec": {:hex, :espec, "1.6.3", "d9355788e508b82743a1b1b9aa5ac64ba37b0547c6210328d909e8a6eb56d42e", [:mix], [{:meck, "0.8.12", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm"},
6-
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
2+
"cowboy": {:hex, :cowboy, "1.1.2", "61ac29ea970389a88eca5a65601460162d370a70018afe6f949a29dca91f3bb0", [:rebar3], [{:cowlib, "~> 1.0.2", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "f4763bbe08233eceed6f24bc4fcc8d71c17cfeafa6439157c57349aa1bb4f17c"},
3+
"cowlib": {:hex, :cowlib, "1.0.2", "9d769a1d062c9c3ac753096f868ca121e2730b9a377de23dec0f7e08b1df84ee", [:make], [], "hexpm", "db622da03aa039e6366ab953e31186cc8190d32905e33788a1acb22744e6abd2"},
4+
"earmark": {:hex, :earmark, "1.3.0", "17f0c38eaafb4800f746b457313af4b2442a8c2405b49c645768680f900be603", [:mix], [], "hexpm", "f8b8820099caf0d5e72ae6482d2b0da96f213cbbe2b5b2191a37966e119eaa27"},
5+
"espec": {:hex, :espec, "1.6.3", "d9355788e508b82743a1b1b9aa5ac64ba37b0547c6210328d909e8a6eb56d42e", [:mix], [{:meck, "0.8.12", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "235ef9931fc6ae8066272b77dc11c462e72af0aa50c6023643acd22b09326d21"},
6+
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm", "dc87f778d8260da0189a622f62790f6202af72f2f3dee6e78d91a18dd2fcd137"},
77
"gun": {:git, "https://github.com/PSPDFKit-labs/gun.git", "0462585ec7b0bcb2ca4b8b91e6d2624a45324b6e", []},
8-
"makeup": {:hex, :makeup, "0.5.5", "9e08dfc45280c5684d771ad58159f718a7b5788596099bdfb0284597d368a882", [:mix], [{:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
9-
"makeup_elixir": {:hex, :makeup_elixir, "0.10.0", "0f09c2ddf352887a956d84f8f7e702111122ca32fbbc84c2f0569b8b65cbf7fa", [:mix], [{:makeup, "~> 0.5.5", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
10-
"meck": {:hex, :meck, "0.8.12", "1f7b1a9f5d12c511848fec26bbefd09a21e1432eadb8982d9a8aceb9891a3cf2", [:rebar3], [], "hexpm"},
11-
"mime": {:hex, :mime, "1.3.0", "5e8d45a39e95c650900d03f897fbf99ae04f60ab1daa4a34c7a20a5151b7a5fe", [:mix], [], "hexpm"},
12-
"nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm"},
13-
"plug": {:hex, :plug, "1.7.0", "cd8c8de89bd9de55eba1c918bf0e7f319737e109b6014875104af025a623e16e", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}], "hexpm"},
14-
"plug_cowboy": {:hex, :plug_cowboy, "1.0.0", "2e2a7d3409746d335f451218b8bb0858301c3de6d668c3052716c909936eb57a", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
15-
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
16-
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
8+
"makeup": {:hex, :makeup, "0.5.5", "9e08dfc45280c5684d771ad58159f718a7b5788596099bdfb0284597d368a882", [:mix], [{:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d7152ff93f2eac07905f510dfa03397134345ba4673a00fbf7119bab98632940"},
9+
"makeup_elixir": {:hex, :makeup_elixir, "0.10.0", "0f09c2ddf352887a956d84f8f7e702111122ca32fbbc84c2f0569b8b65cbf7fa", [:mix], [{:makeup, "~> 0.5.5", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "4a36dd2d0d5c5f98d95b3f410d7071cd661d5af310472229dd0e92161f168a44"},
10+
"meck": {:hex, :meck, "0.8.12", "1f7b1a9f5d12c511848fec26bbefd09a21e1432eadb8982d9a8aceb9891a3cf2", [:rebar3], [], "hexpm", "7a6ab35a42e6c846636e8ecd6fdf2cc2e3f09dbee1abb15c1a7c705c10775787"},
11+
"mime": {:hex, :mime, "1.3.0", "5e8d45a39e95c650900d03f897fbf99ae04f60ab1daa4a34c7a20a5151b7a5fe", [:mix], [], "hexpm", "5e839994289d60326aa86020c4fbd9c6938af188ecddab2579f07b66cd665328"},
12+
"mint": {:hex, :mint, "1.1.0", "1fd0189edd9e3ffdbd7fcd8bc3835902b987a63ec6c4fd1aa8c2a56e2165f252", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}], "hexpm", "5bfd316c3789340b682d5679a8116bcf2112e332447bdc20c1d62909ee45f48d"},
13+
"nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm", "ebb595e19456a72786db6dcd370d320350cb624f0b6203fcc7e23161d49b0ffb"},
14+
"plug": {:hex, :plug, "1.7.0", "cd8c8de89bd9de55eba1c918bf0e7f319737e109b6014875104af025a623e16e", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}], "hexpm", "e21e0ea7cbf87ecbc6ab6a9418db78fa1285dc12c8e0586f44dd6dfa842a190b"},
15+
"plug_cowboy": {:hex, :plug_cowboy, "1.0.0", "2e2a7d3409746d335f451218b8bb0858301c3de6d668c3052716c909936eb57a", [:mix], [{:cowboy, "~> 1.0", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "01d201427a8a1f4483be2465a98b45f5e82263327507fe93404a61c51eb9e9a8"},
16+
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm", "73c1682f0e414cfb5d9b95c8e8cd6ffcfdae699e3b05e1db744e58b7be857759"},
17+
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm", "6e56493a862433fccc3aca3025c946d6720d8eedf6e3e6fb911952a7071c357f"},
1718
}

test/bypass_test.exs

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ defmodule BypassTest do
7171
assert {:ok, 200, ""} = request(bypass.port)
7272

7373
Bypass.down(bypass)
74-
assert {:error, :noconnect} = request(bypass.port)
74+
assert {:error, %Mint.TransportError{reason: :econnrefused}} = request(bypass.port)
7575
end
7676

7777
test "Bypass.up opens the socket again" do
@@ -84,7 +84,7 @@ defmodule BypassTest do
8484
assert {:ok, 200, ""} = request(bypass.port)
8585

8686
Bypass.down(bypass)
87-
assert {:error, :noconnect} = request(bypass.port)
87+
assert {:error, %Mint.TransportError{reason: :econnrefused}} = request(bypass.port)
8888

8989
Bypass.up(bypass)
9090
assert {:ok, 200, ""} = request(bypass.port)
@@ -136,7 +136,8 @@ defmodule BypassTest do
136136
])
137137

138138
capture_log(fn ->
139-
assert {:error, {:closed, 'The connection was lost.'}} = request(bypass.port)
139+
assert {:error, _conn, %Mint.TransportError{reason: :closed}, _responses} =
140+
request(bypass.port)
140141
end)
141142
end
142143

@@ -161,7 +162,8 @@ defmodule BypassTest do
161162
end
162163
])
163164

164-
assert {:error, {:closed, 'The connection was lost.'}} == request(bypass.port)
165+
assert {:error, _conn, %Mint.TransportError{reason: :closed}, _responses} =
166+
request(bypass.port)
165167
end
166168

167169
test "Bypass.down waits for plug process to terminate before shutting it down with expect" do
@@ -448,46 +450,43 @@ defmodule BypassTest do
448450
"high-level" HTTP client, since they do connection pooling and we will sometimes get a connection
449451
closed error and not a failed to connect error, when we test Bypass.down
450452
"""
451-
def request(port, path \\ "/example_path", method \\ :post) do
452-
{:ok, conn} = :gun.start_link(self(), '127.0.0.1', port, %{retry: 0})
453-
454-
try do
455-
case :gun.await_up(conn, 250) do
456-
{:ok, _protocol} ->
457-
stream =
458-
case method do
459-
:post -> :gun.post(conn, path, [], "")
460-
:get -> :gun.get(conn, path, [])
461-
end
462-
463-
case :gun.await(conn, stream, 250) do
464-
{:response, :fin, status, _headers} ->
465-
{:ok, status, ""}
466-
467-
{:response, :nofin, status, _headers} ->
468-
case :gun.await_body(conn, stream, 250) do
469-
{:ok, data} -> {:ok, status, data}
470-
{:error, _} = error -> error
471-
end
472-
473-
{:error, _} = error ->
474-
error
475-
end
476-
477-
{:error, :timeout} ->
478-
raise "Expected gun to die, but it didn't."
479-
480-
{:error, :normal} ->
481-
# `await_up` monitors gun and errors only if gun died (or after `timeout`). That happens
482-
# when gun can't connect and is out of retries (immediately in our case) so we know that
483-
# gun is dead.
484-
{:error, :noconnect}
485-
end
486-
after
487-
Process.unlink(conn)
488-
monitor = Process.monitor(conn)
489-
Process.exit(conn, :kill)
490-
assert_receive {:DOWN, ^monitor, :process, ^conn, _}
453+
def request(port, path \\ "/example_path", method \\ "POST") do
454+
with {:ok, conn} <- Mint.HTTP.connect(:http, "127.0.0.1", port),
455+
{:ok, conn, ref} = Mint.HTTP.request(conn, method, path, [], "") do
456+
receive_responses(conn, ref, 100, [])
457+
end
458+
end
459+
460+
defp receive_responses(conn, ref, status, body) do
461+
receive do
462+
message ->
463+
with {:ok, conn, responses} <- Mint.HTTP.stream(conn, message) do
464+
receive_responses(responses, conn, ref, status, body)
465+
end
466+
end
467+
end
468+
469+
defp receive_responses([], conn, ref, status, body) do
470+
receive_responses(conn, ref, status, body)
471+
end
472+
473+
defp receive_responses([response | responses], conn, ref, status, body) do
474+
case response do
475+
{:status, ^ref, status} ->
476+
receive_responses(responses, conn, ref, status, body)
477+
478+
{:headers, ^ref, _headers} ->
479+
receive_responses(responses, conn, ref, status, body)
480+
481+
{:data, ^ref, data} ->
482+
receive_responses(responses, conn, ref, status, [data | body])
483+
484+
{:done, ^ref} ->
485+
_ = Mint.HTTP.close(conn)
486+
{:ok, status, body |> Enum.reverse() |> IO.iodata_to_binary()}
487+
488+
{:error, ^ref, _reason} = error ->
489+
error
491490
end
492491
end
493492

@@ -578,7 +577,7 @@ defmodule BypassTest do
578577
# Success
579578
bypass = prepare_stubs()
580579
assert {:ok, 200, ""} = request(bypass.port)
581-
assert {:ok, 200, ""} = request(bypass.port, "/foo", :get)
580+
assert {:ok, 200, ""} = request(bypass.port, "/foo", "GET")
582581
assert :ok = Bypass.verify_expectations!(bypass)
583582

584583
# Fail: no requests on a single stub
@@ -597,7 +596,7 @@ defmodule BypassTest do
597596
assert {:ok, 200, ""} = request(bypass.port)
598597
end)
599598

600-
assert {:ok, 200, ""} = request(bypass.port, "/foo", :get)
599+
assert {:ok, 200, ""} = request(bypass.port, "/foo", "GET")
601600
:timer.sleep(10)
602601

603602
assert_raise ESpec.AssertionError, "Expected only one HTTP request for Bypass", fn ->

0 commit comments

Comments
 (0)