Skip to content

Commit

Permalink
feat: converts JSON requests to objects for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
moltar authored and Roman committed Apr 29, 2020
1 parent 97005dc commit e80af32
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
"log": {
"_recordingName": "jest-polly/expands JSON request to JSON object",
"creator": {
"comment": "persister:fs",
"name": "Polly.JS",
"version": "4.1.0"
},
"entries": [
{
"_id": "4ce0bef779e22316114e462b687a900b",
"_order": 0,
"cache": {},
"request": {
"bodySize": 10,
"cookies": [],
"headers": [
{
"_fromType": "array",
"name": "content-type",
"value": "application/json"
},
{
"_fromType": "array",
"name": "accept",
"value": "*/*"
},
{
"_fromType": "array",
"name": "content-length",
"value": "10"
},
{
"_fromType": "array",
"name": "user-agent",
"value": "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)"
},
{
"_fromType": "array",
"name": "accept-encoding",
"value": "gzip,deflate"
},
{
"_fromType": "array",
"name": "connection",
"value": "close"
},
{
"name": "host",
"value": "localhost:8080"
}
],
"headersSize": 245,
"httpVersion": "HTTP/1.1",
"method": "POST",
"postData": {
"mimeType": "application/json",
"params": [],
"text": {
"a": true
}
},
"queryString": [],
"url": "http://localhost:8080/"
},
"response": {
"bodySize": 2,
"content": {
"mimeType": "text/plain",
"size": 2,
"text": "{}"
},
"cookies": [],
"headers": [
{
"name": "content-type",
"value": "text/plain"
},
{
"name": "date",
"value": "Wed, 29 Apr 2020 14:49:38 GMT"
},
{
"name": "connection",
"value": "close"
},
{
"name": "transfer-encoding",
"value": "chunked"
}
],
"headersSize": 112,
"httpVersion": "HTTP/1.1",
"redirectURL": "",
"status": 200,
"statusText": "OK"
},
"startedDateTime": "2020-04-29T14:49:38.521Z",
"time": 33,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 33
}
}
],
"pages": [],
"version": "1.2"
}
}
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const APPLICATION_JSON_MIME = 'application/json'
20 changes: 18 additions & 2 deletions src/jest-polly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import http from 'http'
import fetch from 'node-fetch'

import { jestPollyConfigService } from './config'

const APPLICATION_JSON_MIME = 'application/json'
import { APPLICATION_JSON_MIME } from './constants'

jestPollyConfigService.config = {
matchRequestsBy: {
Expand Down Expand Up @@ -131,4 +130,21 @@ describe('jest-polly', () => {

expect(message).toStrictEqual({})
})

// eslint-disable-next-line sonarjs/no-identical-functions
it('expands JSON request to JSON object', async () => {
expect.assertions(1)

const server = await createServer('{}')

await postMessage()

// Go offline
await destroyServer(server)

// Replays recording
const message = await postMessage()

expect(message).toStrictEqual({})
})
})
32 changes: 30 additions & 2 deletions src/jest-polly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import FSPersister from '@pollyjs/persister-fs'
import { setupPolly } from 'setup-polly-jest'

import { jestPollyConfigService } from './config'
import { APPLICATION_JSON_MIME } from './constants'

Polly.register(NodeHttpAdapter)
Polly.register(FSPersister)

function isJsonMime(text: string) {
return text.includes(APPLICATION_JSON_MIME)
}

// converts JSON responses from text to JSON objects
// See: https://github.com/Netflix/pollyjs/issues/322
Polly.on('create', (polly) => {
Expand All @@ -16,18 +21,28 @@ Polly.on('create', (polly) => {
.on('beforePersist', (request, recording) => {
const { content } = recording.response

if (content && content.mimeType && content.mimeType.includes('application/json')) {
if (content && content.mimeType && isJsonMime(content.mimeType)) {
try {
content.text = JSON.parse(content.text)
} catch (error) {
// noop
}
}

const { postData } = recording.request

if (postData && postData.mimeType && isJsonMime(postData.mimeType)) {
try {
postData.text = JSON.parse(postData.text)
} catch (error) {
// noop
}
}
})
.on('beforeReplay', (request, recording) => {
const { content } = recording.response

if (content && content.mimeType && content.mimeType.includes('application/json')) {
if (content && content.mimeType && isJsonMime(content.mimeType)) {
try {
// allows older-style recordings to exist which had JSON stringified
if (content && content.text && typeof content.text !== 'string') {
Expand All @@ -37,6 +52,19 @@ Polly.on('create', (polly) => {
// noop
}
}

const { postData } = recording.request

if (postData && postData.mimeType && isJsonMime(postData.mimeType)) {
try {
// allows older-style recordings to exist which had JSON stringified
if (postData && postData.text && typeof postData.text !== 'string') {
postData.text = JSON.stringify(postData.text)
}
} catch (error) {
// noop
}
}
})
})

Expand Down

0 comments on commit e80af32

Please sign in to comment.