|
| 1 | +// Copyright 2019 HAProxy Technologies LLC |
| 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 | + |
| 15 | +//go:build e2e_sequential |
| 16 | + |
| 17 | +package cors |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/http" |
| 21 | + |
| 22 | + "github.com/haproxytech/kubernetes-ingress/deploy/tests/e2e" |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + AccessControlAllowOrigin = "Access-Control-Allow-Origin" |
| 27 | + AccessControlAllowMethods = "Access-Control-Allow-Methods" |
| 28 | + AccessControlAllowHeaders = "Access-Control-Allow-Headers" |
| 29 | + AccessControlMaxAge = "Access-Control-Max-Age" |
| 30 | + AccessControlAllowCredential = "Access-Control-Allow-Credentials" |
| 31 | + AnnotationCorsEnable = "cors-enable" |
| 32 | + AnnotationCorsOrigin = "cors-allow-origin" |
| 33 | + AnnotationCorsMethods = "cors-allow-methods" |
| 34 | + AnnotationCorsHeaders = "cors-allow-headers" |
| 35 | + AnnotationCorsAge = "cors-max-age" |
| 36 | + AnnotationCorsCredential = "cors-allow-credentials" |
| 37 | + Star = "*" |
| 38 | +) |
| 39 | + |
| 40 | +func (suite *CorsSuite) Test_Ingress_Alone() { |
| 41 | + suite.Run("Default", func() { |
| 42 | + expectedHeaders := http.Header{ |
| 43 | + AccessControlAllowOrigin: {Star}, |
| 44 | + AccessControlAllowMethods: {Star}, |
| 45 | + AccessControlAllowHeaders: {Star}, |
| 46 | + AccessControlMaxAge: {"5"}, |
| 47 | + } |
| 48 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 49 | + {AnnotationCorsEnable, q("true")}, |
| 50 | + } |
| 51 | + |
| 52 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 53 | + |
| 54 | + suite.eventuallyReturns(expectedHeaders, http.Header{}) |
| 55 | + }) |
| 56 | + |
| 57 | + suite.Run("CorsOriginAlone", func() { |
| 58 | + expectedHeaders := http.Header{ |
| 59 | + AccessControlAllowOrigin: {"http://" + suite.tmplData.Host}, |
| 60 | + AccessControlAllowMethods: {Star}, |
| 61 | + AccessControlAllowHeaders: {Star}, |
| 62 | + AccessControlMaxAge: {"5"}, |
| 63 | + } |
| 64 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 65 | + {AnnotationCorsEnable, q("true")}, |
| 66 | + {AnnotationCorsOrigin, q("http://" + suite.tmplData.Host)}, |
| 67 | + } |
| 68 | + |
| 69 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 70 | + |
| 71 | + suite.eventuallyReturns(expectedHeaders, http.Header{}) |
| 72 | + }) |
| 73 | + |
| 74 | + suite.Run("CorsMethodsAlone", func() { |
| 75 | + expectedHeaders := http.Header{ |
| 76 | + AccessControlAllowOrigin: {Star}, |
| 77 | + AccessControlAllowMethods: {"GET"}, |
| 78 | + AccessControlAllowHeaders: {Star}, |
| 79 | + AccessControlMaxAge: {"5"}, |
| 80 | + } |
| 81 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 82 | + {AnnotationCorsEnable, q("true")}, |
| 83 | + {AnnotationCorsMethods, q("GET")}, |
| 84 | + } |
| 85 | + |
| 86 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 87 | + |
| 88 | + suite.eventuallyReturns(expectedHeaders, http.Header{}) |
| 89 | + }) |
| 90 | + |
| 91 | + suite.Run("CorsMethodsHeaders", func() { |
| 92 | + expectedHeaders := http.Header{ |
| 93 | + AccessControlAllowOrigin: {Star}, |
| 94 | + AccessControlAllowMethods: {Star}, |
| 95 | + AccessControlAllowHeaders: {"Accept"}, |
| 96 | + AccessControlMaxAge: {"5"}, |
| 97 | + } |
| 98 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 99 | + {AnnotationCorsEnable, q("true")}, |
| 100 | + {AnnotationCorsHeaders, q("Accept")}, |
| 101 | + } |
| 102 | + |
| 103 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 104 | + |
| 105 | + suite.eventuallyReturns(expectedHeaders, http.Header{}) |
| 106 | + }) |
| 107 | + |
| 108 | + suite.Run("CorsMethodsAge", func() { |
| 109 | + expectedHeaders := http.Header{ |
| 110 | + AccessControlAllowOrigin: {Star}, |
| 111 | + AccessControlAllowMethods: {Star}, |
| 112 | + AccessControlAllowHeaders: {Star}, |
| 113 | + AccessControlMaxAge: {"500"}, |
| 114 | + } |
| 115 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 116 | + {AnnotationCorsEnable, q("true")}, |
| 117 | + {AnnotationCorsAge, q("500s")}, |
| 118 | + } |
| 119 | + |
| 120 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 121 | + |
| 122 | + suite.eventuallyReturns(expectedHeaders, http.Header{}) |
| 123 | + }) |
| 124 | + |
| 125 | + suite.Run("CorsMethodsCredential", func() { |
| 126 | + expectedHeaders := http.Header{ |
| 127 | + AccessControlAllowOrigin: {Star}, |
| 128 | + AccessControlAllowMethods: {Star}, |
| 129 | + AccessControlAllowHeaders: {Star}, |
| 130 | + AccessControlAllowCredential: {"true"}, |
| 131 | + AccessControlMaxAge: {"5"}, |
| 132 | + } |
| 133 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 134 | + {AnnotationCorsEnable, q("true")}, |
| 135 | + {AnnotationCorsCredential, q("true")}, |
| 136 | + } |
| 137 | + |
| 138 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 139 | + |
| 140 | + suite.eventuallyReturns(expectedHeaders, http.Header{}) |
| 141 | + }) |
| 142 | + |
| 143 | + suite.Run("CorsDisable", func() { |
| 144 | + unexpectedHeaders := http.Header{ |
| 145 | + AccessControlAllowOrigin: {}, |
| 146 | + AccessControlAllowMethods: {}, |
| 147 | + AccessControlAllowHeaders: {}, |
| 148 | + AccessControlMaxAge: {}, |
| 149 | + AccessControlAllowCredential: {}, |
| 150 | + } |
| 151 | + |
| 152 | + suite.tmplData.IngAnnotations = []struct{ Key, Value string }{ |
| 153 | + {AnnotationCorsEnable, q("false")}, |
| 154 | + {AnnotationCorsOrigin, q("http://wrong.com")}, |
| 155 | + {AnnotationCorsCredential, q("true")}, |
| 156 | + {AnnotationCorsMethods, q("GET")}, |
| 157 | + {AnnotationCorsHeaders, q("Accept")}, |
| 158 | + } |
| 159 | + |
| 160 | + suite.NoError(suite.test.Apply("config/deploy.yaml.tmpl", suite.test.GetNS(), suite.tmplData)) |
| 161 | + |
| 162 | + suite.eventuallyReturns(http.Header{}, unexpectedHeaders) |
| 163 | + }) |
| 164 | +} |
| 165 | + |
| 166 | +func (suite *CorsSuite) eventuallyReturns(expecedHeaders, unexpectedHeaders http.Header) { |
| 167 | + suite.Eventually(func() bool { |
| 168 | + res, cls, err := suite.client.Do() |
| 169 | + if err != nil { |
| 170 | + suite.T().Logf("Connection ERROR: %s", err.Error()) |
| 171 | + return false |
| 172 | + } |
| 173 | + defer cls() |
| 174 | + for expectedHeader, expectedValues := range expecedHeaders { |
| 175 | + values, ok := res.Header[expectedHeader] |
| 176 | + if !ok || len(values) != 1 || values[0] != expectedValues[0] { |
| 177 | + return false |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | + for unexpectedHeader := range unexpectedHeaders { |
| 182 | + if _, ok := res.Header[unexpectedHeader]; ok { |
| 183 | + return false |
| 184 | + } |
| 185 | + } |
| 186 | + return true |
| 187 | + }, e2e.WaitDuration, e2e.TickDuration) |
| 188 | +} |
| 189 | + |
| 190 | +func q(value string) string { |
| 191 | + return "\"" + value + "\"" |
| 192 | +} |
0 commit comments