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.
 
 
 

118 lines
3.7 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cors
  15. import (
  16. "net/http"
  17. "net/http/httptest"
  18. "testing"
  19. )
  20. func TestServeHTTPSameOrigin(t *testing.T) {
  21. var handlerRun bool
  22. h := NewHandler(http.HandlerFunc(
  23. func(rw http.ResponseWriter, req *http.Request) {
  24. handlerRun = true
  25. }))
  26. req, err := http.NewRequest("GET", "http://example.com", nil)
  27. if err != nil {
  28. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  29. }
  30. rw := httptest.NewRecorder()
  31. h.ServeHTTP(rw, req)
  32. if !handlerRun {
  33. t.Error("handlerRun: got false, want true")
  34. }
  35. }
  36. func TestServeHTTPPreflight(t *testing.T) {
  37. var handlerRun bool
  38. h := NewHandler(http.HandlerFunc(
  39. func(rw http.ResponseWriter, req *http.Request) {
  40. handlerRun = true
  41. }))
  42. h.AllowCredentials(true)
  43. req, err := http.NewRequest("OPTIONS", "http://example.com", nil)
  44. if err != nil {
  45. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  46. }
  47. req.Header.Set("Origin", "http://google.com")
  48. req.Header.Set("Access-Control-Request-Method", "PUT")
  49. req.Header.Set("Access-Control-Request-Headers", "Cors-Test")
  50. rw := httptest.NewRecorder()
  51. h.ServeHTTP(rw, req)
  52. if got, want := rw.Header().Get("Access-Control-Allow-Origin"), "*"; got != want {
  53. t.Errorf("rw.Header().Get(%q): got %q, want %q", "Access-Control-Allow-Origin", got, want)
  54. }
  55. if got, want := rw.Header().Get("Access-Control-Allow-Methods"), "PUT"; got != want {
  56. t.Errorf("rw.Header().Get(%q): got %q, want %q", "Access-Control-Allow-Methods", got, want)
  57. }
  58. if got, want := rw.Header().Get("Access-Control-Allow-Headers"), "Cors-Test"; got != want {
  59. t.Errorf("rw.Header().Get(%q): got %q, want %q", "Access-Control-Allow-Headers", got, want)
  60. }
  61. if got, want := rw.Header().Get("Access-Control-Allow-Credentials"), "true"; got != want {
  62. t.Errorf("rw.Header().Get(%q): got %q, want %q", "Access-Control-Allow-Credentials", got, want)
  63. }
  64. if handlerRun {
  65. t.Error("handlerRun: got true, want false")
  66. }
  67. }
  68. func TestServeHTTPSimple(t *testing.T) {
  69. var handlerRun bool
  70. h := NewHandler(http.HandlerFunc(
  71. func(rw http.ResponseWriter, req *http.Request) {
  72. handlerRun = true
  73. }))
  74. h.SetOrigin("http://martian.local")
  75. req, err := http.NewRequest("GET", "http://example.com", nil)
  76. if err != nil {
  77. t.Fatalf("http.NewRequest(): got %v, want no error", err)
  78. }
  79. req.Header.Set("Origin", "http://google.com")
  80. req.Header.Set("Access-Control-Request-Method", "GET")
  81. req.Header.Set("Access-Control-Request-Headers", "Cors-Test")
  82. rw := httptest.NewRecorder()
  83. h.ServeHTTP(rw, req)
  84. if got, want := rw.Header().Get("Access-Control-Allow-Origin"), "http://martian.local"; got != want {
  85. t.Errorf("rw.Header().Get(%q): got %q, want %q", "Access-Control-Allow-Origin", got, want)
  86. }
  87. if got, want := rw.Header().Get("Access-Control-Allow-Methods"), "GET"; got != want {
  88. t.Errorf("rw.Header().Get(%q): got %q, want %q", "Access-Control-Allow-Methods", got, want)
  89. }
  90. if got, want := rw.Header().Get("Access-Control-Allow-Headers"), "Cors-Test"; got != want {
  91. t.Errorf("rw.Header().Get(%q): got %q, want %q", "Access-Control-Allow-Headers", got, want)
  92. }
  93. if !handlerRun {
  94. t.Error("handlerRun: got false, want true")
  95. }
  96. }