1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
// Copyright 2018 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestRepositoriesService_ListPreReceiveHooks(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/pre-receive-hooks", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypePreReceiveHooksPreview)
testFormValues(t, r, values{"page": "2"})
fmt.Fprint(w, `[{"id":1}, {"id":2}]`)
})
opt := &ListOptions{Page: 2}
ctx := context.Background()
hooks, _, err := client.Repositories.ListPreReceiveHooks(ctx, "o", "r", opt)
if err != nil {
t.Errorf("Repositories.ListHooks returned error: %v", err)
}
want := []*PreReceiveHook{{ID: Int64(1)}, {ID: Int64(2)}}
if !cmp.Equal(hooks, want) {
t.Errorf("Repositories.ListPreReceiveHooks returned %+v, want %+v", hooks, want)
}
const methodName = "ListPreReceiveHooks"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.ListPreReceiveHooks(ctx, "\n", "\n", opt)
return err
})
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.ListPreReceiveHooks(ctx, "o", "r", opt)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}
func TestRepositoriesService_ListPreReceiveHooks_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
ctx := context.Background()
_, _, err := client.Repositories.ListPreReceiveHooks(ctx, "%", "%", nil)
testURLParseError(t, err)
}
func TestRepositoriesService_GetPreReceiveHook(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
testHeader(t, r, "Accept", mediaTypePreReceiveHooksPreview)
fmt.Fprint(w, `{"id":1}`)
})
ctx := context.Background()
hook, _, err := client.Repositories.GetPreReceiveHook(ctx, "o", "r", 1)
if err != nil {
t.Errorf("Repositories.GetPreReceiveHook returned error: %v", err)
}
want := &PreReceiveHook{ID: Int64(1)}
if !cmp.Equal(hook, want) {
t.Errorf("Repositories.GetPreReceiveHook returned %+v, want %+v", hook, want)
}
const methodName = "GetPreReceiveHook"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.GetPreReceiveHook(ctx, "\n", "\n", -1)
return err
})
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.GetPreReceiveHook(ctx, "o", "r", 1)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}
func TestRepositoriesService_GetPreReceiveHook_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
ctx := context.Background()
_, _, err := client.Repositories.GetPreReceiveHook(ctx, "%", "%", 1)
testURLParseError(t, err)
}
func TestRepositoriesService_UpdatePreReceiveHook(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
input := &PreReceiveHook{}
mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) {
v := new(PreReceiveHook)
assertNilError(t, json.NewDecoder(r.Body).Decode(v))
testMethod(t, r, "PATCH")
if !cmp.Equal(v, input) {
t.Errorf("Request body = %+v, want %+v", v, input)
}
fmt.Fprint(w, `{"id":1}`)
})
ctx := context.Background()
hook, _, err := client.Repositories.UpdatePreReceiveHook(ctx, "o", "r", 1, input)
if err != nil {
t.Errorf("Repositories.UpdatePreReceiveHook returned error: %v", err)
}
want := &PreReceiveHook{ID: Int64(1)}
if !cmp.Equal(hook, want) {
t.Errorf("Repositories.UpdatePreReceiveHook returned %+v, want %+v", hook, want)
}
const methodName = "UpdatePreReceiveHook"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Repositories.UpdatePreReceiveHook(ctx, "\n", "\n", -1, input)
return err
})
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Repositories.UpdatePreReceiveHook(ctx, "o", "r", 1, input)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}
func TestRepositoriesService_PreReceiveHook_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
ctx := context.Background()
_, _, err := client.Repositories.UpdatePreReceiveHook(ctx, "%", "%", 1, nil)
testURLParseError(t, err)
}
func TestRepositoriesService_DeletePreReceiveHook(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
mux.HandleFunc("/repos/o/r/pre-receive-hooks/1", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
})
ctx := context.Background()
_, err := client.Repositories.DeletePreReceiveHook(ctx, "o", "r", 1)
if err != nil {
t.Errorf("Repositories.DeletePreReceiveHook returned error: %v", err)
}
const methodName = "DeletePreReceiveHook"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Repositories.DeletePreReceiveHook(ctx, "\n", "\n", -1)
return err
})
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Repositories.DeletePreReceiveHook(ctx, "o", "r", 1)
})
}
func TestRepositoriesService_DeletePreReceiveHook_invalidOwner(t *testing.T) {
client, _, _, teardown := setup()
defer teardown()
ctx := context.Background()
_, err := client.Repositories.DeletePreReceiveHook(ctx, "%", "%", 1)
testURLParseError(t, err)
}
func TestPreReceiveHook_Marshal(t *testing.T) {
testJSONMarshal(t, &PreReceiveHook{}, "{}")
u := &PreReceiveHook{
ID: Int64(1),
Name: String("name"),
Enforcement: String("e"),
ConfigURL: String("curl"),
}
want := `{
"id": 1,
"name": "name",
"enforcement": "e",
"configuration_url": "curl"
}`
testJSONMarshal(t, u, want)
}
|