mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
apiserver: refactor cors unit test
This commit is contained in:
parent
b30a6a3fc5
commit
ae7327ab8e
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package filters
|
package filters
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -25,22 +26,60 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestCORSAllowedOrigins(t *testing.T) {
|
func TestCORSAllowedOrigins(t *testing.T) {
|
||||||
table := []struct {
|
tests := []struct {
|
||||||
|
name string
|
||||||
allowedOrigins []string
|
allowedOrigins []string
|
||||||
origin string
|
origins []string
|
||||||
allowed bool
|
allowed bool
|
||||||
}{
|
}{
|
||||||
{[]string{}, "example.com", false},
|
{
|
||||||
{[]string{"example.com"}, "example.com", true},
|
name: "allowed origins list is empty",
|
||||||
{[]string{"example.com"}, "not-allowed.com", false},
|
allowedOrigins: []string{},
|
||||||
{[]string{"not-matching.com", "example.com"}, "example.com", true},
|
origins: []string{"example.com"},
|
||||||
{[]string{".*"}, "example.com", true},
|
allowed: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "origin request header not set",
|
||||||
|
allowedOrigins: []string{"example.com"},
|
||||||
|
origins: []string{""},
|
||||||
|
allowed: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "allowed regexp is a match",
|
||||||
|
allowedOrigins: []string{"example.com"},
|
||||||
|
origins: []string{"http://example.com", "example.com"},
|
||||||
|
allowed: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "allowed regexp is not a match",
|
||||||
|
allowedOrigins: []string{"example.com"},
|
||||||
|
origins: []string{"http://not-allowed.com", "not-allowed.com"},
|
||||||
|
allowed: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "allowed list with multiple regex",
|
||||||
|
allowedOrigins: []string{"not-matching.com", "example.com"},
|
||||||
|
origins: []string{"http://example.com", "example.com"},
|
||||||
|
allowed: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "wildcard matching",
|
||||||
|
allowedOrigins: []string{".*"},
|
||||||
|
origins: []string{"http://example.com", "example.com"},
|
||||||
|
allowed: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, item := range table {
|
for _, test := range tests {
|
||||||
|
for _, origin := range test.origins {
|
||||||
|
name := fmt.Sprintf("%s/origin/%s", test.name, origin)
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
var handlerInvoked int
|
||||||
handler := WithCORS(
|
handler := WithCORS(
|
||||||
http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}),
|
http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
||||||
item.allowedOrigins, nil, nil, nil, "true",
|
handlerInvoked++
|
||||||
|
}),
|
||||||
|
test.allowedOrigins, nil, nil, nil, "true",
|
||||||
)
|
)
|
||||||
var response *http.Response
|
var response *http.Response
|
||||||
func() {
|
func() {
|
||||||
@ -51,16 +90,20 @@ func TestCORSAllowedOrigins(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
request.Header.Set("Origin", item.origin)
|
request.Header.Set("Origin", origin)
|
||||||
client := http.Client{}
|
client := http.Client{}
|
||||||
response, err = client.Do(request)
|
response, err = client.Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %v", err)
|
t.Errorf("unexpected error: %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
if item.allowed {
|
if handlerInvoked != 1 {
|
||||||
if !reflect.DeepEqual(item.origin, response.Header.Get("Access-Control-Allow-Origin")) {
|
t.Errorf("Expected the handler to be invoked once, but got: %d", handlerInvoked)
|
||||||
t.Errorf("Expected %#v, Got %#v", item.origin, response.Header.Get("Access-Control-Allow-Origin"))
|
}
|
||||||
|
|
||||||
|
if test.allowed {
|
||||||
|
if !reflect.DeepEqual(origin, response.Header.Get("Access-Control-Allow-Origin")) {
|
||||||
|
t.Errorf("Expected %#v, Got %#v", origin, response.Header.Get("Access-Control-Allow-Origin"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if response.Header.Get("Access-Control-Allow-Credentials") == "" {
|
if response.Header.Get("Access-Control-Allow-Credentials") == "" {
|
||||||
@ -99,6 +142,8 @@ func TestCORSAllowedOrigins(t *testing.T) {
|
|||||||
t.Errorf("Expected Date in Access-Control-Expose-Headers header")
|
t.Errorf("Expected Date in Access-Control-Expose-Headers header")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user