@@ -2,117 +2,36 @@ package middleware
22
33import (
44 "errors"
5- "net"
65 "net/http"
76 "net/http/httptest"
87 "testing"
98
109 "context"
1110
1211 "github.com/remind101/pkg/httpx"
13- "github.com/remind101/pkg/reporter"
1412)
1513
16- type tmpError string
17-
18- func (te tmpError ) Error () string {
19- return string (te )
20- }
21-
22- func (te tmpError ) Temporary () bool {
23- return true
24- }
25-
26- type statusCodeError struct {
27- Err error
28- statusCode int
29- }
30-
31- func (s statusCodeError ) Error () string {
32- return s .Err .Error ()
33- }
34-
35- func (s statusCodeError ) StatusCode () int {
36- return s .statusCode
37- }
38-
39- func TestErrorMiddleware (t * testing.T ) {
40- tests := []struct {
41- Error error
42- Body string
43- Code int
44- ErrorHandler ErrorHandlerFunc
45- }{
46- {
47- Error : errors .New ("boom" ),
48- Body : "boom\n " ,
49- Code : 500 ,
50- },
51- {
52- Error : tmpError ("service unavailable" ),
53- Body : "service unavailable\n " ,
54- Code : 503 ,
55- },
56- {
57- Error : & net.DNSError {Err : "no such host" , IsTimeout : true },
58- Body : "lookup : no such host\n " ,
59- Code : 503 ,
60- },
61- {
62- Error : statusCodeError {Err : errors .New ("invalid request" ), statusCode : 400 },
63- Body : "invalid request\n " ,
64- Code : 400 ,
65- },
66- {
67- Error : errors .New ("boom" ),
68- Body : "{\" error\" :\" boom\" }\n " ,
69- Code : 500 ,
70- ErrorHandler : JSONReportingErrorHandler ,
71- },
72- }
73-
74- for _ , tt := range tests {
75- h := & Error {
76- handler : httpx .HandlerFunc (func (ctx context.Context , w http.ResponseWriter , r * http.Request ) error {
77- return tt .Error
78- }),
79- ErrorHandler : tt .ErrorHandler ,
80- }
81- req , _ := http .NewRequest ("GET" , "/" , nil )
82- resp := httptest .NewRecorder ()
83- ctx := reporter .WithReporter (context .Background (), reporter .NewLogReporter ())
84- err := h .ServeHTTPContext (ctx , resp , req )
85- if err != tt .Error {
86- t .Fatal ("Expected error to be returned" )
87- }
88-
89- if got , want := resp .Body .String (), tt .Body ; got != want {
90- t .Fatalf ("Body => %#v; want %#v" , got , want )
91- }
92-
93- if got , want := resp .Code , tt .Code ; got != want {
94- t .Fatalf ("Status => %v; want %v" , got , want )
95- }
96- }
97- }
98-
9914func TestErrorWithHandler (t * testing.T ) {
10015 var called bool
16+ boomErr := errors .New ("boom" )
10117
10218 h := & Error {
10319 ErrorHandler : func (ctx context.Context , err error , w http.ResponseWriter , r * http.Request ) {
10420 called = true
10521 },
10622 handler : httpx .HandlerFunc (func (ctx context.Context , w http.ResponseWriter , r * http.Request ) error {
107- return errors . New ( "boom" )
23+ return boomErr
10824 }),
10925 }
11026
11127 ctx := context .Background ()
11228 req , _ := http .NewRequest ("GET" , "/path" , nil )
11329 resp := httptest .NewRecorder ()
11430
115- h .ServeHTTPContext (ctx , resp , req )
31+ err := h .ServeHTTPContext (ctx , resp , req )
32+ if err != boomErr {
33+ t .Fatal ("Expected error to be returned" )
34+ }
11635
11736 if ! called {
11837 t .Fatal ("Expected the error handler to be called" )
0 commit comments