From e2066994b1e64481e0e74350688d91c71d03e230 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 19 Dec 2023 10:18:29 -0500 Subject: [PATCH] Fix Web Push notification delivery Finch does not automatically append header content-type: octet-stream for binary payloads. --- changelog.d/web_push.fix | 1 + lib/pleroma/http/web_push.ex | 6 +++- test/pleroma/http/web_push_test.exs | 45 +++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 changelog.d/web_push.fix create mode 100644 test/pleroma/http/web_push_test.exs diff --git a/changelog.d/web_push.fix b/changelog.d/web_push.fix new file mode 100644 index 000000000..cf933e2d4 --- /dev/null +++ b/changelog.d/web_push.fix @@ -0,0 +1 @@ +Fix web push notifications not successfully delivering diff --git a/lib/pleroma/http/web_push.ex b/lib/pleroma/http/web_push.ex index ca399b6c8..888079c1e 100644 --- a/lib/pleroma/http/web_push.ex +++ b/lib/pleroma/http/web_push.ex @@ -6,7 +6,11 @@ defmodule Pleroma.HTTP.WebPush do @moduledoc false def post(url, payload, headers, options \\ []) do - list_headers = Map.to_list(headers) + list_headers = + headers + |> Map.to_list() + |> Kernel.++([{"content-type", "octet-stream"}]) + Pleroma.HTTP.post(url, payload, list_headers, options) end end diff --git a/test/pleroma/http/web_push_test.exs b/test/pleroma/http/web_push_test.exs new file mode 100644 index 000000000..dd8e45e6a --- /dev/null +++ b/test/pleroma/http/web_push_test.exs @@ -0,0 +1,45 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.HTTP.WebPushTest do + use ExUnit.Case + + import Tesla.Mock + alias Pleroma.HTTP + + @push_url "https://some-push-server/" + + setup do + mock(fn + %{ + method: :post, + url: @push_url, + headers: headers + } -> + if {"content-type", "octet-stream"} in headers do + %Tesla.Env{ + status: 200 + } + else + %Tesla.Env{ + status: 403 + } + end + end) + + :ok + end + + test "post" do + response = + HTTP.WebPush.post( + @push_url, + "encrypted payload", + %{"authorization" => "WebPush"}, + [] + ) + + assert {:ok, %{status: 200}} = response + end +end