Skip to content

Commit

Permalink
Expose filenaming mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
notVitaliy committed Mar 12, 2019
1 parent b624927 commit d2d2f28
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
/test/_fixtures/https%3A__openstax.org_*_GET_*_body.raw
/test/_fixtures/https%3A__openstax.org_*_GET_*_options.json

/test/_fixtures/https%3A__jsonplaceholder.typicode.com_users_*.raw
/test/_fixtures/https%3A__jsonplaceholder.typicode.com_users_*.json

/browser-bundle.js
/browser-bundle.js.map
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,19 @@ fetch('https://weedmaps.com/sitemap') // <-- This will be ignored from vcr
})
```

## How do I define custom fixture filenames?

There are 3 find/replace variables exposed `[url]`, `[method]`, and `[hash]`.

```js
// import fetch from 'fetch';
import fetch from 'fetch-vcr';

fetch.configure({
fixtureName: '[url]_[method]_[hash]', // <-- This is the default value
})
```

## Jest Setup

Just add the following to `package.json`:
Expand Down
18 changes: 13 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ var CONFIGURATION = {
mode: VCR_MODE,
fixturePath: './_fixtures',
headerBlacklist: ['authorization', 'user-agent'], // These need to be lowercase
ignoreUrls: [] // regex of urls to ignore
ignoreUrls: [], // regex of urls to ignore
fixtureName: '[url]_[method]_[hash]'
}

function debug(url, message) {
Expand Down Expand Up @@ -91,13 +92,19 @@ function buildHash(url, args) {
}

function buildFilenamePrefix(url, args, hash) {
args = args || { }
var [baseUrl, query] = url.split('?')
url = escape(baseUrl).replace(/\//g, '_')
var method = args.method || 'GET'
url += query ? '_' + hashCode(query) : ''

var method = (args || { }).method || 'GET'
method = method.toUpperCase()
var paramsHash = query ? '_' + hashCode(query) : ''
return url + paramsHash + '_' + method + '_' + hash

var fixtureName = CONFIGURATION.fixtureName

return fixtureName
.replace(/\[url\]/g, url)
.replace(/\[method\]/g, method)
.replace(/\[hash\]/g, hash)
}

function buildOptionsFilename(url, args, hash) {
Expand Down Expand Up @@ -244,6 +251,7 @@ fetchVCR.configure = function (config) {
CONFIGURATION.mode = VCR_MODE || config.mode
CONFIGURATION.fixturePath = config.fixturePath || CONFIGURATION.fixturePath
CONFIGURATION.ignoreUrls = config.ignoreUrls || CONFIGURATION.ignoreUrls
CONFIGURATION.fixtureName = config.fixtureName || CONFIGURATION.fixtureName
if (config.headerBlacklist) {
CONFIGURATION.headerBlacklist = []
config.headerBlacklist.forEach(function (key) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.0.0",
"version": "3.1.0",
"name": "fetch-vcr",
"description": "Stop mocking HTTP Requests. Just record and then play them back",
"main": "./lib/index.js",
Expand Down
23 changes: 23 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import test from 'ava'
import assert from 'assert'
import fetchVCR from '../lib/index'
import fs from 'fs'

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

test('sets custom fixtureName', async t => {
const TEST_URL = `https://jsonplaceholder.typicode.com/users`
const fixtureDir = __dirname + '/_fixtures'

fetchVCR.configure({ fixtureName: '[url]', mode: 'record' })
await fetchVCR(TEST_URL)

assert(fs.existsSync(fixtureDir + '/https%3A__jsonplaceholder.typicode.com_users_body.raw'))

fetchVCR.configure({ fixtureName: '[url]_[method]', mode: 'record' })
await fetchVCR(TEST_URL)

assert(fs.existsSync(fixtureDir + '/https%3A__jsonplaceholder.typicode.com_users_GET_body.raw'))

fetchVCR.configure({ fixtureName: '[url]_[hash]', mode: 'record' })
await fetchVCR(TEST_URL)

assert(fs.existsSync(fixtureDir + '/https%3A__jsonplaceholder.typicode.com_users_3938_body.raw'))

t.pass()
})

test.cb('runs in jsdom', t => {
const fs = require('fs')
const jsdom = require('jsdom')
Expand Down

0 comments on commit d2d2f28

Please sign in to comment.