Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
129 lines (109 loc) · 3.54 KB
/
Copy patherrors_test.go
File metadata and controls
129 lines (109 loc) · 3.54 KB
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
// Copyright (c) Liam Stanley <liam@liam.sh>. All rights reserved. Use of
// this source code is governed by the MIT license that can be found in
// the LICENSE file.
package chix
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
const dummyDSN = "DUMMY_DSN=postgres://audit:audit@127.0.0.1/dummy"
type dummyExposableError struct {
msg string
public bool
}
func (e dummyExposableError) Error() string { return e.msg }
func (e dummyExposableError) Public() bool { return e.public }
func TestIsExposableError(t *testing.T) {
tests := []struct {
name string
err error
want bool
}{
{name: "nil", err: nil, want: false},
{name: "public true", err: dummyExposableError{msg: dummyDSN, public: true}, want: true},
{name: "public false", err: dummyExposableError{msg: dummyDSN, public: false}, want: false},
{name: "plain error", err: errors.New(dummyDSN), want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsExposableError(tt.err); got != tt.want {
t.Fatalf("IsExposableError() = %v, want %v", got, tt.want)
}
})
}
}
func TestErrorWithCode_5xxMasking(t *testing.T) {
const publicMsg = "SAFE_PUBLIC_5XX"
tests := []struct {
name string
err error
wantContain string
wantAbsent string
}{
{
name: "public false",
err: dummyExposableError{msg: dummyDSN, public: false},
wantContain: http.StatusText(http.StatusInternalServerError),
wantAbsent: dummyDSN,
},
{
name: "public true",
err: dummyExposableError{msg: publicMsg, public: true},
wantContain: publicMsg,
},
{
name: "plain error",
err: errors.New(dummyDSN),
wantContain: http.StatusText(http.StatusInternalServerError),
wantAbsent: dummyDSN,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://example.com/", http.NoBody)
rec := httptest.NewRecorder()
ErrorWithCode(rec, req, http.StatusInternalServerError, tt.err)
if rec.Result().StatusCode != http.StatusInternalServerError {
t.Fatalf("expected status code %d, got %d", http.StatusInternalServerError, rec.Result().StatusCode)
}
body := rec.Body.String()
if !strings.Contains(body, tt.wantContain) {
t.Fatalf("body %q does not contain %q", body, tt.wantContain)
}
if tt.wantAbsent != "" && strings.Contains(body, tt.wantAbsent) {
t.Fatalf("body %q contains secret %q", body, tt.wantAbsent)
}
})
}
}
func TestUseRecoverer(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
handler := UseRecoverer()(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
panic("testing panic")
}))
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
if rec.Result().StatusCode != http.StatusInternalServerError {
t.Fatalf("expected status code %d, got %d", http.StatusInternalServerError, rec.Result().StatusCode)
}
}
func TestUseRecovererAbort(t *testing.T) {
defer func() {
if rvr := recover(); rvr != nil {
if e, ok := rvr.(error); ok && errors.Is(e, http.ErrAbortHandler) {
return
}
t.Fatalf("expected panic of type http.ErrAbortHandler, got %v", rvr)
} else {
t.Fatalf("expected panic of type http.ErrAbortHandler, got nil")
}
}()
req := httptest.NewRequest(http.MethodGet, "http://example.com", http.NoBody)
handler := UseRecoverer()(http.HandlerFunc(func(_ http.ResponseWriter, _ *http.Request) {
panic(http.ErrAbortHandler)
}))
handler.ServeHTTP(httptest.NewRecorder(), req)
}