Skip to content

Commit

Permalink
Check the status code of an algolia response to determine success (#1810
Browse files Browse the repository at this point in the history
)

* check the status code of an algolia response to determine success

* clear cache between tests and use two different bypass instances

* minor cleanup

* minor cleanup
  • Loading branch information
anthonyshull authored Dec 4, 2023
1 parent df00b8a commit 924f58e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
12 changes: 9 additions & 3 deletions apps/algolia/lib/algolia/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ defmodule Algolia.Api do
|> Keyword.put(:pool, @http_pool)

send_post_request = fn {body, config} ->
opts
|> generate_url(config)
|> HTTPoison.post(body, headers(config), hackney: hackney)
response =
opts
|> generate_url(config)
|> HTTPoison.post(body, headers(config), hackney: hackney)

case response do
{:ok, %HTTPoison.Response{status_code: 200}} -> response
{_, invalid_response} -> {:error, invalid_response}
end
end

# If we're making a query for results using the same request body AND same
Expand Down
61 changes: 45 additions & 16 deletions apps/algolia/test/api_test.exs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
defmodule Algolia.ApiTest do
use ExUnit.Case
use ExUnit.Case, async: false

@request ~s({"requests" : [{"indexName" : "index"}]})
@success_response ~s({"message" : "success"})
@failure_response ~s({"error": "bad request"})
@request ~s({"requests": [{"indexName": "*"}]})
@success_response ~s({"ok": "success"})

describe "post" do
test "sends a post request once to /1/indexes/$INDEX/$ACTION" do
bypass = Bypass.open()
setup do
ConCache.ets(Algolia.Api) |> :ets.delete_all_objects()

Bypass.expect_once(bypass, "POST", "/1/indexes/*/queries", fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)

case Poison.decode(body) do
{:ok, %{"requests" => [%{"indexName" => "index"}]}} ->
Plug.Conn.send_resp(conn, 200, @success_response)
{:ok, bypass: Bypass.open(), failure: Bypass.open(), success: Bypass.open()}
end

_ ->
Plug.Conn.send_resp(conn, 400, ~s({"error" : "bad request"}))
end
test "caches a successful response", %{bypass: bypass} do
Bypass.expect_once(bypass, "POST", "/1/indexes/*/queries", fn conn ->
Plug.Conn.send_resp(conn, 200, @success_response)
end)

opts = %Algolia.Api{
Expand All @@ -33,9 +30,41 @@ defmodule Algolia.ApiTest do
assert {:ok, %HTTPoison.Response{status_code: 200, body: ^body}} = Algolia.Api.post(opts)
end

test "logs a warning if config keys are missing" do
bypass = Bypass.open()
test "does not cache a failed response", %{failure: failure, success: success} do
Bypass.expect_once(failure, "POST", "/1/indexes/*/queries", fn conn ->
Plug.Conn.send_resp(conn, 400, @failure_response)
end)

failure_opts = %Algolia.Api{
host: "http://localhost:#{failure.port}",
index: "*",
action: "queries",
body: @request
}

assert {:error, %HTTPoison.Response{status_code: 400, body: body}} =
Algolia.Api.post(failure_opts)

assert body == @failure_response

Bypass.expect_once(success, "POST", "/1/indexes/*/queries", fn conn ->
Plug.Conn.send_resp(conn, 200, @success_response)
end)

success_opts = %Algolia.Api{
host: "http://localhost:#{success.port}",
index: "*",
action: "queries",
body: @request
}

assert {:ok, %HTTPoison.Response{status_code: 200, body: body}} =
Algolia.Api.post(success_opts)

assert body == @success_response
end

test "logs a warning if config keys are missing", %{bypass: bypass} do
opts = %Algolia.Api{
host: "http://localhost:#{bypass.port}",
index: "*",
Expand Down

0 comments on commit 924f58e

Please sign in to comment.