mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-01 19:17:58 +00:00
api: store token cookie, tests
This commit is contained in:
47
api/client_test.go
Normal file
47
api/client_test.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestClientToken(t *testing.T) {
|
||||
tokenValue := "foo"
|
||||
handler := func(w http.ResponseWriter, req *http.Request) {
|
||||
http.SetCookie(w, &http.Cookie{
|
||||
Name: TokenCookieName,
|
||||
Value: tokenValue,
|
||||
Expires: time.Now().Add(time.Hour),
|
||||
})
|
||||
}
|
||||
|
||||
config, ln := testHTTPServer(t, http.HandlerFunc(handler))
|
||||
defer ln.Close()
|
||||
|
||||
client, err := NewClient(config)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Should have no token initially
|
||||
if v := client.Token(); v != "" {
|
||||
t.Fatalf("bad: %s", v)
|
||||
}
|
||||
|
||||
// Do a raw "/" request to set the cookie
|
||||
if _, err := client.RawRequest(client.NewRequest("GET", "/")); err != nil {
|
||||
t.Fatalf("err: %s", err)
|
||||
}
|
||||
|
||||
// Verify the token is set
|
||||
if v := client.Token(); v != tokenValue {
|
||||
t.Fatalf("bad: %s", v)
|
||||
}
|
||||
|
||||
client.ClearToken()
|
||||
|
||||
if v := client.Token(); v != "" {
|
||||
t.Fatalf("bad: %s", v)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user