Skip to content

Commit

Permalink
test single and multi tests. remove the hilarious testing random toke…
Browse files Browse the repository at this point in the history
…n modifier
  • Loading branch information
jessepeterson committed Aug 29, 2023
1 parent 1091c4a commit 47e100c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
4 changes: 0 additions & 4 deletions push/nanopush/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"strconv"
"strings"
Expand Down Expand Up @@ -72,9 +71,6 @@ func (p *Provider) do(ctx context.Context, pushInfo *mdm.Push) *push.Response {
jsonPayload := []byte(`{"mdm":"` + pushInfo.PushMagic + `"}`)

url := p.baseURL + "/3/device/" + pushInfo.Token.String()
if rand.Intn(2) == 0 {
url += "x"
}
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(jsonPayload))

if err != nil {
Expand Down
89 changes: 66 additions & 23 deletions push/nanopush/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nanopush
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand All @@ -13,22 +12,67 @@ import (
)

func TestPush(t *testing.T) {
deviceToken := "c2732227a1d8021cfaf781d71fb2f908c61f5861079a00954a5453f1d0281433"
pushMagic := "47250C9C-1B37-4381-98A9-0B8315A441C7"
topic := "com.example.apns-topic"
payload := []byte(`{"mdm":"` + pushMagic + `"}`)
// our "raw" push info
devicePushInfoStrings := [][]string{
{
"c2732227a1d8021cfaf781d71fb2f908c61f5861079a00954a5453f1d0281433",
"47250C9C-1B37-4381-98A9-0B8315A441C7",
"com.example.apns-topic",
},
}

// test a single push
t.Run("single-push", func(t *testing.T) {
testPushDevices(t, devicePushInfoStrings)
})

devicePushInfoStrings = append(devicePushInfoStrings, []string{
"7f1839ca30d5c6d36d6ae426258c4306c14eca90afd709a07375a85ad5a11c69",
"1C0B33FD-9336-4A7A-A080-7BEA9BD032EC",
"com.example.apns-topic",
})

// test a multiple push
t.Run("multiple-push", func(t *testing.T) {
testPushDevices(t, devicePushInfoStrings)
})
}

func testPushDevices(t *testing.T, input [][]string) {
// assemble it into a list and map
devices := make(map[string]*mdm.Push)
var pushInfos []*mdm.Push
for _, devicePushInfos := range input {
pushInfo := &mdm.Push{
PushMagic: devicePushInfos[1],
Topic: devicePushInfos[2],
}
pushInfo.SetTokenString(devicePushInfos[0])
devices[devicePushInfos[0]] = pushInfo
pushInfos = append(pushInfos, pushInfo)
}

apnsID := "922D9F1F-B82E-B337-EDC9-DB4FC8527676"

handler := http.NewServeMux()
server := httptest.NewServer(handler)
defer server.Close()

handler.HandleFunc("/3/device/", func(w http.ResponseWriter, r *http.Request) {
expectURL := fmt.Sprintf("/3/device/%s", deviceToken)
if have, want := r.URL.String(), expectURL; have != want {
t.Errorf("url: have %q, want %q", have, want)
url := r.URL.String()
var device string
var pushMagic string
if len(url) > 11 && url[:10] == "/3/device/" {
device = url[10:]
if _, ok := devices[device]; !ok {
t.Errorf("device id not present: %s", device)
} else {
pushMagic = devices[device].PushMagic
}
} else {
t.Fatal("invalid URL form")
}

payload := []byte(`{"mdm":"` + pushMagic + `"}`)

body, err := io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
Expand All @@ -40,28 +84,27 @@ func TestPush(t *testing.T) {
w.Header().Set("apns-id", apnsID)
})

server := httptest.NewServer(handler)
defer server.Close()

prov := &Provider{
baseURL: server.URL,
client: http.DefaultClient,
}

pushInfo := &mdm.Push{
PushMagic: pushMagic,
Topic: topic,
}
pushInfo.SetTokenString(deviceToken)

resp, err := prov.Push(context.Background(), []*mdm.Push{pushInfo})
resp, err := prov.Push(context.Background(), pushInfos)
if err != nil {
t.Fatal(err)
}

result, ok := resp[deviceToken]
if !ok || result == nil {
t.Fatal("device token not found (or is nil) in response")
for k, v := range resp {
if _, ok := devices[k]; !ok || v == nil {
t.Errorf("device not found (or is nil): %s", k)
} else {
if have, want := v.Id, apnsID; have != want {
t.Errorf("url: have %q, want %q", have, want)
}
}
}

if have, want := result.Id, apnsID; have != want {
t.Errorf("url: have %q, want %q", have, want)
}
}

0 comments on commit 47e100c

Please sign in to comment.