chore: 并入 iotlcm-upstream-chan 历史到 upstream-chan/(原提交 1e74497)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user