From 548c0398183dafbcbaa69b53e7bfb8437c032968 Mon Sep 17 00:00:00 2001 From: ikawaha Date: Mon, 5 Jul 2021 23:50:45 +0900 Subject: [PATCH] Add WithBearerAuth (#15) --- tester.go | 9 +++++++-- tester_test.go | 9 +++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) 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").