Gatekeep ingestion pipeline (#2472)
* Update Sites.Cache So it's now capable of refreshing most recent sites. Refreshing a single site is no longer wanted. * Introduce Warmer.RecentlyUpdated This is Sites Cache warmer that runs only for most recently updated sites every 30s. * Validate Request creation early * Rename RateLimiter to GateKeeper and introduce detailed policies * Update events API tests - a provisioned site is now required * Update events ingestion tests * Make limits visible in CRM Sites index * Hard-deprecate DOMAIN_BLACKLIST * Remove unnecessary clause * Fix typo * Explicitly delegate Warmer.All * GateKeeper.allwoance => GateKeeper.check * Instrument Sites.Cache measurments * Update send_pageview task to output response headers * Instrument ingestion pipeline * Credo * Make event telemetry test a sync case * Simplify Request.uri/hostname handling * Use embedded schema, apply action and rely on get_fieldpull/2477/head r2022.11.28-24
parent
e2563acf36
commit
356575ef78
@ -1,109 +0,0 @@
|
||||
defmodule Plausible.Site.RateLimiter do
|
||||
@policy_for_non_existing_sites :deny
|
||||
@policy_on_rate_limiting_backend_error :allow
|
||||
|
||||
@moduledoc """
|
||||
Thin wrapper around Hammer for rate limiting domain-specific events
|
||||
during the ingestion phase. Currently there are two policies
|
||||
on which the `allow/2` function operates:
|
||||
* `:allow`
|
||||
* `:deny`
|
||||
|
||||
Rate Limiting buckets are configured per site (externally via the CRM).
|
||||
See: `Plausible.Site`
|
||||
|
||||
To look up each site's configuration, the RateLimiter fetches
|
||||
a Site by domain using `Plausible.Cache` interface.
|
||||
If the Site is not found in Cache, a DB refresh attempt is made.
|
||||
The result of that last attempt gets stored in Cache to prevent
|
||||
excessive DB queries.
|
||||
|
||||
The module defines two policies outside the regular bucket inspection:
|
||||
* when the site does not exist in the database: #{@policy_for_non_existing_sites}
|
||||
* when the underlying rate limiting mechanism returns
|
||||
an internal error: #{@policy_on_rate_limiting_backend_error}
|
||||
|
||||
Each policy computation emits a single telemetry event.
|
||||
See: `policy_telemetry_event/1`
|
||||
"""
|
||||
alias Plausible.Site
|
||||
alias Plausible.Site.Cache
|
||||
|
||||
require Logger
|
||||
|
||||
@spec allow?(String.t(), Keyword.t()) :: boolean()
|
||||
def allow?(domain, opts \\ []) do
|
||||
policy(domain, opts) == :allow
|
||||
end
|
||||
|
||||
@spec key(String.t()) :: String.t()
|
||||
def key(domain) do
|
||||
"ingest:site:#{domain}"
|
||||
end
|
||||
|
||||
@spec policy_telemetry_event(:allow | :deny) :: list(atom())
|
||||
def policy_telemetry_event(policy) do
|
||||
[:plausible, :ingest, :rate_limit, policy]
|
||||
end
|
||||
|
||||
defp policy(domain, opts) do
|
||||
result =
|
||||
case get_from_cache_or_refresh(domain, Keyword.get(opts, :cache_opts, [])) do
|
||||
%Ecto.NoResultsError{} ->
|
||||
@policy_for_non_existing_sites
|
||||
|
||||
%Site{} = site ->
|
||||
check_rate_limit(site, opts)
|
||||
|
||||
{:error, _} ->
|
||||
@policy_on_rate_limiting_backend_error
|
||||
end
|
||||
|
||||
:ok = emit_allowance_telemetry(result)
|
||||
result
|
||||
end
|
||||
|
||||
defp check_rate_limit(%Site{ingest_rate_limit_threshold: nil}, _opts) do
|
||||
:allow
|
||||
end
|
||||
|
||||
defp check_rate_limit(%Site{ingest_rate_limit_threshold: threshold} = site, opts)
|
||||
when is_integer(threshold) do
|
||||
key = Keyword.get(opts, :key, key(site.domain))
|
||||
scale_ms = site.ingest_rate_limit_scale_seconds * 1_000
|
||||
|
||||
case Hammer.check_rate(key, scale_ms, threshold) do
|
||||
{:deny, _} ->
|
||||
:deny
|
||||
|
||||
{:allow, _} ->
|
||||
:allow
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error(
|
||||
"Error checking rate limit for '#{key}': #{inspect(reason)}. Falling back to: #{@policy_on_rate_limiting_backend_error}"
|
||||
)
|
||||
|
||||
@policy_on_rate_limiting_backend_error
|
||||
end
|
||||
end
|
||||
|
||||
defp get_from_cache_or_refresh(domain, cache_opts) do
|
||||
case Cache.get(domain, cache_opts) do
|
||||
%Site{} = site ->
|
||||
site
|
||||
|
||||
%Ecto.NoResultsError{} = not_found ->
|
||||
not_found
|
||||
|
||||
nil ->
|
||||
with {:ok, refreshed_item} <- Cache.refresh_one(domain, cache_opts) do
|
||||
refreshed_item
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp emit_allowance_telemetry(policy) do
|
||||
:telemetry.execute(policy_telemetry_event(policy), %{}, %{})
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,8 @@
|
||||
defmodule Plausible.Site.Cache.Warmer.All do
|
||||
@moduledoc """
|
||||
A Cache.Warmer adapter that refreshes the Sites Cache fully.
|
||||
This module exists only to make it explicit what the warmer
|
||||
refreshes, to be used in the supervisor tree.
|
||||
"""
|
||||
defdelegate child_spec(opts), to: Plausible.Site.Cache.Warmer
|
||||
end
|
||||
@ -0,0 +1,20 @@
|
||||
defmodule Plausible.Site.Cache.Warmer.RecentlyUpdated do
|
||||
@moduledoc """
|
||||
A Cache.Warmer adapter that only refreshes the Cache
|
||||
with recently updated sites every 30 seconds.
|
||||
"""
|
||||
alias Plausible.Site.Cache
|
||||
|
||||
@spec child_spec(Keyword.t()) :: Supervisor.child_spec() | :ignore
|
||||
def child_spec(opts) do
|
||||
child_name = Keyword.get(opts, :child_name, __MODULE__)
|
||||
|
||||
opts = [
|
||||
child_name: child_name,
|
||||
interval: :timer.seconds(30),
|
||||
warmer_fn: &Cache.refresh_updated_recently/1
|
||||
]
|
||||
|
||||
Plausible.Site.Cache.Warmer.child_spec(opts)
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,87 @@
|
||||
defmodule Plausible.Site.GateKeeper do
|
||||
@type policy() :: :allow | :not_found | :block | :throttle
|
||||
@policy_for_non_existing_sites :not_found
|
||||
@policy_on_rate_limiting_backend_error :allow
|
||||
|
||||
@type t() :: :allow | {:deny, policy()}
|
||||
|
||||
@moduledoc """
|
||||
Thin wrapper around Hammer for gate keeping domain-specific events
|
||||
during the ingestion phase. When the site is allowed, gate keeping
|
||||
check returns `:allow`, otherwise a `:deny` tagged tuple is returned
|
||||
with one of the following policy markers:
|
||||
* `:not_found` (indicates site not found in cache)
|
||||
* `:block` (indicates disabled sites)
|
||||
* `:throttle` (indicates rate limiting)
|
||||
|
||||
Rate Limiting buckets are configured per site (externally via the CRM).
|
||||
See: `Plausible.Site`
|
||||
|
||||
To look up each site's configuration, the RateLimiter fetches
|
||||
a Site by domain using `Plausible.Cache` interface.
|
||||
|
||||
The module defines two policies outside the regular bucket inspection:
|
||||
* when the the site is not found in cache: #{@policy_for_non_existing_sites}
|
||||
* when the underlying rate limiting mechanism returns
|
||||
an internal error: #{@policy_on_rate_limiting_backend_error}
|
||||
"""
|
||||
alias Plausible.Site
|
||||
alias Plausible.Site.Cache
|
||||
|
||||
require Logger
|
||||
|
||||
@spec check(String.t(), Keyword.t()) :: t()
|
||||
def check(domain, opts \\ []) when is_binary(domain) do
|
||||
case policy(domain, opts) do
|
||||
:allow -> :allow
|
||||
other -> {:deny, other}
|
||||
end
|
||||
end
|
||||
|
||||
@spec key(String.t()) :: String.t()
|
||||
def key(domain) do
|
||||
"ingest:site:#{domain}"
|
||||
end
|
||||
|
||||
defp policy(domain, opts) when is_binary(domain) do
|
||||
result =
|
||||
case Cache.get(domain, Keyword.get(opts, :cache_opts, [])) do
|
||||
nil ->
|
||||
@policy_for_non_existing_sites
|
||||
|
||||
%Site{} = site ->
|
||||
check_rate_limit(site, opts)
|
||||
end
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
defp check_rate_limit(%Site{ingest_rate_limit_threshold: nil}, _opts) do
|
||||
:allow
|
||||
end
|
||||
|
||||
defp check_rate_limit(%Site{ingest_rate_limit_threshold: 0}, _opts) do
|
||||
:block
|
||||
end
|
||||
|
||||
defp check_rate_limit(%Site{ingest_rate_limit_threshold: threshold} = site, opts)
|
||||
when is_integer(threshold) do
|
||||
key = Keyword.get(opts, :key, key(site.domain))
|
||||
scale_ms = site.ingest_rate_limit_scale_seconds * 1_000
|
||||
|
||||
case Hammer.check_rate(key, scale_ms, threshold) do
|
||||
{:deny, _} ->
|
||||
:throttle
|
||||
|
||||
{:allow, _} ->
|
||||
:allow
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error(
|
||||
"Error checking rate limit for '#{key}': #{inspect(reason)}. Falling back to: #{@policy_on_rate_limiting_backend_error}"
|
||||
)
|
||||
|
||||
@policy_on_rate_limiting_backend_error
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,44 @@
|
||||
defmodule Plausible.Ingestion.EventTelemetryTest do
|
||||
import Phoenix.ConnTest
|
||||
|
||||
alias Plausible.Ingestion.Request
|
||||
alias Plausible.Ingestion.Event
|
||||
|
||||
use Plausible.DataCase, async: false
|
||||
|
||||
test "telemetry is emitted for all events", %{test: test} do
|
||||
test_pid = self()
|
||||
|
||||
telemetry_dropped = Event.telemetry_event_dropped()
|
||||
telemetry_buffered = Event.telemetry_event_buffered()
|
||||
|
||||
:telemetry.attach_many(
|
||||
"#{test}-telemetry-handler",
|
||||
[
|
||||
telemetry_dropped,
|
||||
telemetry_buffered
|
||||
],
|
||||
fn event, %{}, metadata, _ ->
|
||||
send(test_pid, {:telemetry_handled, event, metadata})
|
||||
end,
|
||||
%{}
|
||||
)
|
||||
|
||||
site = insert(:site, ingest_rate_limit_threshold: 2)
|
||||
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
url: "http://dummy.site",
|
||||
d: "#{site.domain}"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
|
||||
for _ <- 1..3, do: Event.build_and_buffer(request)
|
||||
|
||||
assert_receive {:telemetry_handled, ^telemetry_buffered, %{}}
|
||||
assert_receive {:telemetry_handled, ^telemetry_buffered, %{}}
|
||||
assert_receive {:telemetry_handled, ^telemetry_dropped, %{reason: :throttle}}
|
||||
end
|
||||
end
|
||||
@ -1,112 +1,102 @@
|
||||
defmodule Plausible.Ingestion.EventTest do
|
||||
use Plausible.DataCase
|
||||
|
||||
@valid_request %Plausible.Ingestion.Request{
|
||||
remote_ip: "2.2.2.2",
|
||||
user_agent:
|
||||
"Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405",
|
||||
event_name: "pageview",
|
||||
uri: URI.parse("http://skywalker.test"),
|
||||
referrer: "http://m.facebook.test/",
|
||||
screen_width: 1440,
|
||||
hash_mode: nil,
|
||||
query_params: %{
|
||||
"utm_medium" => "utm_medium",
|
||||
"utm_source" => "utm_source",
|
||||
"utm_campaign" => "utm_campaign",
|
||||
"utm_content" => "utm_content",
|
||||
"utm_term" => "utm_term",
|
||||
"source" => "source",
|
||||
"ref" => "ref"
|
||||
use Plausible.DataCase, async: true
|
||||
|
||||
import Phoenix.ConnTest
|
||||
|
||||
alias Plausible.Ingestion.Request
|
||||
alias Plausible.Ingestion.Event
|
||||
|
||||
test "event pipeline processes a request into an event" do
|
||||
site = insert(:site)
|
||||
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
url: "http://#{site.domain}"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
|
||||
assert {:ok, %{buffered: [_], dropped: []}} = Event.build_and_buffer(request)
|
||||
end
|
||||
|
||||
test "event pipeline drops a request when site does not exists" do
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
url: "http://dummy.site"
|
||||
}
|
||||
}
|
||||
|
||||
describe "integration" do
|
||||
test "build_and_buffer/1 creates an event" do
|
||||
assert {:ok, %{buffered: [_], dropped: []}} =
|
||||
@valid_request
|
||||
|> Map.put(:domains, ["plausible-ingestion-event-basic.test"])
|
||||
|> Plausible.Ingestion.Event.build_and_buffer()
|
||||
|
||||
assert %Plausible.ClickhouseEvent{
|
||||
session_id: session_id,
|
||||
user_id: user_id,
|
||||
domain: "plausible-ingestion-event-basic.test",
|
||||
browser: "Safari",
|
||||
browser_version: "",
|
||||
city_geoname_id: 2_988_507,
|
||||
country_code: "FR",
|
||||
hostname: "skywalker.test",
|
||||
"meta.key": [],
|
||||
"meta.value": [],
|
||||
name: "pageview",
|
||||
operating_system: "iOS",
|
||||
operating_system_version: "3.2",
|
||||
pathname: "/",
|
||||
referrer: "m.facebook.test",
|
||||
referrer_source: "utm_source",
|
||||
screen_size: "Desktop",
|
||||
subdivision1_code: "FR-IDF",
|
||||
subdivision2_code: "FR-75",
|
||||
transferred_from: "",
|
||||
utm_campaign: "utm_campaign",
|
||||
utm_content: "utm_content",
|
||||
utm_medium: "utm_medium",
|
||||
utm_source: "utm_source",
|
||||
utm_term: "utm_term"
|
||||
} = get_event("plausible-ingestion-event-basic.test")
|
||||
|
||||
assert is_integer(session_id)
|
||||
assert is_integer(user_id)
|
||||
end
|
||||
|
||||
test "build_and_buffer/1 takes multiple domains" do
|
||||
request = %Plausible.Ingestion.Request{
|
||||
@valid_request
|
||||
| domains: [
|
||||
"plausible-ingestion-event-multiple-1.test",
|
||||
"plausible-ingestion-event-multiple-2.test"
|
||||
]
|
||||
}
|
||||
|
||||
assert {:ok, %{buffered: [_, _], dropped: []}} =
|
||||
Plausible.Ingestion.Event.build_and_buffer(request)
|
||||
|
||||
assert %Plausible.ClickhouseEvent{domain: "plausible-ingestion-event-multiple-1.test"} =
|
||||
get_event("plausible-ingestion-event-multiple-1.test")
|
||||
|
||||
assert %Plausible.ClickhouseEvent{domain: "plausible-ingestion-event-multiple-2.test"} =
|
||||
get_event("plausible-ingestion-event-multiple-2.test")
|
||||
end
|
||||
|
||||
test "build_and_buffer/1 drops invalid events" do
|
||||
request = %Plausible.Ingestion.Request{
|
||||
@valid_request
|
||||
| domains: ["plausible-ingestion-event-multiple-with-error-1.test", nil]
|
||||
}
|
||||
|
||||
assert {:ok, %{buffered: [_], dropped: [dropped]}} =
|
||||
Plausible.Ingestion.Event.build_and_buffer(request)
|
||||
|
||||
assert {:error, changeset} = dropped.drop_reason
|
||||
refute changeset.valid?
|
||||
|
||||
assert %Plausible.ClickhouseEvent{
|
||||
domain: "plausible-ingestion-event-multiple-with-error-1.test"
|
||||
} = get_event("plausible-ingestion-event-multiple-with-error-1.test")
|
||||
end
|
||||
|
||||
defp get_event(domain) do
|
||||
Plausible.TestUtils.eventually(fn ->
|
||||
Plausible.Event.WriteBuffer.flush()
|
||||
|
||||
event =
|
||||
Plausible.ClickhouseRepo.one(
|
||||
from e in Plausible.ClickhouseEvent, where: e.domain == ^domain
|
||||
)
|
||||
|
||||
{!is_nil(event), event}
|
||||
end)
|
||||
end
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
|
||||
assert {:ok, %{buffered: [], dropped: [dropped]}} = Event.build_and_buffer(request)
|
||||
assert dropped.drop_reason == :not_found
|
||||
end
|
||||
|
||||
test "event pipeline drops a request when referrer is spam" do
|
||||
site = insert(:site)
|
||||
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
url: "http://dummy.site",
|
||||
referrer: "https://www.1-best-seo.com",
|
||||
domain: site.domain
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
|
||||
assert {:ok, %{buffered: [], dropped: [dropped]}} = Event.build_and_buffer(request)
|
||||
assert dropped.drop_reason == :spam_referrer
|
||||
end
|
||||
|
||||
test "event pipeline drops a request when referrer is spam for multiple domains" do
|
||||
site = insert(:site)
|
||||
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
url: "http://dummy.site",
|
||||
referrer: "https://www.1-best-seo.com",
|
||||
d: "#{site.domain},#{site.domain}"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
|
||||
assert {:ok, %{dropped: [dropped, dropped]}} = Event.build_and_buffer(request)
|
||||
assert dropped.drop_reason == :spam_referrer
|
||||
end
|
||||
|
||||
test "event pipeline selectively drops an event for multiple domains" do
|
||||
site = insert(:site)
|
||||
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
url: "http://dummy.site",
|
||||
d: "#{site.domain},thisdoesnotexist.com"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
|
||||
assert {:ok, %{buffered: [_], dropped: [dropped]}} = Event.build_and_buffer(request)
|
||||
assert dropped.drop_reason == :not_found
|
||||
end
|
||||
|
||||
test "event pipeline selectively drops an event when rate-limited" do
|
||||
site = insert(:site, ingest_rate_limit_threshold: 1)
|
||||
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
url: "http://dummy.site",
|
||||
d: "#{site.domain}"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
|
||||
assert {:ok, %{buffered: [_], dropped: []}} = Event.build_and_buffer(request)
|
||||
assert {:ok, %{buffered: [], dropped: [dropped]}} = Event.build_and_buffer(request)
|
||||
assert dropped.drop_reason == :throttle
|
||||
end
|
||||
end
|
||||
|
||||
@ -0,0 +1,187 @@
|
||||
defmodule Plausible.Ingestion.RequestTest do
|
||||
use Plausible.DataCase, async: true
|
||||
|
||||
import Phoenix.ConnTest
|
||||
import Plug.Conn
|
||||
|
||||
alias Plausible.Ingestion.Request
|
||||
|
||||
test "request cannot be built from conn without payload" do
|
||||
conn = build_conn(:post, "/api/events", %{})
|
||||
assert {:error, changeset} = Request.build(conn)
|
||||
|
||||
errors = Keyword.keys(changeset.errors)
|
||||
assert :event_name in errors
|
||||
assert :domain in errors
|
||||
assert :url in errors
|
||||
end
|
||||
|
||||
test "request cannot be built from non-json payload" do
|
||||
conn = build_conn(:post, "/api/events", "defnotjson")
|
||||
assert {:error, changeset} = Request.build(conn)
|
||||
assert changeset.errors[:request]
|
||||
end
|
||||
|
||||
test "request can be built from URL alone" do
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
url: "http://dummy.site"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
|
||||
assert request.domains == ["dummy.site"]
|
||||
assert request.event_name == "pageview"
|
||||
assert request.pathname == "/"
|
||||
assert request.remote_ip == "127.0.0.1"
|
||||
assert %NaiveDateTime{} = request.timestamp
|
||||
assert request.user_agent == nil
|
||||
assert request.hostname == "dummy.site"
|
||||
assert request.uri.host == "dummy.site"
|
||||
assert request.uri.scheme == "http"
|
||||
assert request.props == %{}
|
||||
end
|
||||
|
||||
test "request can be built with domain" do
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
domain: "dummy.site",
|
||||
url: "http://dummy.site/index"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
assert request.domains == ["dummy.site"]
|
||||
assert request.uri.host == "dummy.site"
|
||||
end
|
||||
|
||||
test "request can be built with domain using shorthands" do
|
||||
payload = %{
|
||||
n: "pageview",
|
||||
d: "dummy.site",
|
||||
u: "http://dummy.site/index"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
assert request.domains == ["dummy.site"]
|
||||
assert request.uri.host == "dummy.site"
|
||||
end
|
||||
|
||||
test "request can be built for multiple domains" do
|
||||
payload = %{
|
||||
n: "pageview",
|
||||
d: "dummy.site,crash.site",
|
||||
u: "http://dummy.site/index"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
assert request.domains == ["dummy.site", "crash.site"]
|
||||
assert request.uri.host == "dummy.site"
|
||||
end
|
||||
|
||||
test "hostname is (none) if host-less uri provided" do
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
domain: "dummy.site",
|
||||
url: "about:config"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
assert request.hostname == "(none)"
|
||||
end
|
||||
|
||||
test "hostname is set" do
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
domain: "dummy.site",
|
||||
url: "http://dummy.site/index.html"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
assert request.hostname == "dummy.site"
|
||||
end
|
||||
|
||||
test "user agent is set as-is" do
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
domain: "dummy.site",
|
||||
url: "http://dummy.site/index.html"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload) |> put_req_header("user-agent", "Mozilla")
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
assert request.user_agent == "Mozilla"
|
||||
end
|
||||
|
||||
test "request params are set" do
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
domain: "dummy.site",
|
||||
url: "http://dummy.site/index.html",
|
||||
referrer: "https://example.com",
|
||||
screen_width: 1024,
|
||||
hashMode: 1,
|
||||
props: %{
|
||||
"custom1" => "property1",
|
||||
"custom2" => "property2"
|
||||
}
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
assert request.referrer == "https://example.com"
|
||||
assert request.screen_width == 1024
|
||||
assert request.hash_mode == 1
|
||||
assert request.props["custom1"] == "property1"
|
||||
assert request.props["custom2"] == "property2"
|
||||
end
|
||||
|
||||
test "pathname is set" do
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
domain: "dummy.site",
|
||||
url: "http://dummy.site/pictures/index.html#foo"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
|
||||
assert request.pathname == "/pictures/index.html"
|
||||
end
|
||||
|
||||
test "pathname is set with hashMode" do
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
domain: "dummy.site",
|
||||
url: "http://dummy.site/pictures/index.html#foo",
|
||||
hashMode: 1
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
|
||||
assert request.pathname == "/pictures/index.html#foo"
|
||||
end
|
||||
|
||||
test "query params are set" do
|
||||
payload = %{
|
||||
name: "pageview",
|
||||
domain: "dummy.site",
|
||||
url: "http://dummy.site/pictures/index.html?foo=bar&baz=bam"
|
||||
}
|
||||
|
||||
conn = build_conn(:post, "/api/events", payload)
|
||||
|
||||
assert {:ok, request} = Request.build(conn)
|
||||
assert request.query_params["foo"] == "bar"
|
||||
assert request.query_params["baz"] == "bam"
|
||||
end
|
||||
end
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue