@@ -87,4 +87,38 @@ func TestWebError(t *testing.T) {
87
87
webError (w , r , config , err , http .StatusInternalServerError )
88
88
require .Equal (t , http .StatusTeapot , w .Result ().StatusCode )
89
89
})
90
+
91
+ t .Run ("Error is sent as HTML when 'Accept' header contains 'text/html'" , func (t * testing.T ) {
92
+ t .Parallel ()
93
+
94
+ w := httptest .NewRecorder ()
95
+ r := httptest .NewRequest (http .MethodGet , "/blah" , nil )
96
+ r .Header .Set ("Accept" , "something/else, text/html" )
97
+ webError (w , r , config , NewErrorStatusCodeFromStatus (http .StatusTeapot ), http .StatusInternalServerError )
98
+ require .Equal (t , http .StatusTeapot , w .Result ().StatusCode )
99
+ require .Contains (t , w .Result ().Header .Get ("Content-Type" ), "text/html" )
100
+ })
101
+
102
+ t .Run ("Error is sent as plain text when 'Accept' header does not contain 'text/html'" , func (t * testing.T ) {
103
+ t .Parallel ()
104
+
105
+ w := httptest .NewRecorder ()
106
+ r := httptest .NewRequest (http .MethodGet , "/blah" , nil )
107
+ r .Header .Set ("Accept" , "application/json" )
108
+ webError (w , r , config , NewErrorStatusCodeFromStatus (http .StatusTeapot ), http .StatusInternalServerError )
109
+ require .Equal (t , http .StatusTeapot , w .Result ().StatusCode )
110
+ require .Contains (t , w .Result ().Header .Get ("Content-Type" ), "text/plain" )
111
+ })
112
+
113
+ t .Run ("Error is sent as plain text when 'Accept' header contains 'text/html' and config.DisableHTMLErrors is true" , func (t * testing.T ) {
114
+ t .Parallel ()
115
+
116
+ config := & Config {Headers : map [string ][]string {}, DisableHTMLErrors : true }
117
+ w := httptest .NewRecorder ()
118
+ r := httptest .NewRequest (http .MethodGet , "/blah" , nil )
119
+ r .Header .Set ("Accept" , "something/else, text/html" )
120
+ webError (w , r , config , NewErrorStatusCodeFromStatus (http .StatusTeapot ), http .StatusInternalServerError )
121
+ require .Equal (t , http .StatusTeapot , w .Result ().StatusCode )
122
+ require .Contains (t , w .Result ().Header .Get ("Content-Type" ), "text/plain" )
123
+ })
90
124
}
0 commit comments