-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_test.go
More file actions
72 lines (67 loc) · 1.92 KB
/
error_test.go
File metadata and controls
72 lines (67 loc) · 1.92 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
package mux
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
)
func TestAbort(t *testing.T) {
var tests = map[int]error{
http.StatusBadRequest: ErrDecodeRequestData, // 400
http.StatusNotFound: ErrNotFound, // 404
http.StatusNotAcceptable: ErrEncodeMatch, // 406
http.StatusUnsupportedMediaType: ErrDecodeContentType, // 415
http.StatusInternalServerError: errors.New("test"), // 500
}
h := New(WithLogger(testLogger))
for want, err := range tests {
w := httptest.NewRecorder()
req := newTestRequest(http.MethodGet, "/", nil)
h.Abort(w, req, err)
resp := w.Result()
resp.Body.Close()
assertStatus(t, resp, want)
}
}
func TestAbortErrRedirect(t *testing.T) {
h := New()
w := httptest.NewRecorder()
req := newTestRequest(http.MethodGet, "/", nil)
h.Abort(w, req, h.Redirect("/test", http.StatusFound))
resp := w.Result()
resp.Body.Close()
assertStatus(t, resp, http.StatusFound)
assertHeader(t, resp, "Location", "/test")
}
func TestAbortErrMethodNotAllowed(t *testing.T) {
h := New()
w := httptest.NewRecorder()
req := newTestRequest(http.MethodPost, "/", nil)
h.Abort(w, req, ErrMethodNotAllowed([]string{"DELETE", "GET", "HEAD", "OPTIONS"}))
resp := w.Result()
resp.Body.Close()
assertStatus(t, resp, http.StatusMethodNotAllowed)
assertHeader(t, resp, "Allow", "DELETE, GET, HEAD, OPTIONS")
}
func TestAbortValidationError(t *testing.T) {
h := New()
w := httptest.NewRecorder()
body := bytes.NewBuffer(make([]byte, 0))
err := json.NewEncoder(body).Encode(testData{N: 0})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
req := newTestRequest(http.MethodGet, "/", body)
var form testData
err = h.Decode(req, &form)
_, ok := err.(ValidationError)
if !ok {
t.Fatalf("unexpected error: %v", err)
}
h.Abort(w, req, err)
resp := w.Result()
resp.Body.Close()
assertStatus(t, resp, http.StatusUnprocessableEntity)
}