Skip to content

Commit

Permalink
API: impl APIResponse.headers/1
Browse files Browse the repository at this point in the history
Incidentally, also:

- Add more function documentation, including "usage", to `APIResponse`
- Add test of `APIRequestContext.fetch/3` to cover default HTTP method (i.e., "GET")
  • Loading branch information
coreyti committed Oct 4, 2024
1 parent 66d0aab commit 6e2f289
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 32 deletions.
59 changes: 44 additions & 15 deletions lib/playwright/api_response.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ defmodule Playwright.APIResponse do
## Usage
{:ok, session, _} = Playwright.launch()
request = Playwright.request(session)
context = APIRequest.new_context(request)
response = APIRequest.get(context, "https://example.com")
json = APIResponse.json(response)
json = APIResponse.json!(response)
"""

use Playwright.SDK.Pipeline
Expand Down Expand Up @@ -56,6 +57,12 @@ defmodule Playwright.APIResponse do
@doc """
Returns a buffer with the response body.
## Usage
request = Playwright.request(session) |> APIRequest.new_context()
response = APIRequestContext.fetch("https://example.com")
APIResponse.body!(response) |> IO.puts()
## Returns
- `binary()`
Expand Down Expand Up @@ -97,15 +104,15 @@ defmodule Playwright.APIResponse do
end
end

# ---
@doc """
Returns the value of a header.
# @spec headers(t()) :: map()
# def headers(response)
# ---
## Usage
@doc """
Returns the value of a header.
request = Playwright.request(session) |> APIRequest.new_context()
response = APIRequestContext.fetch("https://example.com")
APIResponse.header(response, "content-type") |> IO.puts()
## Arguments
Expand All @@ -119,7 +126,6 @@ defmodule Playwright.APIResponse do
- `binary()`
- `nil`
"""
@pipe {:header, [:response, :name]}
@spec header(t(), atom() | String.t()) :: binary() | nil
def header(response, name)

Expand All @@ -137,19 +143,37 @@ defmodule Playwright.APIResponse do
end
end

# ---
@doc """
Returns a `map(name => value)` with all the response HTTP headers associated
with this response.
## Usage
# @spec headers(t()) :: map()
# def headers(response)
request = Playwright.request(session) |> APIRequest.new_context()
response = APIRequestContext.fetch("https://example.com")
APIResponse.headers(response) |> IO.inspect()
# @spec headers_list(APIResponse.t()) :: [map()]
# def headers_list(response)
## Returns
# ---
- `%{String.t() => String.t()}`
"""
@spec headers(t()) :: %{String.t() => String.t()}
def headers(%APIResponse{} = response) do
# Map.new([{1, 2}, {3, 4}])
Enum.reduce(response.headers, %{}, fn %{name: name, value: value}, headers ->
Map.put(headers, name, value)
end)
end

@doc """
Returns a deserialized version of the JSON representation of response body.
## Usage
request = Playwright.request(session) |> APIRequest.new_context()
response = APIRequestContext.fetch("https://example.com")
APIResponse.json!(response) |> IO.inspect()
## Returns
- `serializable()`
Expand Down Expand Up @@ -182,7 +206,6 @@ defmodule Playwright.APIResponse do
- `boolean()`
"""
@pipe {:ok, [:response]}
@spec ok(t()) :: boolean()
def ok(%APIResponse{} = response) do
response.status === 0 || (response.status >= 200 && response.status <= 299)
Expand All @@ -191,6 +214,12 @@ defmodule Playwright.APIResponse do
@doc """
Returns a text representation of response body.
## Usage
request = Playwright.request(session) |> APIRequest.new_context()
response = APIRequestContext.fetch("https://example.com")
APIResponse.text!(response) |> IO.puts()
## Returns
- `binary()`
Expand Down
8 changes: 8 additions & 0 deletions test/api/api_request_context_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ defmodule Playwright.APIRequestContextTest do
end)
end

test "defaults the HTTP request method to 'GET'", %{assets: assets, session: session} do
context = Playwright.request(session) |> APIRequest.new_context()
response = APIRequestContext.fetch(context, assets.prefix <> "/simple.json")

assert APIResponse.ok(response)
assert APIResponse.header(response, "x-playwright-request-method") == "GET"
end

test "on 404, returns `APIResponse` w/ error status", %{assets: assets, session: session} do
context = Playwright.request(session) |> APIRequest.new_context()
%APIResponse{status: status} = response = APIRequestContext.fetch(context, assets.prefix <> "/bogus.json")
Expand Down
28 changes: 11 additions & 17 deletions test/api/api_response_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,18 @@ defmodule Playwright.APIResponseTest do
end
end

describe "APIResponse.header!/1" do
test "on success, returns the value of the HTTP header", %{assets: assets, session: session} do
describe "APIResponse.headers/1" do
test "returns the response headers as a `map(name => value)`", %{assets: assets, session: session} do
request = Playwright.request(session) |> APIRequest.new_context()
response = APIRequestContext.fetch(request, assets.prefix <> "/simple.json")

assert "application/json" = APIResponse.header(response, "content-type")
assert %{
"connection" => "close",
"content-length" => "15",
"content-type" => "application/json",
"x-playwright-request-method" => "GET"
} = APIResponse.headers(response)
end

# @tag :skip
# ...haven't found a way to fail
# test "on failure, raises", %{assets: assets, session: session} do
# end
end

describe "APIResponse.headers/1" do
end

describe "APIResponse.headers!/1" do
end

describe "APIResponse.json/1" do
Expand Down Expand Up @@ -144,19 +138,19 @@ defmodule Playwright.APIResponseTest do
end

describe "APIResponse.ok/1" do
test "is true when the response status code is in the range, 200-299" do
test "returns true when the response status code is in the range, 200-299" do
range = 200..299

Enum.each(range, fn code ->
assert APIResponse.ok(%APIResponse{status: code})
end)
end

test "is true when the response status code is 0" do
test "returns true when the response status code is 0" do
assert APIResponse.ok(%APIResponse{status: 0})
end

test "is false otherwise" do
test "returns false otherwise" do
range = 1..199

Enum.each(range, fn code ->
Expand Down

0 comments on commit 6e2f289

Please sign in to comment.