-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy patherrors_test.go
169 lines (136 loc) · 4.42 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
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
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package http
import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func CleanupEnv() {
env = ""
}
func TestFormatError(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "prod"
s := formatError(errors.New("test error"))
assert.Equal(t, "", s)
env = "dev"
s = formatError(errors.New("test error"))
lines := strings.Split(s, "\n")
assert.Equal(t, "[DEV] test error", lines[0])
}
func TestHandleErrOnBadRequest(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
f := func(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, errors.New("test error"), http.StatusBadRequest)
}
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
f(rec, req)
resp := rec.Result()
errResponse := ErrorResponse{}
err = json.NewDecoder(resp.Body).Decode(&errResponse)
if err != nil {
t.Fatal(err)
}
if len(errResponse.Errors) != 1 {
t.Fatal("expecting exactly one error")
}
assert.Equal(t, http.StatusBadRequest, errResponse.Errors[0].Extensions.Status)
assert.Equal(t, http.StatusText(http.StatusBadRequest), errResponse.Errors[0].Extensions.HTTPError)
assert.Equal(t, "test error", errResponse.Errors[0].Message)
assert.Contains(t, errResponse.Errors[0].Extensions.Stack, "[DEV] test error")
}
func TestHandleErrOnInternalServerError(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
f := func(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, errors.New("test error"), http.StatusInternalServerError)
}
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
f(rec, req)
resp := rec.Result()
errResponse := ErrorResponse{}
err = json.NewDecoder(resp.Body).Decode(&errResponse)
if err != nil {
t.Fatal(err)
}
if len(errResponse.Errors) != 1 {
t.Fatal("expecting exactly one error")
}
assert.Equal(t, http.StatusInternalServerError, errResponse.Errors[0].Extensions.Status)
assert.Equal(t, http.StatusText(http.StatusInternalServerError), errResponse.Errors[0].Extensions.HTTPError)
assert.Equal(t, "test error", errResponse.Errors[0].Message)
assert.Contains(t, errResponse.Errors[0].Extensions.Stack, "[DEV] test error")
}
func TestHandleErrOnNotFound(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
f := func(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, errors.New("test error"), http.StatusNotFound)
}
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
f(rec, req)
resp := rec.Result()
errResponse := ErrorResponse{}
err = json.NewDecoder(resp.Body).Decode(&errResponse)
if err != nil {
t.Fatal(err)
}
if len(errResponse.Errors) != 1 {
t.Fatal("expecting exactly one error")
}
assert.Equal(t, http.StatusNotFound, errResponse.Errors[0].Extensions.Status)
assert.Equal(t, http.StatusText(http.StatusNotFound), errResponse.Errors[0].Extensions.HTTPError)
assert.Equal(t, "test error", errResponse.Errors[0].Message)
assert.Contains(t, errResponse.Errors[0].Extensions.Stack, "[DEV] test error")
}
func TestHandleErrOnDefault(t *testing.T) {
t.Cleanup(CleanupEnv)
env = "dev"
f := func(rw http.ResponseWriter, req *http.Request) {
handleErr(req.Context(), rw, errors.New("unauthorized"), http.StatusUnauthorized)
}
req, err := http.NewRequest("GET", "/test", nil)
if err != nil {
t.Fatal(err)
}
rec := httptest.NewRecorder()
f(rec, req)
resp := rec.Result()
errResponse := ErrorResponse{}
err = json.NewDecoder(resp.Body).Decode(&errResponse)
if err != nil {
t.Fatal(err)
}
if len(errResponse.Errors) != 1 {
t.Fatal("expecting exactly one error")
}
assert.Equal(t, http.StatusUnauthorized, errResponse.Errors[0].Extensions.Status)
assert.Equal(t, http.StatusText(http.StatusUnauthorized), errResponse.Errors[0].Extensions.HTTPError)
assert.Equal(t, "unauthorized", errResponse.Errors[0].Message)
assert.Contains(t, errResponse.Errors[0].Extensions.Stack, "[DEV] unauthorized")
}