requestOnce
僅針對下一個請求,使用給定的覆寫變更請求參數一次。您可以連續多次呼叫 requestOnce
,它將依序套用覆寫。如果您只使用 requestOnce
,且資源被呼叫的次數超過已定義的模擬,則會恢復為原始資源。
用法
mock.requestOnce({ header, cookies, method, url, header, statusCode, fetchResponse })
參數
名稱 | 類型 | 詳細資訊 |
---|---|---|
overwrites | MockOverwrite | 覆寫回應的承載 |
overwrites.header | Record<string, string> | 覆寫特定標頭 |
overwrites.cookies | Record<string, string> | 覆寫請求 Cookie |
overwrites.method | 字串 | 覆寫請求方法 |
overwrites.url | 字串 | 覆寫請求 URL 以啟動重新導向 |
params 可選 | MockResponseParams | 額外要覆寫的回應參數 |
params.header 可選 | 物件 | 覆寫特定標頭 |
params.statusCode 可選 | 數字 | 覆寫回應狀態碼 |
params.fetchResponse 可選 | 布林值 | 在以模擬資料回應之前,擷取真實的回應 |
範例
respond.js
it('adds different auth headers to my API requests', async () => {
const mock = await browser.mock('https://application.com/api', {
method: 'get'
})
mock.requestOnce({
headers: { 'Authorization': 'Bearer token' }
})
mock.requestOnce({
headers: { 'Authorization': 'Another bearer token' }
})
await browser.url('https://application.com')
// ...
})