From 8277c102b2f3565415fdb90e1f846410275032d8 Mon Sep 17 00:00:00 2001 From: Calvin Gerling Date: Thu, 4 May 2023 20:46:34 -0300 Subject: [PATCH] use hex_repo to fetch data inside Hex.Repo --- lib/hex/repo.ex | 56 ++++++++++++++++++++---------------------- test/hex/repo_test.exs | 14 ++++++++--- 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/lib/hex/repo.ex b/lib/hex/repo.ex index 96518d8a..b423d89b 100644 --- a/lib/hex/repo.ex +++ b/lib/hex/repo.ex @@ -181,30 +181,39 @@ defmodule Hex.Repo do defp merge_values(left, _right), do: left def get_package(repo, package, etag) do - headers = Map.merge(etag_headers(etag), auth_headers(repo)) + config = + HTTP.config() + |> put_auth_config(repo) + |> put_etag_config(etag) - HTTP.request(:get, package_url(repo, package), headers, nil) + :mix_hex_repo.get_package(config, package) |> handle_response() end def get_docs(repo, package, version) do - headers = auth_headers(repo) + config = + HTTP.config() + |> put_auth_config(repo) - HTTP.request(:get, docs_url(repo, package, version), headers, nil) + :mix_hex_repo.get_docs(config, package, version) |> handle_response() end def get_tarball(repo, package, version) do - headers = auth_headers(repo) + config = + HTTP.config() + |> put_auth_config(repo) - HTTP.request(:get, tarball_url(repo, package, version), headers, nil) + :mix_hex_repo.get_tarball(config, package, version) |> handle_response() end def get_public_key(repo) do - headers = auth_headers(repo) + config = + HTTP.config() + |> put_auth_config(repo) - HTTP.request(:get, public_key_url(repo), headers, nil) + :mix_hex_repo.get_public_key(config) |> handle_response() end @@ -242,38 +251,27 @@ defmodule Hex.Repo do |> version_latest() end - defp package_url(repo, package) do - config = get_repo(repo) - config.url <> "/packages/#{URI.encode(package)}" - end - - defp docs_url(repo, package, version) do - config = get_repo(repo) - config.url <> "/docs/#{URI.encode(package)}-#{URI.encode(version)}.tar.gz" - end - def tarball_url(repo, package, version) do config = get_repo(repo) config.url <> "/tarballs/#{URI.encode(package)}-#{URI.encode(version)}.tar" end - defp public_key_url(repo), do: repo.url <> "/public_key" + defp put_auth_config(config, repo) when is_binary(repo) or is_nil(repo) do + repo = get_repo(repo) - defp etag_headers(nil), do: %{} - defp etag_headers(etag), do: %{"if-none-match" => etag} + put_auth_config(config, repo) + end - defp auth_headers(repo) when is_binary(repo) or repo == nil do - repo - |> get_repo() - |> auth_headers() + defp put_auth_config(config, %{trusted: true} = repo) do + %{config | repo_key: repo.auth_key, repo_url: repo.url} end - defp auth_headers(%{trusted: true, auth_key: key}) when is_binary(key) do - %{"authorization" => key} + defp put_auth_config(config, %{trusted: _}) do + config end - defp auth_headers(%{trusted: _, auth_key: _}) do - %{} + defp put_etag_config(config, etag) do + %{config | http_etag: etag} end defp parse_csv(body) do diff --git a/test/hex/repo_test.exs b/test/hex/repo_test.exs index 6c87d316..a6fefe3c 100644 --- a/test/hex/repo_test.exs +++ b/test/hex/repo_test.exs @@ -3,7 +3,7 @@ defmodule Hex.RepoTest do @private_key File.read!(Path.join(__DIR__, "../fixtures/test_priv.pem")) - test "get_package" do + test "get_package/3" do assert {:ok, {200, _, _}} = Hex.Repo.get_package("hexpm", "postgrex", "") assert_raise Mix.Error, ~r"Unknown repository \"bad\"", fn -> @@ -11,6 +11,14 @@ defmodule Hex.RepoTest do end end + test "get_tarball/3" do + assert {:ok, {200, _, _}} = Hex.Repo.get_tarball("hexpm", "postgrex", "0.2.1") + + assert_raise Mix.Error, ~r"Unknown repository \"bad\"", fn -> + Hex.Repo.get_tarball("bad", "postgrex", "0.2.1") + end + end + test "verify signature" do message = :mix_hex_registry.sign_protobuf("payload", @private_key) assert Hex.Repo.verify(message, "hexpm") == "payload" @@ -69,7 +77,7 @@ defmodule Hex.RepoTest do Plug.Conn.resp(conn, 200, hexpm.public_key) "/not_found/public_key" -> - assert Plug.Conn.get_req_header(conn, "authorization") == [] + assert Plug.Conn.get_req_header(conn, "authorization") == ["key"] Plug.Conn.resp(conn, 404, "not found") end end) @@ -78,7 +86,7 @@ defmodule Hex.RepoTest do assert {:ok, {200, public_key, _}} = Hex.Repo.get_public_key(config) assert public_key == hexpm.public_key - config = %{url: "http://localhost:#{bypass.port}/not_found", auth_key: "key", trusted: false} + config = %{url: "http://localhost:#{bypass.port}/not_found", auth_key: "key", trusted: true} assert {:ok, {404, "not found", _}} = Hex.Repo.get_public_key(config) end