Skip to content

Commit

Permalink
test: add test case from branch v2
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jan 17, 2025
1 parent 5823c50 commit fe81780
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ func addCredentials(c *Client, r *Request) error {
// Build the token Auth header
if !isStringEmpty(r.AuthToken) {
credentialsAdded = true
r.RawRequest.Header.Set(r.HeaderAuthorizationKey, r.AuthScheme+" "+r.AuthToken)
r.RawRequest.Header.Set(r.HeaderAuthorizationKey, strings.TrimSpace(r.AuthScheme+" "+r.AuthToken))
}

if !c.IsDisableWarn() && credentialsAdded {
Expand Down
29 changes: 23 additions & 6 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,13 +671,30 @@ func TestRequestAuthScheme(t *testing.T) {
SetAuthScheme("OAuth").
SetAuthToken("004DDB79-6801-4587-B976-F093E6AC44FF")

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

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
})

t.Run("empty auth scheme GH954", func(t *testing.T) {
tokenValue := "004DDB79-6801-4587-B976-F093E6AC44FF"

// set client level
c.SetAuthScheme("").
SetAuthToken(tokenValue)

resp, err := c.R().
Get(ts.URL + "/profile")

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, tokenValue, resp.Request.Header.Get(hdrAuthorizationKey))
})
}

func TestFormData(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions resty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -537,15 +537,15 @@ func createAuthServerTLSOptional(t *testing.T, useTLS bool) *httptest.Server {

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

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

return
}

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

0 comments on commit fe81780

Please sign in to comment.