
Research
Security News
Malicious PyPI Package Exploits Deezer API for Coordinated Music Piracy
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
mock-browser
Advanced tools
A mock browser with window, document, location, navigation, local and session storage to use for client side code tests in a node environment.
#Mock Browser
A mock browser with window, document, location, navigation, local and session storage to use for client side code tests in a node environment. A majority of the implementation is from the jsdom project with enhancements for local and session storage plus some fixes for un-implemented document features (classList).
The mock browser eliminates the need for a headless browser like phantomjs to provide a much faster unit test framework. It's perfect for browserify projects that run tests prior to compiling the bundle.
npm install mock-browser --save
If you just use the mock for testing then '--save-dev' is more appropriate. But, the AbstractBrowser could (should) be used for run time to replace client logic that normally accesses globals provided by the local browser. This not only enhances testing and mocking but also enables cross-browser workarounds through interception rather than override.
The typical use case is a browserify project with unit tests that need to provide access to browser globals like window, document, location, etc. The best way to simulate this is to use a proxy wall between the normally global browser objects a require the application to get instances of window, document, etc. That way you can supply a mock for all of these objects with a common interface (AbstractBrowser). It looks like this...
var MockBrowser = require('mock-browser').mocks.MockBrowser;
var mock = new MockBrowser();
// and in the run-code
var doc = mock.getDocument();
var div = doc.createElement('div');
var localStorage = doc.getLocalStorage();
localStorage.setItem('mykey', 'my value');
assert localStorage.getItem('mykey') === 'my value';
Two convenience methods are added to make it easy to get either a window or dom document mock.
The AbstractBrowser object can be used as an interface for run-time client apps to access browser window related objects. This approach enables code to be used both in and outside the browser.
var AbstractBrowser = require('mock-browser').delegates.AbstractBrowser;
// configure in some factory
var opts = {};
if (typeof window === 'object') {
// assign the browser window if it exists
opts.window = window;
} else {
// create a mock window object for testing
opts.window = MockBrowser.createWindow();
}
// create the browser object with a real window in brwosers and mock when not in browser
var browser = new AbstractBrowser( opts );
var doc = browser.getDocument();
var element = doc.getElementById('my-dom-element');
Instance methods...
Object Methods...
These methods are inherited by MockBrowser.
For run-time use you can extend AbstractBrowser to inherit the API. This enables attaching other object to the browser object rather than using globals.
var AbstractBrowser = require('mock-browser').delegates.AbstractBrowser;
var MyBrowser = function(options) {
var browser = this,
builder = options.componentBuilder;
// inherit getWindow(), getDocument(), getLocation(), getHistory(),
// getLocalStorage(), getSessionStorage()
AbstractBrowser.extend( this, options );
// my browser API extension...
this.getComponentBuilder = function() {
return builder;
};
};
The MockStorage object is used to mock out local and session storage. When used with mock browser, they are attached to the window object and can be accessed from AbstractBrowser instances by invoking getLocalStorage() or getSessionStorage().
This object may also be used standalone when you just need to mock out either local or session storage. It looks like this:
var MockStorage = require('mock-browser').mocks.MockStorage;
var storage = new MockStorage();
storage.setItem( 'mykey', 'my string value' );
assert storage.getItem( 'mykey' ) === 'my string value';
assert storage.length === 1;
Coming soon...
All objects are tested using gulp and mocha. You can run tests by doing this:
make test
// or
gulp test
// or
npm test
copyright © 2014 rain city software | version 0.90.17
FAQs
A mock browser with window, document, location, navigation, local and session storage to use for client side code tests in a node environment.
The npm package mock-browser receives a total of 4,101 weekly downloads. As such, mock-browser popularity was classified as popular.
We found that mock-browser demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.
Security News
Newly introduced telemetry in devenv 1.4 sparked a backlash over privacy concerns, leading to the removal of its AI-powered feature after strong community pushback.