Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1bffadc

Browse files
committedFeb 10, 2017
Make cassette API more predictable by splitting it into two calls
This will give users of this library more control over how they want to mount and unmount multiple cassettes. As well as the added benefit of being able to control when cassettes can be unmounted. Which in turn simplifies how one would deal with multiple promises.
1 parent 1ef4e20 commit 1bffadc

File tree

5 files changed

+101
-59
lines changed

5 files changed

+101
-59
lines changed
 

‎CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 1.0.0 (Feb 10, 2017)
4+
- Split casssette API in two functions so users can mount and unmount cassettes at different parts of their codebase
5+
36
## 0.1.0 (Jun 16, 2016)
47
### Added
58
- Initial release

‎README.md

+23-5
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,29 @@ Using axios-vcr is very simple. All you need to do is to provide a cassette path
2525
```javascript
2626
var axiosVCR = require('axios-vcr');
2727

28-
axiosVCR.mountCassette('./test/fixtures/cats.json', () => {
29-
axios.get('https://reddit.com/r/cats.json').then(response => {
30-
// axios-vcr will store the remote response from /cats.json
31-
// in ./test/fixtures/cats.json
32-
// Subsequent requests will then load the response directly from the file system
28+
axiosVCR.mountCassette('./test/fixtures/cats.json')
29+
30+
axios.get('https://reddit.com/r/cats.json').then(response => {
31+
// axios-vcr will store the remote response from /cats.json
32+
// in ./test/fixtures/cats.json
33+
// Subsequent requests will then load the response directly from the file system
34+
35+
VCR.ejectCassette('https://reddit.com/r/cats.json')
36+
})
37+
```
38+
39+
### Usage in a test case
40+
```javascript
41+
it('makes your requests load faster and more reliably', function(done) {
42+
// mount a cassette
43+
axiosVCR.mountCassette('./fixtures/test_case_name.json')
44+
45+
myAPI.fetchSomethingFromRemote().then(function(response) {
46+
assert.equal(response.something, 'some value')
47+
done()
48+
49+
// Eject the cassette when all your promises have been fulfilled
50+
axiosVCR.ejectCassette('./fixture/test_case_name.json')
3351
})
3452
})
3553
```

‎index.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var RequestMiddleware = require('./lib/RequestMiddleware');
22
var ResponseMiddleware = require('./lib/ResponseMiddleware');
33

