summaryrefslogtreecommitdiff
path: root/errdefs/http_helpers_test.go
blob: 7806d283901ee0d343c928e7f4363be6b03e1de8 (plain)
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
package errdefs

import (
	"fmt"
	"net/http"
	"testing"

	"gotest.tools/v3/assert"
)

func TestFromStatusCode(t *testing.T) {
	testErr := fmt.Errorf("some error occurred")

	testCases := []struct {
		err    error
		status int
		check  func(error) bool
	}{
		{
			err:    testErr,
			status: http.StatusNotFound,
			check:  IsNotFound,
		},
		{
			err:    testErr,
			status: http.StatusBadRequest,
			check:  IsInvalidParameter,
		},
		{
			err:    testErr,
			status: http.StatusConflict,
			check:  IsConflict,
		},
		{
			err:    testErr,
			status: http.StatusUnauthorized,
			check:  IsUnauthorized,
		},
		{
			err:    testErr,
			status: http.StatusServiceUnavailable,
			check:  IsUnavailable,
		},
		{
			err:    testErr,
			status: http.StatusForbidden,
			check:  IsForbidden,
		},
		{
			err:    testErr,
			status: http.StatusNotModified,
			check:  IsNotModified,
		},
		{
			err:    testErr,
			status: http.StatusNotImplemented,
			check:  IsNotImplemented,
		},
		{
			err:    testErr,
			status: http.StatusInternalServerError,
			check:  IsSystem,
		},
		{
			err:    Unknown(testErr),
			status: http.StatusInternalServerError,
			check:  IsUnknown,
		},
		{
			err:    DataLoss(testErr),
			status: http.StatusInternalServerError,
			check:  IsDataLoss,
		},
		{
			err:    Deadline(testErr),
			status: http.StatusInternalServerError,
			check:  IsDeadline,
		},
		{
			err:    Cancelled(testErr),
			status: http.StatusInternalServerError,
			check:  IsCancelled,
		},
	}

	for _, tc := range testCases {
		t.Run(http.StatusText(tc.status), func(t *testing.T) {
			err := FromStatusCode(tc.err, tc.status)
			assert.Check(t, tc.check(err), "unexpected error-type %T", err)
		})
	}
}