diff --git a/tester.go b/tester.go index f9531a1..9ca4b57 100644 --- a/tester.go +++ b/tester.go @@ -107,9 +107,9 @@ func (tt *Tester) HasHeaders(headers map[string]string) *Tester { return tt } -// BasicAuth /////////////////////////////////////////////////////// +// authorization headers /////////////////////////////////////////////////////// -// WithBasicAuth aliases for the basic auth request header. +// WithBasicAuth is an alias to set basic auth in the request header. func (tt *Tester) WithBasicAuth(user, pass string) *Tester { var b bytes.Buffer b.WriteString(user) @@ -118,6 +118,11 @@ func (tt *Tester) WithBasicAuth(user, pass string) *Tester { return tt.WithHeader("Authorization", "Basic "+base64.StdEncoding.EncodeToString(b.Bytes())) } +// WithBearerAuth is an alias to set bearer auth in the request header. +func (tt *Tester) WithBearerAuth(token string) *Tester { + return tt.WithHeader("Authorization", "Bearer: "+token) +} + // cookies /////////////////////////////////////////////////////// // HasCookie puts cookie on the request. diff --git a/tester_test.go b/tester_test.go index 6930b00..68d93fe 100644 --- a/tester_test.go +++ b/tester_test.go @@ -160,6 +160,15 @@ func TestWithBasicAuth(t *testing.T) { assert.Equal(t, checker.request.Header.Get("Authorization"), "Basic "+h) } +func TestWithBearerAuth(t *testing.T) { + checker := makeTestChecker() + checker.Test(t, "GET", "/some"). + WithBearerAuth("token") + + v := checker.request.Header.Get("Authorization") + assert.Equal(t, "Bearer: token", v) +} + func TestWithCookie(t *testing.T) { checker := makeTestChecker() checker.Test(t, "GET", "/some").