|
| 1 | +// Copyright 2021 The go-github AUTHORS. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | + |
| 6 | +package github |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "net/http" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/google/go-cmp/cmp" |
| 15 | +) |
| 16 | + |
| 17 | +func TestAppsService_ListHookDeliveries(t *testing.T) { |
| 18 | + client, mux, _, teardown := setup() |
| 19 | + defer teardown() |
| 20 | + |
| 21 | + mux.HandleFunc("/app/hook/deliveries", func(w http.ResponseWriter, r *http.Request) { |
| 22 | + testMethod(t, r, "GET") |
| 23 | + testFormValues(t, r, values{"cursor": "v1_12077215967"}) |
| 24 | + fmt.Fprint(w, `[{"id":1}, {"id":2}]`) |
| 25 | + }) |
| 26 | + |
| 27 | + opts := &ListCursorOptions{Cursor: "v1_12077215967"} |
| 28 | + |
| 29 | + ctx := context.Background() |
| 30 | + |
| 31 | + deliveries, _, err := client.Apps.ListHookDeliveries(ctx, opts) |
| 32 | + if err != nil { |
| 33 | + t.Errorf("Apps.ListHookDeliveries returned error: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + want := []*HookDelivery{{ID: Int64(1)}, {ID: Int64(2)}} |
| 37 | + if d := cmp.Diff(deliveries, want); d != "" { |
| 38 | + t.Errorf("Apps.ListHooks want (-), got (+):\n%s", d) |
| 39 | + } |
| 40 | + |
| 41 | + const methodName = "ListHookDeliveries" |
| 42 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 43 | + got, resp, err := client.Apps.ListHookDeliveries(ctx, opts) |
| 44 | + if got != nil { |
| 45 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 46 | + } |
| 47 | + return resp, err |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +func TestAppsService_GetHookDelivery(t *testing.T) { |
| 52 | + client, mux, _, teardown := setup() |
| 53 | + defer teardown() |
| 54 | + |
| 55 | + mux.HandleFunc("/app/hook/deliveries/1", func(w http.ResponseWriter, r *http.Request) { |
| 56 | + testMethod(t, r, "GET") |
| 57 | + fmt.Fprint(w, `{"id":1}`) |
| 58 | + }) |
| 59 | + |
| 60 | + ctx := context.Background() |
| 61 | + hook, _, err := client.Apps.GetHookDelivery(ctx, 1) |
| 62 | + if err != nil { |
| 63 | + t.Errorf("Apps.GetHookDelivery returned error: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + want := &HookDelivery{ID: Int64(1)} |
| 67 | + if !cmp.Equal(hook, want) { |
| 68 | + t.Errorf("Apps.GetHookDelivery returned %+v, want %+v", hook, want) |
| 69 | + } |
| 70 | + |
| 71 | + const methodName = "GetHookDelivery" |
| 72 | + testBadOptions(t, methodName, func() (err error) { |
| 73 | + _, _, err = client.Apps.GetHookDelivery(ctx, -1) |
| 74 | + return err |
| 75 | + }) |
| 76 | + |
| 77 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 78 | + got, resp, err := client.Apps.GetHookDelivery(ctx, 1) |
| 79 | + if got != nil { |
| 80 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 81 | + } |
| 82 | + return resp, err |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +func TestAppsService_RedeliverHookDelivery(t *testing.T) { |
| 87 | + client, mux, _, teardown := setup() |
| 88 | + defer teardown() |
| 89 | + |
| 90 | + mux.HandleFunc("/app/hook/deliveries/1/attempts", func(w http.ResponseWriter, r *http.Request) { |
| 91 | + testMethod(t, r, "POST") |
| 92 | + fmt.Fprint(w, `{"id":1}`) |
| 93 | + }) |
| 94 | + |
| 95 | + ctx := context.Background() |
| 96 | + hook, _, err := client.Apps.RedeliverHookDelivery(ctx, 1) |
| 97 | + if err != nil { |
| 98 | + t.Errorf("Apps.RedeliverHookDelivery returned error: %v", err) |
| 99 | + } |
| 100 | + |
| 101 | + want := &HookDelivery{ID: Int64(1)} |
| 102 | + if !cmp.Equal(hook, want) { |
| 103 | + t.Errorf("Apps.RedeliverHookDelivery returned %+v, want %+v", hook, want) |
| 104 | + } |
| 105 | + |
| 106 | + const methodName = "RedeliverHookDelivery" |
| 107 | + testBadOptions(t, methodName, func() (err error) { |
| 108 | + _, _, err = client.Apps.RedeliverHookDelivery(ctx, -1) |
| 109 | + return err |
| 110 | + }) |
| 111 | + |
| 112 | + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { |
| 113 | + got, resp, err := client.Apps.RedeliverHookDelivery(ctx, 1) |
| 114 | + if got != nil { |
| 115 | + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) |
| 116 | + } |
| 117 | + return resp, err |
| 118 | + }) |
| 119 | +} |
0 commit comments