2016-07-26 01:30:28 +05:30
|
|
|
package server
|
2016-10-05 20:31:35 +05:30
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2016-10-13 07:21:32 +05:30
|
|
|
|
|
|
|
"golang.org/x/net/context"
|
2016-10-05 20:31:35 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func TestHandleHealth(t *testing.T) {
|
2016-10-13 07:21:32 +05:30
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2016-10-14 06:45:20 +05:30
|
|
|
httpServer, server := newTestServer(ctx, t, nil)
|
2016-10-05 20:31:35 +05:30
|
|
|
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 14:55:16 +05:30
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|