修复小程序内充值设备识别
为 API 请求和刷新请求补充宿主运行时请求头。 在小程序充值设备身份异常时清理旧 token 并重新拉起小程序登录。 补充小程序充值回归测试并隔离测试 spy。
This commit is contained in:
@@ -213,6 +213,84 @@ describe('apiClient', () => {
|
||||
expect(getStoredAccessToken()).toBe('fresh-token');
|
||||
});
|
||||
|
||||
it('attaches mini program client headers to refresh and protected requests', async () => {
|
||||
Object.assign(window, {
|
||||
location: {
|
||||
search:
|
||||
'?clientRuntime=wechat_mini_program&hostPlatform=ios&miniProgramEnv=release',
|
||||
},
|
||||
wx: {
|
||||
miniProgram: {
|
||||
postMessage: vi.fn(),
|
||||
},
|
||||
},
|
||||
});
|
||||
fetchMock
|
||||
.mockResolvedValueOnce(
|
||||
createResponseMock({
|
||||
status: 200,
|
||||
body: JSON.stringify({
|
||||
ok: true,
|
||||
data: {
|
||||
token: 'mini-token',
|
||||
},
|
||||
error: null,
|
||||
meta: {
|
||||
apiVersion: '2026-06-16',
|
||||
},
|
||||
}),
|
||||
}),
|
||||
)
|
||||
.mockResolvedValueOnce(
|
||||
createResponseMock({
|
||||
status: 200,
|
||||
body: JSON.stringify({
|
||||
ok: true,
|
||||
data: {
|
||||
value: 11,
|
||||
},
|
||||
error: null,
|
||||
meta: {
|
||||
apiVersion: '2026-06-16',
|
||||
},
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
const result = await requestJson<{ value: number }>(
|
||||
'/api/runtime/protected',
|
||||
{ method: 'GET' },
|
||||
'读取受保护数据失败',
|
||||
);
|
||||
|
||||
expect(result).toEqual({ value: 11 });
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(
|
||||
1,
|
||||
'/api/auth/refresh',
|
||||
expect.objectContaining({
|
||||
headers: expect.objectContaining({
|
||||
'x-client-type': 'mini_program',
|
||||
'x-client-runtime': 'wechat_mini_program',
|
||||
'x-client-platform': 'ios',
|
||||
'x-mini-program-env': 'release',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(fetchMock).toHaveBeenNthCalledWith(
|
||||
2,
|
||||
'/api/runtime/protected',
|
||||
expect.objectContaining({
|
||||
headers: expect.objectContaining({
|
||||
Authorization: 'Bearer mini-token',
|
||||
'x-client-type': 'mini_program',
|
||||
'x-client-runtime': 'wechat_mini_program',
|
||||
'x-client-platform': 'ios',
|
||||
'x-mini-program-env': 'release',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('does not emit auth change events when 401 probe requests opt into silent mode', async () => {
|
||||
fetchMock.mockResolvedValueOnce(createResponseMock({ status: 401 }));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user