handlers_test.go 809 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package rest
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func TestCorsHandlerWithOrigins(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. origins []string
  12. expect string
  13. }{
  14. {
  15. name: "allow all origins",
  16. expect: allOrigins,
  17. },
  18. {
  19. name: "allow one origin",
  20. origins: []string{"local"},
  21. expect: "local",
  22. },
  23. {
  24. name: "allow many origins",
  25. origins: []string{"local", "remote"},
  26. expect: "local",
  27. },
  28. }
  29. for _, test := range tests {
  30. t.Run(test.name, func(t *testing.T) {
  31. w := httptest.NewRecorder()
  32. handler := CorsHandler(test.origins...)
  33. handler.ServeHTTP(w, nil)
  34. assert.Equal(t, http.StatusNoContent, w.Result().StatusCode)
  35. assert.Equal(t, test.expect, w.Header().Get(allowOrigin))
  36. })
  37. }
  38. }