Skip to content
quarry Artifact [d8a5c50698]
Login

Artifact [d8a5c50698]

Artifact d8a5c50698ebdca3de2de1ae0bd9464f3575a65966e7c6ce2232a8f38dffe4e8:


package notify

import (
	"context"
	"net/http"
	"net/http/httptest"
	"testing"
)

// notify 0.1.9 records each device's outcome in the push message's
// extras.devices; the client hands them on, one per device.
func TestPushOutcomeCarriesEachDevicesOutcome(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch r.URL.Path {
		case "/v1/notifications":
			w.Write([]byte(`{"items":[{"ID":"n1"}]}`))
		case "/v1/notifications/n1/messages":
			w.Write([]byte(`[{"Channel":"push","Status":"sent","ErrorText":"delivered 1/2: 1 failure(s) (x)",` +
				`"extras":{"devices":[` +
				`{"subscriptionId":"s1","host":"fcm.googleapis.com","outcome":"delivered"},` +
				`{"subscriptionId":"s2","host":"web.push.apple.com","outcome":"retired","status":403,"reason":"BadJwtToken"}]}}]`))
		default:
			t.Errorf("unexpected %s", r.URL.Path)
		}
	}))
	defer srv.Close()

	res, err := New(srv.URL, "").PushOutcome(context.Background(), "dan", "txn-1")
	if err != nil {
		t.Fatal(err)
	}
	if len(res.Devices) != 2 {
		t.Fatalf("FAIL: %d devices, want 2: %+v", len(res.Devices), res.Devices)
	}
	if d := res.Devices[1]; d.Host != "web.push.apple.com" || d.Outcome != "retired" || d.Status != 403 || d.Reason != "BadJwtToken" {
		t.Errorf("FAIL: second device %+v", d)
	}
	if d := res.Devices[0]; d.Outcome != "delivered" || d.Status != 0 {
		t.Errorf("FAIL: first device %+v", d)
	}
}