Skip to content

Commit

Permalink
Add WithBearerAuth (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ikawaha authored Jul 5, 2021
1 parent ea06331 commit 548c039
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions tester_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down

0 comments on commit 548c039

Please sign in to comment.