Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the purpose of these test methods? #304

Closed
ibraheemdev opened this issue Aug 12, 2020 · 1 comment
Closed

What is the purpose of these test methods? #304

ibraheemdev opened this issue Aug 12, 2020 · 1 comment

Comments

@ibraheemdev
Copy link
Contributor

ibraheemdev commented Aug 12, 2020

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
Copy link
Member

aarondl commented Aug 26, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants