@@ -190,6 +190,53 @@ func TestProxyController(t *testing.T) {
190190 assert .Equal (t , 401 , recorder .Code )
191191 },
192192 },
193+ {
194+ description : "Ensure forward auth with is browser false returns json" ,
195+ middlewares : []gin.HandlerFunc {},
196+ run : func (t * testing.T , router * gin.Engine , recorder * httptest.ResponseRecorder ) {
197+ req := httptest .NewRequest ("GET" , "/api/auth/traefik" , nil )
198+ req .Header .Set ("x-forwarded-host" , "test.example.com" )
199+ req .Header .Set ("x-forwarded-proto" , "https" )
200+ req .Header .Set ("x-forwarded-uri" , "/" )
201+ router .ServeHTTP (recorder , req )
202+
203+ assert .Equal (t , 401 , recorder .Code )
204+ assert .Contains (t , recorder .Body .String (), `"status":401` )
205+ assert .Contains (t , recorder .Body .String (), `"message":"Unauthorized"` )
206+ },
207+ },
208+ {
209+ description : "Ensure forward auth with caddy and browser user agent returns redirect" ,
210+ middlewares : []gin.HandlerFunc {},
211+ run : func (t * testing.T , router * gin.Engine , recorder * httptest.ResponseRecorder ) {
212+ req := httptest .NewRequest ("GET" , "/api/auth/traefik" , nil )
213+ req .Header .Set ("x-forwarded-host" , "test.example.com" )
214+ req .Header .Set ("x-forwarded-proto" , "https" )
215+ req .Header .Set ("x-forwarded-uri" , "/" )
216+ req .Header .Set ("user-agent" , browserUserAgent )
217+ router .ServeHTTP (recorder , req )
218+
219+ assert .Equal (t , 307 , recorder .Code )
220+ location := recorder .Header ().Get ("Location" )
221+ assert .Contains (t , location , "https://tinyauth.example.com/login?redirect_uri=" )
222+ assert .Contains (t , location , "https%3A%2F%2Ftest.example.com%2F" )
223+ },
224+ },
225+ {
226+ description : "Ensure forward auth with caddy and non browser user agent returns json" ,
227+ middlewares : []gin.HandlerFunc {},
228+ run : func (t * testing.T , router * gin.Engine , recorder * httptest.ResponseRecorder ) {
229+ req := httptest .NewRequest ("GET" , "/api/auth/traefik" , nil )
230+ req .Header .Set ("x-forwarded-host" , "test.example.com" )
231+ req .Header .Set ("x-forwarded-proto" , "https" )
232+ req .Header .Set ("x-forwarded-uri" , "/" )
233+ router .ServeHTTP (recorder , req )
234+
235+ assert .Equal (t , 401 , recorder .Code )
236+ assert .Contains (t , recorder .Body .String (), `"status":401` )
237+ assert .Contains (t , recorder .Body .String (), `"message":"Unauthorized"` )
238+ },
239+ },
193240 {
194241 description : "Ensure normal authentication flow for forward auth" ,
195242 middlewares : []gin.HandlerFunc {
0 commit comments