Skip to content

Commit 31182d4

Browse files
committed
Refactor Sentry.PlugContextTest
1 parent 2e0028e commit 31182d4

File tree

1 file changed

+36
-28
lines changed

1 file changed

+36
-28
lines changed

test/plug_context_test.exs renamed to test/sentry/plug_context_test.exs

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
defmodule Sentry.PlugContextTest do
2-
use ExUnit.Case
2+
use ExUnit.Case, async: true
33
use Plug.Test
44

5+
setup do
6+
%{conn: conn(:get, "/test?hello=world")}
7+
end
8+
59
def body_scrubber(conn) do
610
Map.take(conn.params, ["foo"])
711
end
812

913
def header_scrubber(conn) do
10-
Enum.into(conn.req_headers, %{})
11-
|> Map.take(["x-not-secret-header"])
14+
conn.req_headers |> Map.new() |> Map.take(["x-not-secret-header"])
1215
end
1316

1417
def cookie_scrubber(conn) do
15-
Enum.into(conn.cookies, %{})
16-
|> Map.take(["not-secret"])
18+
conn.cookies |> Map.new() |> Map.take(["not-secret"])
1719
end
1820

1921
def remote_address_reader(conn) do
@@ -23,8 +25,8 @@ defmodule Sentry.PlugContextTest do
2325
end
2426
end
2527

26-
test "sets request context" do
27-
Sentry.PlugContext.call(conn(:get, "/test?hello=world"), [])
28+
test "sets request context", %{conn: conn} do
29+
call(conn, [])
2830

2931
assert %{
3032
request: %{
@@ -45,10 +47,10 @@ defmodule Sentry.PlugContextTest do
4547
} = Sentry.Context.get_all()
4648
end
4749

48-
test "sets request context with real client ip if request is forwarded" do
49-
conn(:get, "/test?hello=world")
50+
test "sets request context with real client ip if request is forwarded", %{conn: conn} do
51+
conn
5052
|> put_req_header("x-forwarded-for", "10.0.0.1")
51-
|> Sentry.PlugContext.call([])
53+
|> call([])
5254

5355
assert %{
5456
request: %{
@@ -69,45 +71,44 @@ defmodule Sentry.PlugContextTest do
6971
} = Sentry.Context.get_all()
7072
end
7173

72-
test "allows configuring request address reader" do
73-
conn(:get, "/test")
74+
test "allows configuring request address reader", %{conn: conn} do
75+
conn
7476
|> put_req_header("cf-connecting-ip", "10.0.0.2")
75-
|> Sentry.PlugContext.call(remote_address_reader: {__MODULE__, :remote_address_reader})
77+
|> call(remote_address_reader: {__MODULE__, :remote_address_reader})
7678

7779
assert %{"REMOTE_ADDR" => "10.0.0.2"} = Sentry.Context.get_all().request.env
7880
end
7981

8082
test "allows configuring body scrubber" do
81-
Sentry.PlugContext.call(conn(:get, "/test?hello=world&foo=bar"),
82-
body_scrubber: {__MODULE__, :body_scrubber}
83-
)
83+
conn = conn(:get, "/test?hello=world&foo=bar")
84+
call(conn, body_scrubber: {__MODULE__, :body_scrubber})
8485

8586
assert %{
8687
"foo" => "bar"
8788
} == Sentry.Context.get_all().request.data
8889
end
8990

90-
test "allows configuring header scrubber" do
91-
conn(:get, "/test?hello=world&foo=bar")
91+
test "allows configuring header scrubber", %{conn: conn} do
92+
conn
9293
|> put_req_header("x-not-secret-header", "not secrets")
9394
|> put_req_header("x-secret-header", "secrets")
94-
|> Sentry.PlugContext.call(header_scrubber: {__MODULE__, :header_scrubber})
95+
|> call(header_scrubber: {__MODULE__, :header_scrubber})
9596

9697
assert %{"x-not-secret-header" => "not secrets"} == Sentry.Context.get_all().request.headers
9798
end
9899

99-
test "allows configuring cookie scrubber" do
100-
conn(:get, "/test?hello=world&foo=bar")
100+
test "allows configuring cookie scrubber", %{conn: conn} do
101+
conn
101102
|> put_req_header("cookie", "secret=secret;not-secret=not-secret")
102-
|> Sentry.PlugContext.call(cookie_scrubber: {__MODULE__, :cookie_scrubber})
103+
|> call(cookie_scrubber: {__MODULE__, :cookie_scrubber})
103104

104105
assert %{"not-secret" => "not-secret"} == Sentry.Context.get_all().request.cookies
105106
end
106107

107-
test "allows configuring request id header" do
108-
conn(:get, "/test?hello=world&foo=bar")
108+
test "allows configuring request id header", %{conn: conn} do
109+
conn
109110
|> put_resp_header("my-request-id", "abc123")
110-
|> Sentry.PlugContext.call(request_id_header: "my-request-id")
111+
|> call(request_id_header: "my-request-id")
111112

112113
assert %{"REQUEST_ID" => "abc123"} = Sentry.Context.get_all().request.env
113114
end
@@ -131,10 +132,13 @@ defmodule Sentry.PlugContextTest do
131132
|> put_req_header("authorization", "secrets")
132133
|> put_req_header("authentication", "secrets")
133134
|> put_req_header("content-type", "application/json")
134-
|> Sentry.PlugContext.call([])
135+
|> call([])
135136

136137
request_context = Sentry.Context.get_all().request
137138

139+
assert request_context.headers == %{"content-type" => "application/json"}
140+
assert request_context.cookies == %{}
141+
138142
assert request_context.data == %{
139143
"another_cc" => "*********",
140144
"cc" => "*********",
@@ -153,8 +157,8 @@ defmodule Sentry.PlugContextTest do
153157
test "handles data scrubbing with file upload" do
154158
upload = %Plug.Upload{path: "test/fixtures/my_image.png", filename: "my_image.png"}
155159

156-
conn(:post, "/error_route", %{"image" => upload, "password" => "my_password"})
157-
|> Sentry.PlugContext.call([])
160+
conn = conn(:post, "/error_route", %{"image" => upload, "password" => "my_password"})
161+
call(conn, [])
158162

159163
assert Sentry.Context.get_all().request.data == %{
160164
"password" => "*********",
@@ -165,4 +169,8 @@ defmodule Sentry.PlugContextTest do
165169
}
166170
}
167171
end
172+
173+
defp call(conn, opts) do
174+
Plug.run(conn, [{Sentry.PlugContext, opts}])
175+
end
168176
end

0 commit comments

Comments
 (0)