@@ -3,14 +3,15 @@ package router
3
3
import (
4
4
"errors"
5
5
"fmt"
6
- "github.com/netlify/netlify-commons/tracing"
7
- "github.com/sirupsen/logrus/hooks/test"
8
- "github.com/stretchr/testify/assert"
9
- "github.com/stretchr/testify/require"
10
6
"io/ioutil"
11
7
"net/http"
12
8
"net/http/httptest"
13
9
"testing"
10
+
11
+ "github.com/netlify/netlify-commons/tracing"
12
+ "github.com/sirupsen/logrus/hooks/test"
13
+ "github.com/stretchr/testify/assert"
14
+ "github.com/stretchr/testify/require"
14
15
)
15
16
16
17
func TestHandleError_ErrorIsNil (t * testing.T ) {
@@ -28,6 +29,25 @@ func TestHandleError_ErrorIsNil(t *testing.T) {
28
29
assert .Empty (t , w .Header ())
29
30
}
30
31
32
+ func TestHandleError_ErrorIsNilPointerToTypeHTTPError (t * testing.T ) {
33
+ logger , loggerOutput := test .NewNullLogger ()
34
+ w , r , _ := tracing .NewTracer (
35
+ httptest .NewRecorder (),
36
+ httptest .NewRequest (http .MethodGet , "/" , nil ),
37
+ logger ,
38
+ "test" ,
39
+ )
40
+
41
+ h := func (_ http.ResponseWriter , _ * http.Request ) * HTTPError {
42
+ return nil
43
+ }
44
+
45
+ HandleError (h (w , r ), w , r )
46
+
47
+ assert .Empty (t , loggerOutput .AllEntries ())
48
+ assert .Empty (t , w .Header ())
49
+ }
50
+
31
51
func TestHandleError_StandardError (t * testing.T ) {
32
52
logger , loggerOutput := test .NewNullLogger ()
33
53
w , r , _ := tracing .NewTracer (
@@ -39,7 +59,7 @@ func TestHandleError_StandardError(t *testing.T) {
39
59
40
60
HandleError (errors .New ("random error" ), w , r )
41
61
42
- require .Len (t , loggerOutput .AllEntries (), 1 )
62
+ require .Len (t , loggerOutput .AllEntries (), 1 )
43
63
assert .Equal (t , "Unhandled server error: random error" , loggerOutput .AllEntries ()[0 ].Message )
44
64
assert .Empty (t , w .Header ())
45
65
}
@@ -55,9 +75,9 @@ func TestHandleError_HTTPError(t *testing.T) {
55
75
)
56
76
57
77
httpErr := & HTTPError {
58
- Code : http .StatusInternalServerError ,
59
- Message : http .StatusText (http .StatusInternalServerError ),
60
- InternalError : errors .New ("random error" ),
78
+ Code : http .StatusInternalServerError ,
79
+ Message : http .StatusText (http .StatusInternalServerError ),
80
+ InternalError : errors .New ("random error" ),
61
81
InternalMessage : "Something unexpected happened" ,
62
82
}
63
83
@@ -70,7 +90,6 @@ func TestHandleError_HTTPError(t *testing.T) {
70
90
expectedBody := fmt .Sprintf (`{"code":500,"msg":"Internal Server Error","json":null,"error_id":"%s"}` , tracing .RequestID (r ))
71
91
assert .Equal (t , expectedBody , string (b ))
72
92
73
- require .Len (t , loggerOutput .AllEntries (), 1 )
93
+ require .Len (t , loggerOutput .AllEntries (), 1 )
74
94
assert .Equal (t , httpErr .InternalMessage , loggerOutput .AllEntries ()[0 ].Message )
75
95
}
76
-
0 commit comments