-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statuscode_test.go
72 lines (64 loc) · 1.7 KB
/
statuscode_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
// Copyright (c) 2023, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package errors
import (
stderrors "errors"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWithStatusCode(t *testing.T) {
for name, wantErr := range provideErrors(true) {
t.Run(name, func(t *testing.T) {
haveErr := WithStatusCode(wantErr, http.StatusOK)
assert.Exactly(t, http.StatusOK, GetStatusCode(haveErr))
assert.ErrorIs(t, haveErr, wantErr)
// update existing exitcode
t.Run("update", func(t *testing.T) {
haveErr2 := WithStatusCode(haveErr, http.StatusContinue)
assert.Exactly(t, http.StatusContinue, GetStatusCode(haveErr2))
assert.Same(t, haveErr, haveErr2)
})
})
}
t.Run("nil", func(t *testing.T) {
assert.Exactly(t, nil, WithStatusCode(nil, http.StatusNotFound))
})
}
func TestGetStatusCodeOr(t *testing.T) {
tests := map[string]struct {
err error
or int
want int
}{
"nil": {
err: nil,
or: http.StatusOK,
want: http.StatusOK,
},
"std error": {
err: stderrors.New("std err"),
or: http.StatusContinue,
want: http.StatusContinue,
},
"std error with exit code": {
err: WithStatusCode(stderrors.New("std err"), http.StatusAccepted),
want: http.StatusAccepted,
},
"error": {
err: New("some error without exit code"),
or: http.StatusNotFound,
want: http.StatusNotFound,
},
"error with exit code": {
err: WithStatusCode(New("bar"), http.StatusFound),
want: http.StatusFound,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
assert.Exactly(t, tc.want, GetStatusCodeOr(tc.err, tc.or))
})
}
}