Skip to content

Commit

Permalink
Merge pull request nock#117 from tinogomes/master
Browse files Browse the repository at this point in the history
Sugar sintaxe for times method: "once", "twice", and "thrice"
  • Loading branch information
pgte committed Jul 17, 2013
2 parents 1c7eb1d + 14600a7 commit 89d21fb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,13 +149,23 @@ var scope = nock('https://my.server.com:8081')
You are able to specify the number of times to repeat the same response.

```js
nock('https://zombo.com').get('/').times(2).reply(200, 'Ok');
nock('https://zombo.com').get('/').times(4).reply(200, 'Ok');

http.get('https://zombo.com/'); // respond body "Ok"
http.get('https://zombo.com/'); // respond body "Ok"
http.get('https://zombo.com/'); // respond body "Ok"
http.get('https://zombo.com/'); // respond body "Ok"
http.get('https://zombo.com/'); // respond with zombo.com result
```

Sugar sintaxe

```js
nock('https://zombo.com').get('/').once().reply(200, 'Ok');
nock('https://zombo.com').get('/').twice().reply(200, 'Ok');
nock('https://zombo.com').get('/').thrice().reply(200, 'Ok');
```

## Chaining

You can chain behaviour like this:
Expand Down
15 changes: 15 additions & 0 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ function startScope(basePath, options) {

return this;
}

function once() {
return this.times(1);
}

function twice() {
return this.times(2);
}

function thrice() {
return this.times(3);
}

var interceptor = {
_key: key
Expand All @@ -195,6 +207,9 @@ function startScope(basePath, options) {
, filteringPath: filteringPath
, matchHeader: matchHeader
, times: times
, once: once
, twice: twice
, thrice: thrice
};

return interceptor;
Expand Down
68 changes: 62 additions & 6 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -1856,21 +1856,77 @@ test('enable real HTTP request only for google.com, via regexp', function(t) {
nock.enableNetConnect();
});

test('repeating response 2 times', function(t) {
test('repeating once', function(t) {
nock.disableNetConnect();

var _mock = nock('https://zombo.com')
.get('/')
.times(2)
.once()
.reply(200, "Hello World!");

http.get('https://zombo.com', function(res) {
t.equal(200, res.statusCode, 'first request');
});

nock.cleanAll()
t.end();

http.get('https://zombo.com', function(res) {
t.equal(200, res.statusCode, 'second request');
});
nock.enableNetConnect();
});

test('repeating twice', function(t) {
nock.disableNetConnect();

var _mock = nock('https://zombo.com')
.get('/')
.twice()
.reply(200, "Hello World!");

for (var i=0; i < 2; i++) {
http.get('https://zombo.com', function(res) {
t.equal(200, res.statusCode, 'first request');
});
};

nock.cleanAll()
t.end();

nock.enableNetConnect();
});

test('repeating thrice', function(t) {
nock.disableNetConnect();

var _mock = nock('https://zombo.com')
.get('/')
.thrice()
.reply(200, "Hello World!");

for (var i=0; i < 3; i++) {
http.get('https://zombo.com', function(res) {
t.equal(200, res.statusCode, 'first request');
});
};

nock.cleanAll()
t.end();

nock.enableNetConnect();
});

test('repeating response 4 times', function(t) {
nock.disableNetConnect();

var _mock = nock('https://zombo.com')
.get('/')
.times(4)
.reply(200, "Hello World!");

for (var i=0; i < 4; i++) {
http.get('https://zombo.com', function(res) {
t.equal(200, res.statusCode, 'first request');
});
};

nock.cleanAll()
t.end();
Expand Down

0 comments on commit 89d21fb

Please sign in to comment.