Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test case from branch v2 #955

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading