cors_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cors
  14. import (
  15. "net/http"
  16. "net/http/httptest"
  17. "reflect"
  18. "testing"
  19. )
  20. func TestCORSInfo(t *testing.T) {
  21. tests := []struct {
  22. s string
  23. winfo CORSInfo
  24. ws string
  25. }{
  26. {"", CORSInfo{}, ""},
  27. {"http://127.0.0.1", CORSInfo{"http://127.0.0.1": true}, "http://127.0.0.1"},
  28. {"*", CORSInfo{"*": true}, "*"},
  29. // with space around
  30. {" http://127.0.0.1 ", CORSInfo{"http://127.0.0.1": true}, "http://127.0.0.1"},
  31. // multiple addrs
  32. {
  33. "http://127.0.0.1,http://127.0.0.2",
  34. CORSInfo{"http://127.0.0.1": true, "http://127.0.0.2": true},
  35. "http://127.0.0.1,http://127.0.0.2",
  36. },
  37. }
  38. for i, tt := range tests {
  39. info := CORSInfo{}
  40. if err := info.Set(tt.s); err != nil {
  41. t.Errorf("#%d: set error = %v, want nil", i, err)
  42. }
  43. if !reflect.DeepEqual(info, tt.winfo) {
  44. t.Errorf("#%d: info = %v, want %v", i, info, tt.winfo)
  45. }
  46. if g := info.String(); g != tt.ws {
  47. t.Errorf("#%d: info string = %s, want %s", i, g, tt.ws)
  48. }
  49. }
  50. }
  51. func TestCORSInfoOriginAllowed(t *testing.T) {
  52. tests := []struct {
  53. set string
  54. origin string
  55. wallowed bool
  56. }{
  57. {"http://127.0.0.1,http://127.0.0.2", "http://127.0.0.1", true},
  58. {"http://127.0.0.1,http://127.0.0.2", "http://127.0.0.2", true},
  59. {"http://127.0.0.1,http://127.0.0.2", "*", false},
  60. {"http://127.0.0.1,http://127.0.0.2", "http://127.0.0.3", false},
  61. {"*", "*", true},
  62. {"*", "http://127.0.0.1", true},
  63. }
  64. for i, tt := range tests {
  65. info := CORSInfo{}
  66. if err := info.Set(tt.set); err != nil {
  67. t.Errorf("#%d: set error = %v, want nil", i, err)
  68. }
  69. if g := info.OriginAllowed(tt.origin); g != tt.wallowed {
  70. t.Errorf("#%d: allowed = %v, want %v", i, g, tt.wallowed)
  71. }
  72. }
  73. }
  74. func TestCORSHandler(t *testing.T) {
  75. info := &CORSInfo{}
  76. if err := info.Set("http://127.0.0.1,http://127.0.0.2"); err != nil {
  77. t.Fatalf("unexpected set error: %v", err)
  78. }
  79. h := &CORSHandler{
  80. Handler: http.NotFoundHandler(),
  81. Info: info,
  82. }
  83. header := func(origin string) http.Header {
  84. return http.Header{
  85. "Access-Control-Allow-Methods": []string{"POST, GET, OPTIONS, PUT, DELETE"},
  86. "Access-Control-Allow-Origin": []string{origin},
  87. "Access-Control-Allow-Headers": []string{"accept, content-type"},
  88. }
  89. }
  90. tests := []struct {
  91. method string
  92. origin string
  93. wcode int
  94. wheader http.Header
  95. }{
  96. {"GET", "http://127.0.0.1", http.StatusNotFound, header("http://127.0.0.1")},
  97. {"GET", "http://127.0.0.2", http.StatusNotFound, header("http://127.0.0.2")},
  98. {"GET", "http://127.0.0.3", http.StatusNotFound, http.Header{}},
  99. {"OPTIONS", "http://127.0.0.1", http.StatusOK, header("http://127.0.0.1")},
  100. }
  101. for i, tt := range tests {
  102. rr := httptest.NewRecorder()
  103. req := &http.Request{
  104. Method: tt.method,
  105. Header: http.Header{"Origin": []string{tt.origin}},
  106. }
  107. h.ServeHTTP(rr, req)
  108. if rr.Code != tt.wcode {
  109. t.Errorf("#%d: code = %v, want %v", i, rr.Code, tt.wcode)
  110. }
  111. // it is set by http package, and there is no need to test it
  112. rr.HeaderMap.Del("Content-Type")
  113. if !reflect.DeepEqual(rr.HeaderMap, tt.wheader) {
  114. t.Errorf("#%d: header = %+v, want %+v", i, rr.HeaderMap, tt.wheader)
  115. }
  116. }
  117. }