npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mock-xhr

v0.1.0

Published

A mock implementation of XMLHttpRequest for unit testing

Downloads

271

Readme

MockHttpRequest

This JavaScript module provides a mock implementation of XMLHttpRequest_ for unit testing. In nearly all ways it behaves like a regular XMLHttpRequest_ (with support for Progress Events_), except of course it doesn't open a network connection. Instead it exposes the relevant data through the following extra attributes and methods:

method, url, async, user, password These attributes correspond to the arguments of the open() method.

urlParts Object representing individual parts of the URL passed to open(), e.g. protocol, host, port, path, query, anchor. If present, the query string (query) is parsed and its elements are available from the queryKey property.

requestText This attribute will contain the request body that was passed to the send() method.

getRequestHeader(header) Returns a request header set by the client.

setResponseHeader(header, value) Sets a response header. This should be called before calling the receive() method.

receive(status, data) Send data back to the client. status is an integer representing the HTTP status code (e.g. 200) and data is the response body (string).

err(exception) Simulate a request error, such as NETWORK_ERR.

authenticate(user, password) Verify HTTP credentials passed to open(), inserted into the URL, or sent via the Authorization header (in that order).

Simple example

Let's look at a brief example. Imagine you have this piece of code that wants to make an AJAX connection::

$ var request = new MockHttpRequest(); $ request.open("POST", "http://some.host/path"); $ request.setRequestHeader("Content-Type", "application/robot"); $ request.onload = function () {

print("Received response: " + this.statusText);
print("Response body: " + this.responseText);

}; $ request.send("Hey meatbag!");

The request data will be set accordingly::

$ request.method "POST" $ request.url "http://some.host/path" $ request.async true

$ request.requestText "Hey meatbag!" $ request.getRequestHeader("Content-Type") "application/robot"

We can now simulate sending data back to the client::

$ request.receive(404, "Sorry, I have no idea what you're talking about."); Received response: 404 Not Found Response body: Sorry, I have no idea what you're talking about.

MockHttpServer for tests

Often you can't just change the client code to use MockHttpRequest for tests. In such cases, you can use MockHttpServer to intercept all instances of XMLHttpRequest.

For example, imagine you were trying to test a function called makeAJAXCall() that relied on getting information through XMLHttpRequest. First let's set up a server instance and define a request handler in which we mimick the server application and return whatever data the server would normally return::

var server = new MockHttpServer(); server.handle = function (request) { request.setResponseHeader("Content-Type", "application/robot"); request.receive(200, "I am Bender, please insert girder!"); };

Now we can switch on the "server" to divert all XMLHttpRequests to our handler. Then we call the function we want to test. Finally we must not forget to clean up by stopping the server::

server.start(); makeAJAXCall(); server.stop();

About

MockHttpRequest was written by Philipp von Weitershausen. It is released under the MIT License.

.. _XMLHttpRequest: http://www.w3.org/TR/XMLHttpRequest .. _Progress Events: http://www.w3.org/TR/progress-events/ .. _Philipp von Weitershausen: mailto:[email protected] .. _MIT License: http://www.opensource.org/licenses/mit-license.php