Skip to content

Commit

Permalink
Fix code style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
almirsarajcic committed Nov 15, 2024
1 parent a82e965 commit ae4a0c1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 363 deletions.
5 changes: 2 additions & 3 deletions lib/phx_tools_web/plugs/curl_detector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ defmodule PhxToolsWeb.CurlDetector do
end
end

defp request_sent_from_curl?(user_agent) do
user_agent && String.starts_with?(user_agent, "curl")
end
defp request_sent_from_curl?(nil), do: false
defp request_sent_from_curl?(user_agent), do: String.starts_with?(user_agent, "curl")

defp script_content do
:phx_tools
Expand Down
8 changes: 4 additions & 4 deletions lib/phx_tools_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ defmodule PhxToolsWeb.Router do
plug :put_secure_browser_headers
end

pipeline :system_detector do
plug PhxToolsWeb.SystemDetector
end

pipeline :curl_detector do
plug PhxToolsWeb.CurlDetector
end

pipeline :system_detector do
plug PhxToolsWeb.SystemDetector
end

scope "/", PhxToolsWeb do
pipe_through [:browser, :curl_detector, :system_detector]

Expand Down
23 changes: 17 additions & 6 deletions test/phx_tools_web/plugs/curl_detector_test.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule PhxToolsWeb.CurlDetectorTest do
use PhxToolsWeb.ConnCase, async: true

@script_content File.read!("test/support/script.sh")
@script_content "Welcome to the phx.tools shell script"

describe "call/2" do
test "returns script content for requests with curl User-Agent on root path", %{conn: conn} do
test "responds with script content for requests sent from curl", %{conn: conn} do
conn =
conn
|> put_req_header("user-agent", "curl/7.68.0")
Expand All @@ -14,17 +14,18 @@ defmodule PhxToolsWeb.CurlDetectorTest do
assert String.contains?(conn.resp_body, @script_content)
end

test "returns script content for /macOS.sh regardless of User-Agent,", %{conn: conn} do
test "responds with website content for requests not sent from curl", %{conn: conn} do
conn =
conn
|> put_req_header("user-agent", "Mozilla/5.0")
|> get("/macOS.sh")
|> get("/")

assert conn.status == 200
assert String.contains?(conn.resp_body, @script_content)
refute String.contains?(conn.resp_body, @script_content)
assert String.contains?(conn.resp_body, "<html")
end

test "returns script content for /Linux.sh regardless of User-Agent,", %{conn: conn} do
test "responds with the script for /Linux.sh regardless of User-Agent", %{conn: conn} do
conn =
conn
|> put_req_header("user-agent", "Mozilla/5.0")
Expand All @@ -33,5 +34,15 @@ defmodule PhxToolsWeb.CurlDetectorTest do
assert conn.status == 200
assert String.contains?(conn.resp_body, @script_content)
end

test "responds with the script for /macOS.sh regardless of User-Agent", %{conn: conn} do
conn =
conn
|> put_req_header("user-agent", "Mozilla/5.0")
|> get("/macOS.sh")

assert conn.status == 200
assert String.contains?(conn.resp_body, @script_content)
end
end
end
Loading

0 comments on commit ae4a0c1

Please sign in to comment.