2016-07-25 20:00:28 +00:00
|
|
|
package server
|
2016-10-05 15:01:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2016-10-13 01:51:32 +00:00
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2016-10-05 15:01:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleHealth(t *testing.T) {
|
2016-10-13 01:51:32 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2016-10-14 01:15:20 +00:00
|
|
|
httpServer, server := newTestServer(ctx, t, nil)
|
2016-10-05 15:01:35 +00:00
|
|
|
defer httpServer.Close()
|
|
|
|
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
server.handleHealth(rr, httptest.NewRequest("GET", "/healthz", nil))
|
|
|
|
if rr.Code != http.StatusOK {
|
|
|
|
t.Errorf("expected 200 got %d", rr.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-12-29 09:25:16 +00:00
|
|
|
|
|
|
|
var discoveryHandlerCORSTests = []struct {
|
|
|
|
DiscoveryAllowedOrigins []string
|
|
|
|
Origin string
|
|
|
|
ResponseAllowOrigin string //The expected response: same as Origin in case of valid CORS flow
|
|
|
|
}{
|
|
|
|
{nil, "http://foo.example", ""}, //Default behavior: cross origin requests not allowed
|
|
|
|
{[]string{}, "http://foo.example", ""},
|
|
|
|
{[]string{"http://foo.example"}, "http://foo.example", "http://foo.example"},
|
|
|
|
{[]string{"http://bar.example", "http://foo.example"}, "http://foo.example", "http://foo.example"},
|
|
|
|
{[]string{"*"}, "http://foo.example", "http://foo.example"},
|
|
|
|
{[]string{"http://bar.example"}, "http://foo.example", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDiscoveryHandlerCORS(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
for _, testcase := range discoveryHandlerCORSTests {
|
|
|
|
|
|
|
|
httpServer, server := newTestServer(ctx, t, func(c *Config) {
|
|
|
|
c.DiscoveryAllowedOrigins = testcase.DiscoveryAllowedOrigins
|
|
|
|
})
|
|
|
|
defer httpServer.Close()
|
|
|
|
|
|
|
|
discoveryHandler, err := server.discoveryHandler()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to get discovery handler: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Perform preflight request
|
|
|
|
rrPreflight := httptest.NewRecorder()
|
|
|
|
reqPreflight := httptest.NewRequest("OPTIONS", "/.well-kown/openid-configuration", nil)
|
|
|
|
reqPreflight.Header.Set("Origin", testcase.Origin)
|
|
|
|
reqPreflight.Header.Set("Access-Control-Request-Method", "GET")
|
|
|
|
discoveryHandler.ServeHTTP(rrPreflight, reqPreflight)
|
|
|
|
if rrPreflight.Code != http.StatusOK {
|
|
|
|
t.Errorf("expected 200 got %d", rrPreflight.Code)
|
|
|
|
}
|
|
|
|
headerAccessControlPreflight := rrPreflight.HeaderMap.Get("Access-Control-Allow-Origin")
|
|
|
|
if headerAccessControlPreflight != testcase.ResponseAllowOrigin {
|
|
|
|
t.Errorf("expected '%s' got '%s'", testcase.ResponseAllowOrigin, headerAccessControlPreflight)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Perform request
|
|
|
|
rr := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/.well-kown/openid-configuration", nil)
|
|
|
|
req.Header.Set("Origin", testcase.Origin)
|
|
|
|
discoveryHandler.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusOK {
|
|
|
|
t.Errorf("expected 200 got %d", rr.Code)
|
|
|
|
}
|
|
|
|
headerAccessControl := rr.HeaderMap.Get("Access-Control-Allow-Origin")
|
|
|
|
if headerAccessControl != testcase.ResponseAllowOrigin {
|
|
|
|
t.Errorf("expected '%s' got '%s'", testcase.ResponseAllowOrigin, headerAccessControl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|