Skip to content

Commit 780f9a9

Browse files
committed
fix: parse JSON post data in grabRecordedNetworkTraffics so every helper returns the same type
1 parent 4953029 commit 780f9a9

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎lib/helper/network/actions.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ async function seeTraffic({ name, url, parameters, requestPostData, timeout = 10
5656
}
5757
}
5858

59+
function parsePostData(postData) {
60+
if (typeof postData !== 'string') return postData
61+
try {
62+
return JSON.parse(postData)
63+
} catch (e) {
64+
return postData
65+
}
66+
}
67+
5968
async function grabRecordedNetworkTraffics() {
6069
if (!this.recordedAtLeastOnce) {
6170
throw new Error('Failure in test automation. You use "I.grabRecordedNetworkTraffics", but "I.startRecordingTraffic" was never called before.');
@@ -67,7 +76,7 @@ async function grabRecordedNetworkTraffics() {
6776
url: request.url,
6877
method: request.method,
6978
headers: request.requestHeaders,
70-
postData: request.requestPostData,
79+
postData: parsePostData(request.requestPostData),
7180
}
7281

7382
if (!resp) {

‎test/helper/webapi.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,6 +2479,20 @@ export function tests() {
24792479
expect(headerNames).to.include('user-agent')
24802480
})
24812481

2482+
it('should grab JSON post data of recorded traffics as an object', async () => {
2483+
await I.amOnPage('https://codecept.io/')
2484+
await I.startRecordingTraffic()
2485+
await I.executeScript(() =>
2486+
fetch('https://codecept.io/traffic-post-data', { method: 'POST', body: JSON.stringify({ name: 'codecept' }) })
2487+
.then(res => res.status)
2488+
.catch(() => 0),
2489+
)
2490+
await I.seeTraffic({ name: 'post', url: 'https://codecept.io/traffic-post-data' })
2491+
const traffics = await I.grabRecordedNetworkTraffics()
2492+
const post = traffics.find(t => t.request.method === 'POST')
2493+
expect(post.request.postData).to.deep.equal({ name: 'codecept' })
2494+
})
2495+
24822496
it('should stop the network recording', async () => {
24832497
await I.startRecordingTraffic()
24842498
await I.amOnPage('https://codecept.io/')

0 commit comments

Comments
 (0)