volatiletech / authboss

The boss of http auth.
MIT License
3.81k stars 207 forks source link

What is the purpose of these test methods? #304

Closed ibraheemdev closed 3 years ago

ibraheemdev commented 4 years ago

What is the purpose of these test methods in auth.go?

h.ab.Events.Before(authboss.EventAuth, func(w http.ResponseWriter, r *http.Request, handled bool) (bool, error) {
  w.WriteHeader(http.StatusTeapot)
  beforeCalled = true
  return true, nil
})

It writes a status teapot before the auth event, and then checks to see if that status changes:

if resp.Code != http.StatusTeapot {
  t.Error("should have left the response alone once teapot was sent")
}

Why would the status not change? Why is this being tested? I can see that this verifies that the correct events were being called, but what is the purpose of writing the status code? Isn't the event already being verified in the "normal" test?:

t.Run("normal", func(t *testing.T) {
    t.Parallel()
    h := setupMore(testSetup()) var beforeCalled, afterCalled bool
    var beforeHasValues, afterHasValues bool
    h.ab.Events.Before(authboss.EventAuth, func(w http.ResponseWriter, r *http.Request, handled bool) (bool, error) {
        beforeCalled = true
        beforeHasValues = r.Context().Value(authboss.CTXKeyValues) != nil
        return false, nil
})
aarondl commented 4 years ago

It's probably testing the code to prevent panics since http.ResponseWriter's normal Go HTTP Server implementation panics if you set the status twice. It wants to ensure the status isn't changing.