Skip to content

Commit

Permalink
Make http adapter customizable (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
danschultzer authored Nov 5, 2018
1 parent d1bff77 commit 97c9279
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 28 deletions.
10 changes: 8 additions & 2 deletions lib/premailex/html_inline_styles.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ defmodule Premailex.HTMLInlineStyles do

defp load_css({"link", {"href", url}}) do
url
|> HTTPoison.get()
|> http_adapter().get()
|> parse_url_response()
end

defp parse_url_response({:ok, %{body: resp}}), do: CSSParser.parse(resp)
defp parse_url_response({:ok, %{body: resp}}) when is_binary(resp), do: CSSParser.parse(resp)
defp parse_url_response(_), do: nil

defp add_rule_set_to_html(%{selector: selector, rules: rules, specificity: specificity}, html) do
Expand Down Expand Up @@ -134,4 +134,10 @@ defmodule Premailex.HTMLInlineStyles do
false -> tree
end
end

@default_http_adapter HTTPoison

defp http_adapter do
Application.get_env(:premailex, :http_adapter, @default_http_adapter)
end
end
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ defmodule Premailex.Mixfile do
{:meeseeks, ">= 0.9.0 or < 0.11.0", optional: true},
{:httpoison, "~> 0.13 or ~> 1.0"},
{:credo, "~> 0.10", only: [:dev, :test], runtime: false},
{:bypass, github: "oo6/bypass", branch: "use-plug-cowboy", only: :test},
{:ex_doc, ">= 0.0.0", only: :dev}
]
end
Expand Down
8 changes: 0 additions & 8 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
%{
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm"},
"bypass": {:git, "https://github.com/oo6/bypass.git", "50af8d7b5f8fd52254b435ed54eb5f8be67fa63e", [branch: "use-plug-cowboy"]},
"certifi": {:hex, :certifi, "2.4.2", "75424ff0f3baaccfd34b1214184b6ef616d89e420b258bb0a5ea7d7bc628f7f0", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm"},
"cowboy": {:hex, :cowboy, "2.5.0", "4ef3ae066ee10fe01ea3272edc8f024347a0d3eb95f6fbb9aed556dacbfc1337", [:rebar3], [{:cowlib, "~> 2.6.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.6.2", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
"cowlib": {:hex, :cowlib, "2.6.0", "8aa629f81a0fc189f261dc98a42243fa842625feea3c7ec56c48f4ccdb55490f", [:rebar3], [], "hexpm"},
"credo": {:hex, :credo, "0.10.2", "03ad3a1eff79a16664ed42fc2975b5e5d0ce243d69318060c626c34720a49512", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm"},
"earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm"},
"ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
Expand All @@ -18,15 +15,10 @@
"meeseeks": {:hex, :meeseeks, "0.10.1", "4c8d71c491f30ee3e592d6609bbef05b77236b34755eaa5c8b5d15428539d1e7", [:mix], [{:meeseeks_html5ever, "~> 0.10.1", [hex: :meeseeks_html5ever, repo: "hexpm", optional: false]}], "hexpm"},
"meeseeks_html5ever": {:hex, :meeseeks_html5ever, "0.10.1", "3ea11440b91dc61751dd05dd6bbfd23110f7a2116338f42b6835e8a07e1522cc", [:mix], [{:rustler, "~> 0.18.0", [hex: :rustler, repo: "hexpm", optional: false]}], "hexpm"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm"},
"mime": {:hex, :mime, "1.3.0", "5e8d45a39e95c650900d03f897fbf99ae04f60ab1daa4a34c7a20a5151b7a5fe", [:mix], [], "hexpm"},
"mimerl": {:hex, :mimerl, "1.0.2", "993f9b0e084083405ed8252b99460c4f0563e41729ab42d9074fd5e52439be88", [:rebar3], [], "hexpm"},
"mochiweb": {:hex, :mochiweb, "2.18.0", "eb55f1db3e6e960fac4e6db4e2db9ec3602cc9f30b86cd1481d56545c3145d2e", [:rebar3], [], "hexpm"},
"nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm"},
"parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm"},
"plug": {:hex, :plug, "1.7.1", "8516d565fb84a6a8b2ca722e74e2cd25ca0fc9d64f364ec9dbec09d33eb78ccd", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}], "hexpm"},
"plug_cowboy": {:hex, :plug_cowboy, "2.0.0", "ab0c92728f2ba43c544cce85f0f220d8d30fc0c90eaa1e6203683ab039655062", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
"plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
"ranch": {:hex, :ranch, "1.6.2", "6db93c78f411ee033dbb18ba8234c5574883acb9a75af0fb90a9b82ea46afa00", [:rebar3], [], "hexpm"},
"rustler": {:hex, :rustler, "0.18.0", "db4bd0c613d83a1badc31be90ddada6f9821de29e4afd15c53a5da61882e4f2d", [:mix], [], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.4", "f0eafff810d2041e93f915ef59899c923f4568f4585904d010387ed74988e77b", [:make, :mix, :rebar3], [], "hexpm"},
"unicode_util_compat": {:hex, :unicode_util_compat, "0.4.1", "d869e4c68901dd9531385bb0c8c40444ebf624e60b6962d95952775cac5e90cd", [:rebar3], [], "hexpm"},
Expand Down
28 changes: 13 additions & 15 deletions test/premailex/html_inline_styles_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ defmodule Premailex.HTMLInlineStylesTest do
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link href="STYLESHEET_URL" media="all" rel="stylesheet">
<link href="INVALID_URL" media="all" rel="stylesheet">
<link href="http://localhost/styles.css" media="all" rel="stylesheet">
<link href="http://localhost/invalid_styles.css" media="all" rel="stylesheet">
<link media="all" rel="stylesheet">
<title>Test</title>
<style>#{@css_inline_content}</style>
Expand Down Expand Up @@ -61,23 +61,21 @@ defmodule Premailex.HTMLInlineStylesTest do
</html>
"""

setup do
bypass = Bypass.open()
module =
quote do
def get("http://localhost/styles.css"),
do: {:ok, %HTTPoison.Response{status_code: 200, body: unquote(@css_link_content)}}

Bypass.expect_once(bypass, "GET", "/styles.css", fn conn ->
Plug.Conn.resp(conn, 200, @css_link_content)
end)
def get("http://localhost/invalid_styles.css"),
do: {:ok, %HTTPoison.Response{status_code: 404}}
end

Bypass.expect_once(bypass, "GET", "/invalid_styles.css", fn conn ->
Plug.Conn.resp(conn, 500, "{}")
end)
Module.create(HTTPAdapterMock, module, Macro.Env.location(__ENV__))

input =
@input
|> String.replace("STYLESHEET_URL", "http://localhost:#{bypass.port}/styles.css")
|> String.replace("INVALID_URL", "http://localhost:#{bypass.port}/invalid_styles.css")
setup do
Application.put_env(:premailex, :http_adapter, HTTPAdapterMock)

{:ok, bypass: bypass, input: input}
{:ok, input: @input}
end

test "process/1", %{input: input} do
Expand Down
2 changes: 0 additions & 2 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ ExUnit.start()

if System.get_env("HTML_PARSER") == "meeseeks",
do: Application.put_env(:premailex, :html_parser, Premailex.HTMLParser.Meeseeks)

Application.ensure_all_started(:bypass)

0 comments on commit 97c9279

Please sign in to comment.