chore: 并入 iotlcm-upstream-chan 历史到 upstream-chan/(原提交 1e74497)
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package upstreamimpl
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLanShangTokenProviderFetch(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/get_token" {
|
||||
t.Fatalf("path = %q", r.URL.Path)
|
||||
}
|
||||
if got := r.URL.Query().Get("sub_account"); got != "\u66fc\u76db\u7269\u8054\u7f51" {
|
||||
t.Fatalf("sub_account = %q", got)
|
||||
}
|
||||
if got := r.URL.Query().Get("secret"); got != "Qw@0371252" {
|
||||
t.Fatalf("secret = %q", got)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{"token":"abc123","expire_in":120}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
provider := NewLanShangTokenProvider(LanShangTokenConfig{
|
||||
BaseURL: server.URL, Path: "/get_token", SubAccount: "\u66fc\u76db\u7269\u8054\u7f51",
|
||||
Secret: "Qw@0371252", Timeout: time.Second,
|
||||
})
|
||||
token, err := provider.Fetch(context.Background())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if token.Value != "abc123" {
|
||||
t.Fatalf("token = %q", token.Value)
|
||||
}
|
||||
if token.ExpiresAt.Before(time.Now().Add(100 * time.Second)) {
|
||||
t.Fatalf("expiry too short: %s", token.ExpiresAt)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLanShangTokenProviderRejectsEmptyToken(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte(`{"expire_in":120}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
provider := NewLanShangTokenProvider(LanShangTokenConfig{
|
||||
BaseURL: server.URL, SubAccount: "a", Secret: "b", Timeout: time.Second,
|
||||
})
|
||||
if _, err := provider.Fetch(context.Background()); err == nil {
|
||||
t.Fatal("expected empty token error")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user