51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
package upstreamimpl
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestQianxunTokenProviderAcceptsTokenWithoutExpiry(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost || r.URL.Path != "/oauth/access_token" {
|
|
t.Fatalf("request = %s %s", r.Method, r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte(`{"code":0,"msg":"success","data":{"token":"token-1","expireDate":0}}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
provider := NewQianxunTokenProvider(QianxunTokenConfig{
|
|
BaseURL: server.URL, Path: "/oauth/access_token", OperatorID: "operator-1",
|
|
AppID: "app-1", TokenType: "card", Key: "key-1", Timeout: time.Second,
|
|
})
|
|
token, err := provider.Fetch(context.Background())
|
|
if err != nil {
|
|
t.Fatalf("fetch token: %v", err)
|
|
}
|
|
if token.Value != "token-1" {
|
|
t.Fatalf("token = %q", token.Value)
|
|
}
|
|
if !token.ExpiresAt.IsZero() {
|
|
t.Fatalf("expireDate=0 must produce a token without expiry: %s", token.ExpiresAt)
|
|
}
|
|
}
|
|
|
|
func TestQianxunTokenProviderRejectsEmptyToken(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
_, _ = w.Write([]byte(`{"code":0,"msg":"success","data":{"token":"","expireDate":0}}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
provider := NewQianxunTokenProvider(QianxunTokenConfig{
|
|
BaseURL: server.URL, OperatorID: "operator-1", AppID: "app-1",
|
|
TokenType: "card", Key: "key-1", Timeout: time.Second,
|
|
})
|
|
if _, err := provider.Fetch(context.Background()); err == nil {
|
|
t.Fatal("expected empty token error")
|
|
}
|
|
}
|