cors_test.go 1010 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package gold
  2. import (
  3. "net/http"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestCORSRequestHasOrigin(t *testing.T) {
  8. requestOrigin := "https://example.com"
  9. url := testServer.URL + "/_test/user1"
  10. req, err := http.NewRequest("GET", url, nil)
  11. assert.NoError(t, err)
  12. req.Header.Set("Origin", requestOrigin)
  13. resp, err := httpClient.Do(req)
  14. assert.NoError(t, err)
  15. assert.Equal(t, requestOrigin, resp.Header.Get("Access-Control-Allow-Origin"))
  16. }
  17. func TestCORSRequestHasNoOrigin(t *testing.T) {
  18. url := testServer.URL + "/_test/user1"
  19. req, err := http.NewRequest("GET", url, nil)
  20. assert.NoError(t, err)
  21. resp, err := httpClient.Do(req)
  22. assert.NoError(t, err)
  23. assert.Equal(t, "*", resp.Header.Get("Access-Control-Allow-Origin"))
  24. }
  25. func TestVaryHeader(t *testing.T) {
  26. url := testServer.URL + "/_test/user1"
  27. req, err := http.NewRequest("GET", url, nil)
  28. assert.NoError(t, err)
  29. resp, err := httpClient.Do(req)
  30. assert.NoError(t, err)
  31. assert.Equal(t, "Origin", resp.Header.Get("Vary"))
  32. }