Skip to content

Commit d2d2f28

Browse files
committed
Expose filenaming mechanism
1 parent b624927 commit d2d2f28

5 files changed

Lines changed: 53 additions & 6 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
/test/_fixtures/https%3A__openstax.org_*_GET_*_body.raw
44
/test/_fixtures/https%3A__openstax.org_*_GET_*_options.json
55

6+
/test/_fixtures/https%3A__jsonplaceholder.typicode.com_users_*.raw
7+
/test/_fixtures/https%3A__jsonplaceholder.typicode.com_users_*.json
8+
69
/browser-bundle.js
710
/browser-bundle.js.map

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ fetch('https://weedmaps.com/sitemap') // <-- This will be ignored from vcr
8484
})
8585
```
8686

87+
## How do I define custom fixture filenames?
88+
89+
There are 3 find/replace variables exposed `[url]`, `[method]`, and `[hash]`.
90+
91+
```js
92+
// import fetch from 'fetch';
93+
import fetch from 'fetch-vcr';
94+
95+
fetch.configure({
96+
fixtureName: '[url]_[method]_[hash]', // <-- This is the default value
97+
})
98+
```
99+
87100
## Jest Setup
88101

89102
Just add the following to `package.json`:

lib/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ var CONFIGURATION = {
2121
mode: VCR_MODE,
2222
fixturePath: './_fixtures',
2323
headerBlacklist: ['authorization', 'user-agent'], // These need to be lowercase
24-
ignoreUrls: [] // regex of urls to ignore
24+
ignoreUrls: [], // regex of urls to ignore
25+
fixtureName: '[url]_[method]_[hash]'
2526
}
2627

2728
function debug(url, message) {
@@ -91,13 +92,19 @@ function buildHash(url, args) {
9192
}
9293

9394
function buildFilenamePrefix(url, args, hash) {
94-
args = args || { }
9595
var [baseUrl, query] = url.split('?')
9696
url = escape(baseUrl).replace(/\//g, '_')
97-
var method = args.method || 'GET'
97+
url += query ? '_' + hashCode(query) : ''
98+
99+
var method = (args || { }).method || 'GET'
98100
method = method.toUpperCase()
99-
var paramsHash = query ? '_' + hashCode(query) : ''
100-
return url + paramsHash + '_' + method + '_' + hash
101+
102+
var fixtureName = CONFIGURATION.fixtureName
103+
104+
return fixtureName
105+
.replace(/\[url\]/g, url)
106+
.replace(/\[method\]/g, method)
107+
.replace(/\[hash\]/g, hash)
101108
}
102109

103110
function buildOptionsFilename(url, args, hash) {
@@ -244,6 +251,7 @@ fetchVCR.configure = function (config) {
244251
CONFIGURATION.mode = VCR_MODE || config.mode
245252
CONFIGURATION.fixturePath = config.fixturePath || CONFIGURATION.fixturePath
246253
CONFIGURATION.ignoreUrls = config.ignoreUrls || CONFIGURATION.ignoreUrls
254+
CONFIGURATION.fixtureName = config.fixtureName || CONFIGURATION.fixtureName
247255
if (config.headerBlacklist) {
248256
CONFIGURATION.headerBlacklist = []
249257
config.headerBlacklist.forEach(function (key) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "3.0.0",
2+
"version": "3.1.0",
33
"name": "fetch-vcr",
44
"description": "Stop mocking HTTP Requests. Just record and then play them back",
55
"main": "./lib/index.js",

test/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'ava'
22
import assert from 'assert'
33
import fetchVCR from '../lib/index'
4+
import fs from 'fs'
45

56
function createLongParams (num) {
67
return 'abcdefghijklmnopqrstuvwxyz'.split('').map(letter => `${letter}=${num}`).join('&')
@@ -82,6 +83,28 @@ test('caches fixture with long name', async t => {
8283
t.pass()
8384
})
8485

86+
test('sets custom fixtureName', async t => {
87+
const TEST_URL = `https://jsonplaceholder.typicode.com/users`
88+
const fixtureDir = __dirname + '/_fixtures'
89+
90+
fetchVCR.configure({ fixtureName: '[url]', mode: 'record' })
91+
await fetchVCR(TEST_URL)
92+
93+
assert(fs.existsSync(fixtureDir + '/https%3A__jsonplaceholder.typicode.com_users_body.raw'))
94+
95+
fetchVCR.configure({ fixtureName: '[url]_[method]', mode: 'record' })
96+
await fetchVCR(TEST_URL)
97+
98+
assert(fs.existsSync(fixtureDir + '/https%3A__jsonplaceholder.typicode.com_users_GET_body.raw'))
99+
100+
fetchVCR.configure({ fixtureName: '[url]_[hash]', mode: 'record' })
101+
await fetchVCR(TEST_URL)
102+
103+
assert(fs.existsSync(fixtureDir + '/https%3A__jsonplaceholder.typicode.com_users_3938_body.raw'))
104+
105+
t.pass()
106+
})
107+
85108
test.cb('runs in jsdom', t => {
86109
const fs = require('fs')
87110
const jsdom = require('jsdom')

0 commit comments

Comments
 (0)