Skip to content

Commit cffb5b6

Browse files
authored
test: add test case from branch v2 (#955)
1 parent 5823c50 commit cffb5b6

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

middleware.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func addCredentials(c *Client, r *Request) error {
284284
// Build the token Auth header
285285
if !isStringEmpty(r.AuthToken) {
286286
credentialsAdded = true
287-
r.RawRequest.Header.Set(r.HeaderAuthorizationKey, r.AuthScheme+" "+r.AuthToken)
287+
r.RawRequest.Header.Set(r.HeaderAuthorizationKey, strings.TrimSpace(r.AuthScheme+" "+r.AuthToken))
288288
}
289289

290290
if !c.IsDisableWarn() && credentialsAdded {

request_test.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -671,13 +671,30 @@ func TestRequestAuthScheme(t *testing.T) {
671671
SetAuthScheme("OAuth").
672672
SetAuthToken("004DDB79-6801-4587-B976-F093E6AC44FF")
673673

674-
resp, err := c.R().
675-
SetAuthScheme("Bearer").
676-
SetAuthToken("004DDB79-6801-4587-B976-F093E6AC44FF-Request").
677-
Get(ts.URL + "/profile")
674+
t.Run("override auth scheme", func(t *testing.T) {
675+
resp, err := c.R().
676+
SetAuthScheme("Bearer").
677+
SetAuthToken("004DDB79-6801-4587-B976-F093E6AC44FF-Request").
678+
Get(ts.URL + "/profile")
678679

679-
assertError(t, err)
680-
assertEqual(t, http.StatusOK, resp.StatusCode())
680+
assertError(t, err)
681+
assertEqual(t, http.StatusOK, resp.StatusCode())
682+
})
683+
684+
t.Run("empty auth scheme GH954", func(t *testing.T) {
685+
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"
686+
687+
// set client level
688+
c.SetAuthScheme("").
689+
SetAuthToken(tokenValue)
690+
691+
resp, err := c.R().
692+
Get(ts.URL + "/profile")
693+
694+
assertError(t, err)
695+
assertEqual(t, http.StatusOK, resp.StatusCode())
696+
assertEqual(t, tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
697+
})
681698
}
682699

683700
func TestFormData(t *testing.T) {

resty_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,15 +537,15 @@ func createAuthServerTLSOptional(t *testing.T, useTLS bool) *httptest.Server {
537537

538538
w.Header().Set(hdrContentTypeKey, "application/json; charset=utf-8")
539539

540-
if !strings.HasPrefix(auth, "Bearer ") {
540+
if strings.HasPrefix(auth, "Basic ") {
541541
w.Header().Set("Www-Authenticate", "Protected Realm")
542542
w.WriteHeader(http.StatusUnauthorized)
543543
_, _ = w.Write([]byte(`{ "id": "unauthorized", "message": "Invalid credentials" }`))
544544

545545
return
546546
}
547547

548-
if auth[7:] == "004DDB79-6801-4587-B976-F093E6AC44FF" || auth[7:] == "004DDB79-6801-4587-B976-F093E6AC44FF-Request" {
548+
if strings.Contains(auth, "004DDB79-6801-4587-B976-F093E6AC44FF") {
549549
_, _ = w.Write([]byte(`{ "id": "success", "message": "login successful" }`))
550550
}
551551
}

0 commit comments

Comments
 (0)