New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fake-http-request

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fake-http-request - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

.travis.yml

13

index.js
/*global require, module */
var createFakeRequest = require('./src/create-fake-request'),
oldRequests = {},
installFake = function (type) {
installFake = function (options) {
'use strict';
var target = type || 'https',
requestModule = require(target);
if (typeof options === 'object' && (options.type || options.matcher)) {
var target = options.type || 'https';
var matcher = options.matcher;
} else {
var target = options || 'https';
}
var requestModule = require(target);
if (oldRequests[target]) {

@@ -12,3 +17,3 @@ throw new Error('Fake HTTP request is already installed in ' + target);

oldRequests[target] = requestModule.request;
requestModule.request = createFakeRequest();
requestModule.request = createFakeRequest(matcher, oldRequests[target]);
},

@@ -15,0 +20,0 @@ uninstallFake = function (type) {

{
"name": "fake-http-request",
"version": "1.3.0",
"version": "1.4.0",
"description": "Utility class to fake HTTP/HTTPS requests for unit testing Node.js projects. It captures arguments for outgoing requests and allows you to simulate network errors and responses easily.",

@@ -5,0 +5,0 @@ "scripts": {

# Fake Node.js HTTP Request
[![Build status](https://travis-ci.org/gojko/fake-http-request.svg?v=1)](https://travis-ci.org/gojko/fake-http-request)
Utility class to fake a HTTP/HTTPS request for unit testing Node.js projects. It captures arguments for outgoing requests and allows you to simulate network errors and responses easily. It can also pipe outgoing HTTP/S requests to custom functions, so you can asynchronously wait for them.
[![NPM](https://nodei.co/npm/fake-http-request.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/fake-http-request/)`
## Installation
Install using NPM, with
```
npm install fake-http-request
npm install fake-http-request -D
```

@@ -40,3 +45,3 @@

You can also use `https.request.pipe` to pass in a function that will receive a call every time a network request is initiated.
You can also use `https.request.pipe` to pass in a function that will receive a call every time a network request is initiated. The call is executed using `setTimeout` so you can also respond, knowing that the synchronous processing of the calling function is complete. The arguments to the pipe will be the arguments passed to the HTTP call, and `this` will be set to the fake request.

@@ -85,11 +90,26 @@ ### Example

https.request.pipe(function (options) {
console.log('Received call',
https.request.calls[0].args[0].host,
https.request.calls[0].args[0].port,
https.request.calls[0].args[0].path
);
console.log('Received call', options);
this.respond(200, 'OK', 'some html here');
});
request('https://www.google.com');
```
```
### Usage with domain matchers
In case you want to block just a certain URLs, you can pass an object with request type (`type`) and regex matcher to the `install` method. If matcher is not provided, `fake-http-request` will match all URLs. Request type is optional and it defaults to `https`.
For example, this will fake only requests made to Github URL via HTTPS:
```javascript
var fakeRequest = require('fake-http-request');
fakeRequest.install({
type: 'https',
matcher: /github/
});
// Do something with fake requests
fakeRequest.uninstall('https');
```

@@ -8,3 +8,6 @@ /* global require, it, describe, beforeEach, afterEach, expect, jasmine */

beforeEach(function () {
fakeRequest.install('https');
fakeRequest.install({
type: 'https',
matcher: /google/
});
});

@@ -43,2 +46,23 @@ afterEach(function () {

});
it('can fake only the requests matched with matcher if provided', function (done) {
var numOfHttpRequests = https.request.calls.length;
request('https://www.github.com', function() {
expect(https.request.calls.length).toBe(numOfHttpRequests);
done();
}).on('error', done.fail);
});
it('fakes only the requests it can match', function (done) {
request('https://www.google.com', function() {
expect(https.request.calls.length).toBe(1);
request('https://www.npmjs.com', function () {
expect(https.request.calls.length).toBe(1);
done();
});
}).on('request', function () {
var numOfHttpRequests = https.request.calls.length
numOfHttpRequests &&
typeof https.request.calls[numOfHttpRequests - 1].respond === 'function' &&
https.request.calls[numOfHttpRequests - 1].respond(200, 'OK', 'Hello there');
}).on('error', done.fail);
});
});

@@ -30,2 +30,10 @@ /*global describe, require, beforeEach, afterEach, it, expect*/

});
it('can receive config as an object', function () {
fakeRequest.install({
type: 'http'
});
expect(originalHttpRequest).not.toBe(http.request);
expect(originalHttpsRequest).toBe(https.request);
fakeRequest.uninstall('http');
})
it('refuses to install twice', function () {

@@ -32,0 +40,0 @@ fakeRequest.install();

/*global module, require, setTimeout */
var FakeCall = require('./fake-call');
module.exports = function createFakeRequest() {
module.exports = function createFakeRequest(matcher, passthrough) {
'use strict';
var calls = [],
pipes = [],
result = function () {
result = function (request) {
if (matcher && !matcher.test(request.href)) {
return passthrough.apply(this, arguments);
}
var argsArray = [].slice.apply(arguments),

@@ -17,2 +20,6 @@ listeners = {},

},
once: function (eventName, listener) {
listeners[eventName] = listener;
return fake;
},
end: function () {

@@ -19,0 +26,0 @@ return fake;

@@ -13,2 +13,5 @@ /*global module */

},
once: function (eventName, listener) {
responseListeners[eventName] = listener;
},
statusCode: statusCode,

@@ -15,0 +18,0 @@ statusMessage: statusMessage

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc