Skip to content

Commit

Permalink
[req] Add Bearer Token property to 'Request' struct
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Feb 27, 2024
1 parent 3afb72c commit d6c7091
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 12.101.0

* `[req]` Added Bearer Token property to `Request` struct

### 12.100.0

* `[log]` Added `NilLogger`
Expand Down
2 changes: 1 addition & 1 deletion ek.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// ////////////////////////////////////////////////////////////////////////////////// //

// VERSION is current ek package version
const VERSION = "12.100.0"
const VERSION = "12.101.0"

// ////////////////////////////////////////////////////////////////////////////////// //

Expand Down
5 changes: 5 additions & 0 deletions req/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ type Request struct {
Accept string // Accept header
BasicAuthUsername string // Basic auth username
BasicAuthPassword string // Basic auth password
BearerAuth string // Bearer auth token
AutoDiscard bool // Automatically discard all responses with status code != 200
FollowRedirect bool // Follow redirect
Close bool // Close indicates whether to close the connection after sending request
Expand Down Expand Up @@ -681,6 +682,10 @@ func createRequest(e *Engine, r Request, bodyReader io.Reader) (*http.Request, e
req.SetBasicAuth(r.BasicAuthUsername, r.BasicAuthPassword)
}

if r.BearerAuth != "" {
req.Header.Add("Authorization", "Bearer "+r.BearerAuth)
}

if r.Close {
req.Close = true
}
Expand Down
24 changes: 24 additions & 0 deletions req/req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
_URL_ACCEPT = "/accept"
_URL_USER_AGENT = "/user-agent"
_URL_BASIC_AUTH = "/basic-auth"
_URL_BEARER = "/bearer"
_URL_STRING_RESP = "/string-response"
_URL_JSON_RESP = "/json-response"
_URL_DISCARD = "/discard"
Expand All @@ -48,6 +49,7 @@ const (
_TEST_ACCEPT = "application/vnd.example.api+json;version=2"
_TEST_BASIC_AUTH_USER = "admin"
_TEST_BASIC_AUTH_PASS = "password"
_TEST_BEARER_TOKEN = "XUWjA4EnRqUNyqmz"
_TEST_STRING_RESP = "Test String Response"
)

Expand Down Expand Up @@ -351,6 +353,16 @@ func (s *ReqSuite) TestBasicAuth(c *C) {
c.Assert(resp.StatusCode, Equals, 200)
}

func (s *ReqSuite) TestBearerAuth(c *C) {
resp, err := Request{
URL: s.url + _URL_BEARER,
BearerAuth: _TEST_BEARER_TOKEN,
}.Do()

c.Assert(err, IsNil)
c.Assert(resp.StatusCode, Equals, 200)
}

func (s *ReqSuite) TestStringResp(c *C) {
resp, err := Request{
URL: s.url + _URL_STRING_RESP,
Expand Down Expand Up @@ -643,6 +655,7 @@ func runHTTPServer(s *ReqSuite, c *C) {
server.Handler.(*http.ServeMux).HandleFunc(_URL_ACCEPT, acceptRequestHandler)
server.Handler.(*http.ServeMux).HandleFunc(_URL_USER_AGENT, uaRequestHandler)
server.Handler.(*http.ServeMux).HandleFunc(_URL_BASIC_AUTH, basicAuthRequestHandler)
server.Handler.(*http.ServeMux).HandleFunc(_URL_BEARER, bearerRequestHandler)
server.Handler.(*http.ServeMux).HandleFunc(_URL_STRING_RESP, stringRespRequestHandler)
server.Handler.(*http.ServeMux).HandleFunc(_URL_JSON_RESP, jsonRespRequestHandler)
server.Handler.(*http.ServeMux).HandleFunc(_URL_DISCARD, discardRequestHandler)
Expand Down Expand Up @@ -894,6 +907,17 @@ func basicAuthRequestHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}

func bearerRequestHandler(w http.ResponseWriter, r *http.Request) {
header := r.Header.Get("Authorization")

if header != "Bearer "+_TEST_BEARER_TOKEN {
w.WriteHeader(960)
return
}

w.WriteHeader(200)
}

func stringRespRequestHandler(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(_TEST_STRING_RESP))
}
Expand Down

0 comments on commit d6c7091

Please sign in to comment.