Micro-service for file storage and processing written in Go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

  1. package auth
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestAuthorizationMiddleware(t *testing.T) {
  9. t.Run("AuthorizationMiddleware returns 403 response when authorization header is incorrect", func(t *testing.T) {
  10. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  11. w.WriteHeader(http.StatusOK)
  12. })
  13. middleware := AuthenticationMiddleware{
  14. Secret: "foo",
  15. }
  16. middlewareHandler := middleware.Middleware(handler)
  17. request, _ := http.NewRequest("GET", "/", nil)
  18. responseRecorder := httptest.NewRecorder()
  19. middlewareHandler.ServeHTTP(responseRecorder, request)
  20. assert.Equal(t, responseRecorder.Code, 403)
  21. })
  22. t.Run("AuthorizationMiddleware continues when authorization header is correct", func(t *testing.T) {
  23. handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  24. w.WriteHeader(http.StatusOK)
  25. })
  26. middleware := AuthenticationMiddleware{
  27. Secret: "foo",
  28. }
  29. middlewareHandler := middleware.Middleware(handler)
  30. request, _ := http.NewRequest("GET", "/", nil)
  31. request.Header.Set("Authorization", "foo")
  32. responseRecorder := httptest.NewRecorder()
  33. middlewareHandler.ServeHTTP(responseRecorder, request)
  34. assert.Equal(t, responseRecorder.Code, 200)
  35. })
  36. }