Skip to content

Commit

Permalink
test challenge forgetting logic
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy committed Nov 24, 2022
1 parent d127354 commit 2d7e2a0
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions transport_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package digest

import (
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -109,3 +111,40 @@ func TestTransportLive(t *testing.T) {
})
}
}

func TestTransportNoChallenge(t *testing.T) {
var expired bool
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if auth := r.Header.Get("Authorization"); auth != "" {
if expired {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
} else {
fmt.Fprintf(w, "Success")
expired = true
}
return
}
chal := &Challenge{
Realm: "test",
Nonce: "jgdfsijdfisd",
Algorithm: "MD5",
QOP: []string{"auth"},
}
w.Header().Add("WWW-Authenticate", chal.String())
http.Error(w, "Unauthorized", http.StatusUnauthorized)
}))
defer ts.Close()
client := http.Client{
Transport: &Transport{
Username: "test",
Password: "test",
},
}
// first request should succeed
res1, err := client.Get(ts.URL)
assert.NilError(t, err)
assert.Equal(t, res1.StatusCode, http.StatusOK)
// second request should fail
_, err = client.Get(ts.URL)
assert.Assert(t, errors.Is(err, ErrNoChallenge))
}

0 comments on commit 2d7e2a0

Please sign in to comment.