4-
function mountCassette(cassettePath, cb) {
4+
var cassettes = {}
5+
6+
function mountCassette(cassettePath) {
57
var axios = require('axios');
68

79
var responseInterceptor = axios.interceptors.response.use(
@@ -12,16 +14,26 @@ function mountCassette(cassettePath, cb) {
1214
var requestInterceptor = axios.interceptors.request.use(
1315
RequestMiddleware.success(cassettePath),
1416
RequestMiddleware.failure
15-
)
17+
);
18+
19+
cassettes[cassettePath] = {
20+
responseInterceptor: responseInterceptor,
21+
requestInterceptor: requestInterceptor,
22+
axios: axios
23+
};
24+
}
1625

17-
cb();
26+
function ejectCassette(cassettePath) {
27+
var interceptors = cassettes[cassettePath];
28+
var axios = interceptors.axios;
1829

19-
axios.interceptors.response.eject(responseInterceptor);
20-
axios.interceptors.request.eject(requestInterceptor);
30+
axios.interceptors.response.eject(interceptors.responseInterceptor);
31+
axios.interceptors.request.eject(interceptors.requestInterceptor);
2132
}
2233

2334
module.exports = {
2435
mountCassette: mountCassette,
36+
ejectCassette: ejectCassette,
2537
RequestMiddleware: RequestMiddleware,
2638
ResponseMiddleware: ResponseMiddleware
2739
}

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "axios-vcr",
3-
"version": "0.1.0",
3+
"version": "1.0.0",
44
"description": "axios-vcr is a set of middlewares for axios that allow you to record and replay requests.",
55
"main": "index.js",
66
"homepage": "http://github.com/nettofarah/axios-vcr",

‎test/axios_vcr_test.js

+57-48
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,43 @@ describe('Axios VCR', function() {
3535

3636
it('generates stubs for requests', function(done) {
3737
var path = './test/fixtures/posts.json'
38-
VCR.mountCassette(path, function () {
39-
axios.get(posts).then(function(response) {
40-
getFixture(path, response.config).then(function(fixture) {
41-
assert.deepEqual(fixture.originalResponseData.data, response.data)
42-
done()
43-
})
38+
VCR.mountCassette(path)
39+
40+
axios.get(posts).then(function(response) {
41+
getFixture(path, response.config).then(function(fixture) {
42+
assert.deepEqual(fixture.originalResponseData.data, response.data)
43+
done()
44+
VCR.ejectCassette(path)
4445
})
4546
})
4647
})
4748

4849
it('works with nested folders', function(done) {
4950
var cassettePath = './test/fixtures/nested/posts.json'
50-
VCR.mountCassette(cassettePath, function () {
51-
axios.get(posts).then(function(response) {
52-
getFixture(cassettePath, response.config).then(function(fixture) {
53-
assert.deepEqual(fixture.originalResponseData.data, response.data)
54-
done()
55-
})
56-
}).catch(function(err) { console.log(err) })
57-
})
51+
VCR.mountCassette(cassettePath)
52+
53+
axios.get(posts).then(function(response) {
54+
getFixture(cassettePath, response.config).then(function(fixture) {
55+
assert.deepEqual(fixture.originalResponseData.data, response.data)
56+
done()
57+
58+
VCR.ejectCassette(cassettePath)
59+
})
60+
}).catch(function(err) { console.log(err) })
5861
})
5962

6063
it('stores headers and status', function(done) {
6164
var cassettePath = './test/fixtures/posts.json'
62-
VCR.mountCassette(cassettePath, function () {
63-
axios.get(posts).then(function(response) {
64-
getFixture(cassettePath, response.config).then(function(fixture) {
65-
assert.deepEqual(fixture.originalResponseData.headers, response.headers)
66-
assert.equal(fixture.originalResponseData.status, response.status)
67-
assert.equal(fixture.originalResponseData.statusText, response.statusText)
68-
done()
69-
})
65+
VCR.mountCassette(cassettePath)
66+
67+
axios.get(posts).then(function(response) {
68+
getFixture(cassettePath, response.config).then(function(fixture) {
69+
assert.deepEqual(fixture.originalResponseData.headers, response.headers)
70+
assert.equal(fixture.originalResponseData.status, response.status)
71+
assert.equal(fixture.originalResponseData.statusText, response.statusText)
72+
done()
73+
74+
VCR.ejectCassette(cassettePath)
7075
})
7176
})
7277
})
@@ -84,14 +89,15 @@ describe('Axios VCR', function() {
8489
assert(fileExists(path))
8590

8691
var url = 'http://something.com/unexisting'
92+
VCR.mountCassette(path)
8793

88-
VCR.mountCassette(path, function () {
89-
axios.get(url).then(function(res) {
90-
getFixture(path, res.config).then(function(fixture) {
91-
assert.deepEqual(fixture.originalResponseData, _.omit(res, 'fixture'))
92-
done()
93-
}).catch(err => { console.log(err); done() })
94-
})
94+
axios.get(url).then(function(res) {
95+
getFixture(path, res.config).then(function(fixture) {
96+
assert.deepEqual(fixture.originalResponseData, _.omit(res, 'fixture'))
97+
done()
98+
99+
VCR.ejectCassette(path)
100+
}).catch(err => { console.log(err); done() })
95101
})
96102
})
97103

@@ -103,13 +109,14 @@ describe('Axios VCR', function() {
103109
} catch(e) {}
104110

105111
assert(!fileExists(path))
112+
VCR.mountCassette(path)
106113

107-
VCR.mountCassette(path, function () {
108-
axios.get(posts).then(function(response) {
109-
assert.equal(200, response.status)
110-
fs.unlinkSync(path)
111-
done()
112-
})
114+
axios.get(posts).then(function(response) {
115+
assert.equal(200, response.status)
116+
fs.unlinkSync(path)
117+
done()
118+
119+
VCR.ejectCassette(path)
113120
})
114121
})
115122
})
@@ -126,22 +133,24 @@ describe('Axios VCR', function() {
126133
it('stores multiple requests in the same cassette', function(done) {
127134
var path = './test/fixtures/multiple.json'
128135

129-
VCR.mountCassette(path, function() {
130-
var usersPromise = axios.get(usersUrl)
131-
var todosPromise = axios.get(todosUrl)
136+
VCR.mountCassette(path)
137+
138+
var usersPromise = axios.get(usersUrl)
139+
var todosPromise = axios.get(todosUrl)
132140

133-
Promise.all([usersPromise, todosPromise]).then(function(responses) {
134-
var usersResponse = responses[0]
135-
var todosResponse = responses[1]
141+
Promise.all([usersPromise, todosPromise]).then(function(responses) {
142+
var usersResponse = responses[0]
143+
var todosResponse = responses[1]
136144

137-
var usersResponsePromise = getFixture(path, usersResponse.config)
138-
var todosResponsePromise = getFixture(path, todosResponse.config)
145+
var usersResponsePromise = getFixture(path, usersResponse.config)
146+
var todosResponsePromise = getFixture(path, todosResponse.config)
147+
148+
Promise.all([usersResponsePromise, todosResponsePromise]).then(function(fixtures) {
149+
assert.deepEqual(fixtures[0].originalResponseData.data, usersResponse.data)
150+
assert.deepEqual(fixtures[1].originalResponseData.data, todosResponse.data)
151+
done()
139152

140-
Promise.all([usersResponsePromise, todosResponsePromise]).then(function(fixtures) {
141-
assert.deepEqual(fixtures[0].originalResponseData.data, usersResponse.data)
142-
assert.deepEqual(fixtures[1].originalResponseData.data, todosResponse.data)
143-
done()
144-
})
153+
VCR.ejectCassette(path)
145154
})
146155
})
147156
})

0 commit comments

Comments
 (0)
Please sign in to comment.