fix(gold): parse VNAppMob JWT object wrapper

This commit is contained in:
2026-06-15 12:20:40 +07:00
parent c889d77472
commit 67387ff350
2 changed files with 51 additions and 4 deletions
+12 -4
View File
@@ -232,11 +232,19 @@ func (c *VNAppMobClient) refreshKeyLocked(ctx context.Context) error {
if err != nil {
return fmt.Errorf("vnappmob: refresh read body: %w", err)
}
// The endpoint may return either a JSON-quoted string or a raw JWT.
// The live endpoint wraps the JWT in {"results":"<jwt>"}. Keep fallbacks
// for a JSON-quoted string or a raw JWT in case the format changes.
token = strings.TrimSpace(string(body))
var quoted string
if err := json.Unmarshal(body, &quoted); err == nil {
token = strings.TrimSpace(quoted)
var wrapped struct {
Results string `json:"results"`
}
if err := json.Unmarshal(body, &wrapped); err == nil {
token = strings.TrimSpace(wrapped.Results)
} else {
var quoted string
if err := json.Unmarshal(body, &quoted); err == nil {
token = strings.TrimSpace(quoted)
}
}
if token == "" {
return fmt.Errorf("vnappmob: refresh returned empty token")
@@ -141,6 +141,45 @@ func TestRefreshKey(t *testing.T) {
}
}
func TestRefreshKey_ObjectWrapper(t *testing.T) {
exp := time.Unix(1000, 0).Add(14 * 24 * time.Hour).Unix()
jwt := makeJWT(exp)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/request_api_key" {
t.Errorf("refresh path: got %s", r.URL.Path)
}
fmt.Fprintf(w, `{"results":%q}`, jwt)
}))
defer srv.Close()
c := newTestVNAppMobClient(srv, storage.NewMemoryKVStore())
key, err := c.getKey(context.Background())
if err != nil {
t.Fatalf("getKey: %v", err)
}
if key != jwt {
t.Fatalf("key mismatch: got %q, want %q", key, jwt)
}
}
func TestRefreshKey_QuotedString(t *testing.T) {
exp := time.Unix(1000, 0).Add(14 * 24 * time.Hour).Unix()
jwt := makeJWT(exp)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%q", jwt)
}))
defer srv.Close()
c := newTestVNAppMobClient(srv, storage.NewMemoryKVStore())
key, err := c.getKey(context.Background())
if err != nil {
t.Fatalf("getKey: %v", err)
}
if key != jwt {
t.Fatalf("key mismatch: got %q, want %q", key, jwt)
}
}
func TestFetchSJCPrice(t *testing.T) {
exp := time.Unix(1000, 0).Add(14 * 24 * time.Hour).Unix()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {