-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
107 lines (99 loc) · 3.17 KB
/
errors_test.go
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
// Copyright (c) 2022, Direct Decisions Go client 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 directdecisions_test
import (
"context"
"net/http"
"strings"
"testing"
"directdecisions.com/directdecisions"
)
func TestErrors(t *testing.T) {
for _, tc := range []struct {
body string
status int
errors []error
}{
{
status: http.StatusBadRequest,
errors: []error{directdecisions.ErrHTTPStatusBadRequest},
},
{
status: http.StatusUnauthorized,
errors: []error{directdecisions.ErrHTTPStatusUnauthorized},
},
{
status: http.StatusForbidden,
errors: []error{directdecisions.ErrHTTPStatusForbidden},
},
{
status: http.StatusNotFound,
errors: []error{directdecisions.ErrHTTPStatusNotFound},
},
{
status: http.StatusMethodNotAllowed,
errors: []error{directdecisions.ErrHTTPStatusMethodNotAllowed},
},
{
status: http.StatusTooManyRequests,
errors: []error{directdecisions.ErrHTTPStatusTooManyRequests},
},
{
status: http.StatusInternalServerError,
errors: []error{directdecisions.ErrHTTPStatusInternalServerError},
},
{
status: http.StatusServiceUnavailable,
errors: []error{directdecisions.ErrHTTPStatusServiceUnavailable},
},
{
status: http.StatusBadGateway,
errors: []error{directdecisions.ErrHTTPStatusBadGateway},
},
{
body: `{"message": "Bad Request", "Code": 400, "errors": ["Invalid Data"]}`,
status: http.StatusBadRequest,
errors: []error{directdecisions.ErrHTTPStatusBadRequest, directdecisions.ErrInvalidData},
},
{
body: `{"message": "Bad Request", "Code": 400, "errors": ["Missing Choices"]}`,
status: http.StatusBadRequest,
errors: []error{directdecisions.ErrHTTPStatusBadRequest, directdecisions.ErrMissingChoices},
},
{
body: `{"message": "Bad Request", "Code": 400, "errors": ["Choice Required"]}`,
status: http.StatusBadRequest,
errors: []error{directdecisions.ErrHTTPStatusBadRequest, directdecisions.ErrChoiceRequired},
},
{
body: `{"message": "Bad Request", "Code": 400, "errors": ["Choice Too Long"]}`,
status: http.StatusBadRequest,
errors: []error{directdecisions.ErrHTTPStatusBadRequest, directdecisions.ErrChoiceTooLong},
},
{
body: `{"message": "Bad Request", "Code": 400, "errors": ["Ballot Required", "Voter ID Too Long", "Invalid Voter ID"]}`,
status: http.StatusBadRequest,
errors: []error{directdecisions.ErrHTTPStatusBadRequest, directdecisions.ErrBallotRequired, directdecisions.ErrVoterIDTooLong, directdecisions.ErrInvalidVoterID},
},
} {
t.Run(joinErrors(tc.errors), func(t *testing.T) {
client, mux, _ := newClient(t, "")
mux.HandleFunc("/v1/votings", requireMethod("POST", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(tc.status)
_, _ = w.Write([]byte(tc.body))
}))
_, err := client.Votings.Create(context.Background(), nil)
assertErrors(t, err, tc.errors...)
})
}
}
func joinErrors(errors []error) string {
s := make([]string, len(errors))
for i, err := range errors {
s[i] = err.Error()
}
return strings.Join(s, ", ")
}