Skip to content

Commit

Permalink
feat: inflates JSON mime types to JSON objects for store
Browse files Browse the repository at this point in the history
  • Loading branch information
moltar committed Apr 4, 2020
1 parent 4d0b974 commit 7ac70ea
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{
"log": {
"_recordingName": "expands JSON response to JSON object",
"creator": {
"comment": "persister:fs",
"name": "Polly.JS",
"version": "4.0.4"
},
"entries": [
{
"_id": "7638f8072f801ac2dcff82b36b3e0d76",
"_order": 0,
"cache": {},
"request": {
"bodySize": 0,
"cookies": [],
"headers": [
{
"_fromType": "array",
"name": "accept",
"value": "*/*"
},
{
"_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": 192,
"httpVersion": "HTTP/1.1",
"method": "GET",
"queryString": [],
"url": "http://localhost:8080/"
},
"response": {
"bodySize": 10,
"content": {
"mimeType": "application/json",
"size": 10,
"text": {
"a": true
}
},
"cookies": [],
"headers": [
{
"name": "content-type",
"value": "application/json"
},
{
"name": "date",
"value": "Sat, 04 Apr 2020 05:35:52 GMT"
},
{
"name": "connection",
"value": "close"
},
{
"name": "transfer-encoding",
"value": "chunked"
}
],
"headersSize": 118,
"httpVersion": "HTTP/1.1",
"redirectURL": "",
"status": 200,
"statusText": "OK"
},
"startedDateTime": "2020-04-04T05:35:52.196Z",
"time": 21,
"timings": {
"blocked": -1,
"connect": -1,
"dns": -1,
"receive": 0,
"send": 0,
"ssl": -1,
"wait": 21
}
}
],
"pages": [],
"version": "1.2"
}
}
18 changes: 18 additions & 0 deletions src/jest-polly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,24 @@ test('replays recording', async () => {
expect(message).toBe(RESPONSE)
})

test('expands JSON response to JSON object', async () => {
expect.assertions(1)

const RESPONSE = JSON.stringify({ a: true })

const server = await createServer(RESPONSE, 'application/json')

// Records if missing
await fetchMessage()

// Go offline
await destroyServer(server)

// Replays recording
const message = await fetchMessage()
expect(message).toBe(RESPONSE)
})

async function fetchMessage() {
const response = await fetch('http://localhost:8080')
return response.text()
Expand Down
35 changes: 32 additions & 3 deletions src/jest-polly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ import { jestPollyConfigService } from './config'
Polly.register(NodeHttpAdapter)
Polly.register(FSPersister)

// converts JSON responses from text to JSON objects
// See: https://github.com/Netflix/pollyjs/issues/322
Polly.on('create', polly => {
polly.server
.any()
.on('beforePersist', (req, recording) => {
const { content } = recording.response

if (content.mimeType.includes('application/json')) {
try {
content.text = JSON.parse(content.text)
} catch (e) {
// noop
}
}
})
.on('beforeReplay', (req, recording) => {
const { content } = recording.response

if (content.mimeType.includes('application/json')) {
try {
content.text = JSON.stringify(content.text)
} catch (e) {
// noop
}
}
})
})

// We have to define our own "Context" here, and can't re-use types from
// "@types/setup-polly-jest" package, because, for some reason, there is a problem with
// exporting the type and when importing @scaleleap/jest-polly in consuming modules,
Expand All @@ -20,8 +49,8 @@ export const jestPollyContext: JestPollyContext = setupPolly()

// and then before each test run, we'll update it with actual configuration, because
// some of the configuration, depends on being inside a test
beforeEach(() =>
jestPollyContext.polly.configure(jestPollyConfigService.config),
)
beforeEach(() => {
jestPollyContext.polly.configure(jestPollyConfigService.config)
})

afterEach(() => jestPollyContext.polly.flush())

0 comments on commit 7ac70ea

Please sign in to comment.