11 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
12 "github.com/stretchr/testify/assert"
15func updateURL(url *url.URL, hostname string) {
20type ValidateSessionStateTestProvider struct {
24var _ Provider = (*ValidateSessionStateTestProvider)(nil)
26func (tp *ValidateSessionStateTestProvider) GetEmailAddress(ctx context.Context, s *sessions.SessionState) (string, error) {
27 return "", errors.New("not implemented")
30// Note that we're testing the internal validateToken() used to implement
31// several Provider's ValidateSessionState() implementations
32func (tp *ValidateSessionStateTestProvider) ValidateSessionState(ctx context.Context, s *sessions.SessionState) bool {
36type ValidateSessionStateTest struct {
37 backend *httptest.Server
39 provider *ValidateSessionStateTestProvider
43func NewValidateSessionStateTest() *ValidateSessionStateTest {
44 var vtTest ValidateSessionStateTest
46 vtTest.backend = httptest.NewServer(
47 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
48 if r.URL.Path != "/oauth/tokeninfo" {
50 w.Write([]byte("unknown URL"))
52 tokenParam := r.FormValue("access_token")
55 receivedHeaders := r.Header
56 for k := range vtTest.header {
57 received := receivedHeaders.Get(k)
58 expected := vtTest.header.Get(k)
59 if received == "" || received != expected {
65 w.Write([]byte("no token param and missing or incorrect headers"))
68 w.WriteHeader(vtTest.responseCode)
69 w.Write([]byte("only code matters; contents disregarded"))
72 backendURL, _ := url.Parse(vtTest.backend.URL)
73 vtTest.provider = &ValidateSessionStateTestProvider{
74 ProviderData: &ProviderData{
75 ValidateURL: &url.URL{
77 Host: backendURL.Host,
78 Path: "/oauth/tokeninfo",
82 vtTest.responseCode = 200
86func (vtTest *ValidateSessionStateTest) Close() {
87 vtTest.backend.Close()
90func TestValidateSessionStateValidToken(t *testing.T) {
91 vtTest := NewValidateSessionStateTest()
93 assert.Equal(t, true, validateToken(context.Background(), vtTest.provider, "foobar", nil))
96func TestValidateSessionStateValidTokenWithHeaders(t *testing.T) {
97 vtTest := NewValidateSessionStateTest()
99 vtTest.header = make(http.Header)
100 vtTest.header.Set("Authorization", "Bearer foobar")
101 assert.Equal(t, true,
102 validateToken(context.Background(), vtTest.provider, "foobar", vtTest.header))
105func TestValidateSessionStateEmptyToken(t *testing.T) {
106 vtTest := NewValidateSessionStateTest()
108 assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "", nil))
111func TestValidateSessionStateEmptyValidateURL(t *testing.T) {
112 vtTest := NewValidateSessionStateTest()
114 vtTest.provider.Data().ValidateURL = nil
115 assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
118func TestValidateSessionStateRequestNetworkFailure(t *testing.T) {
119 vtTest := NewValidateSessionStateTest()
120 // Close immediately to simulate a network failure
122 assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
125func TestValidateSessionStateExpiredToken(t *testing.T) {
126 vtTest := NewValidateSessionStateTest()
128 vtTest.responseCode = 401
129 assert.Equal(t, false, validateToken(context.Background(), vtTest.provider, "foobar", nil))
132func TestStripTokenNotPresent(t *testing.T) {
133 test := "http://local.test/api/test?a=1&b=2"
134 assert.Equal(t, test, stripToken(test))
137func TestStripToken(t *testing.T) {
138 test := "http://local.test/api/test?access_token=deadbeef&b=1&c=2"
139 expected := "http://local.test/api/test?access_token=dead...&b=1&c=2"
140 assert.Equal(t, expected, stripToken(test))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list