-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy patherrors_test.go
More file actions
75 lines (62 loc) · 1.97 KB
/
errors_test.go
File metadata and controls
75 lines (62 loc) · 1.97 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
package beyond
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func init() {
*errorEmail = "[email protected]"
}
type testIntString struct {
code int
text string
}
func TestErrorQuery(t *testing.T) {
for errorQuery, expectedValues := range map[string]testIntString{
"invalid_request": {400, "400 - Bad Request"},
"access_denied": {403, "403 - Forbidden"},
"invalid_resource": {404, "404 - Not Found"},
"unknown": {500, "500 - Internal Server Error"},
"server_error": {500, "500 - Internal Server Error"},
"unsupported_response_type": {501, "501 - Not Implemented"},
"temporarily_unavailable": {503, "503 - Service Unavailable"},
} {
request := httptest.NewRequest("GET", "/oidc?error="+errorQuery, nil)
request.Host = *host
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, expectedValues.code, resp.StatusCode)
assert.Contains(t, string(body), expectedValues.text)
}
}
func TestErrorPlain(t *testing.T) {
*errorPlain = true
request := httptest.NewRequest("GET", "/oidc?error=server_error&error_description=Foo+Biz", nil)
request.Host = *host
w := httptest.NewRecorder()
testMux.ServeHTTP(w, request)
resp := w.Result()
body, _ := io.ReadAll(resp.Body)
assert.Equal(t, 500, resp.StatusCode)
assert.Contains(t, string(body), "Foo Biz")
*errorPlain = false
}
type testResponseWriter struct {
http.ResponseWriter
}
func (w *testResponseWriter) WriteHeader(code int) {}
func (w *testResponseWriter) Header() http.Header { return http.Header{} }
func (w *testResponseWriter) Write(data []byte) (n int, err error) {
return 0, fmt.Errorf("WriteError")
}
func TestErrorExecuteWriteError(t *testing.T) {
w := &testResponseWriter{}
err := errorExecute(w, 500, "WriteError")
assert.Equal(t, "WriteError", err.Error())
errorHandler(w, 500, "WriteError")
}