Skip to content

Commit

Permalink
Add ProcessCookie() test
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Bland committed May 8, 2015
1 parent f554f99 commit 5cbdb74
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions oauthproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,56 @@ func TestSignInPageDirectAccessRedirectsToRoot(t *testing.T) {
t.Fatal(`expected redirect to "/", but was "` + match[1] + `"`)
}
}

type ProcessCookieTest struct {
opts *Options
proxy *OauthProxy
rw *httptest.ResponseRecorder
req *http.Request
}

func NewProcessCookieTest() *ProcessCookieTest {
var pc_test ProcessCookieTest

pc_test.opts = NewOptions()
pc_test.opts.Upstreams = append(pc_test.opts.Upstreams, "unused")
pc_test.opts.CookieSecret = "foobar"
pc_test.opts.ClientID = "bazquux"
pc_test.opts.ClientSecret = "xyzzyplugh"
pc_test.opts.Validate()

pc_test.proxy = NewOauthProxy(pc_test.opts, func(email string) bool {
return true
})

pc_test.rw = httptest.NewRecorder()
pc_test.req, _ = http.NewRequest("GET", "/", strings.NewReader(""))
return &pc_test
}

func (p *ProcessCookieTest) MakeCookie(value string) *http.Cookie {
return p.proxy.MakeCookie(p.req, value, p.opts.CookieExpire)
}

func (p *ProcessCookieTest) AddCookie(value string) {
p.req.AddCookie(p.MakeCookie(value))
}

func (p *ProcessCookieTest) ProcessCookie() (email, user, access_token string, ok bool) {
return p.proxy.ProcessCookie(p.rw, p.req)
}

func TestProcessCookie(t *testing.T) {
pc_test := NewProcessCookieTest()
pc_test.AddCookie("[email protected]")
email, user, _, ok := pc_test.ProcessCookie()
assert.Equal(t, true, ok)
assert.Equal(t, "[email protected]", email)
assert.Equal(t, "michael.bland", user)
}

func TestProcessCookieError(t *testing.T) {
pc_test := NewProcessCookieTest()
_, _, _, ok := pc_test.ProcessCookie()
assert.Equal(t, false, ok)
}

0 comments on commit 5cbdb74

Please sign in to comment.