npm install nock
Usage
Example 1 : Access homepage of Google
In this example we are intercepting a GET request to a given host (http://www.google.com) + path ('/') and responding with a desired response code and contents.
var nock = require('nock');
var response = nock('http://www.google.com')
.get('/')
.reply(200, 'Hello Google');
console.log(response);
The Above code will give us an output as below
Example 2 : Access an API
In this example we are intercepting a GET request to a given host (http://jsonplaceholder.typicode.com) + path ('/comments?postId=1') and responding with a desired response code and contents.
var nock = require('nock');
var response = nock('http://jsonplaceholder.typicode.com')
.get('/comments?postId=1')
.reply(200, 'Hello JsonPlaceHolder');
console.log(response);
The Above code will give us an output as below
In short nock intercepts the requests and throws back what you want. This post is a very basic post of nock. If you need any more information feel free to contact us. Please Like and Share the CodingDefined Blog, if you find it interesting and helpful.
No comments:
Post a Comment