
Security News
Node.js TSC Votes to Stop Distributing Corepack
Corepack will be phased out from future Node.js releases following a TSC vote.
An HTTP mocking server based on Nock.
Hock is an HTTP mocking server with an API designed to closely match that of Nock. The key difference between Nock and Hock is that nock works by overriding http.clientRequest
, allowing requests to be intercepted before they go over the wire.
Hock is designed as a fully functioning HTTP service. You enqueue requests and responses in a similar fashion to Nock:
var hock = require('hock'),
request = require('request');
hock.createHock(function(err, hockServer) {
var port = hockServer.address().port;
hockServer
.get('/some/url')
.reply(200, 'Hello!');
request('http://localhost:' + port + '/some/url', function(err, res, body) {
console.log(body);
});
});
A port can be optionally specified when creating the server:
hock.createHock(12345, function(err, hockServer) {
....
});
Unlike Nock, you create a Hock
server with a callback based factory method. Behind the scenes, this spins up the new HTTP service, and begins listening to requests.
Hock supports the 5 primary HTTP methods at this time:
// Returns a hock Request object
var req = hockServer.get(url, requestHeaders);
// Returns a hock Request object
var req = hockServer.delete(url, requestHeaders);
// Returns a hock Request object
var req = hockServer.post(url, body, requestHeaders);
// Returns a hock Request object
var req = hockServer.put(url, body, requestHeaders);
// Returns a hock Request object
var req = hockServer.head(url, requestHeaders);
All of these methods return an instance of a Request
, a hock object which contains all of the state for a mocked request. To define the response and enqueue into the hockServer
, call either reply
or replyWithFile
on the Request
object:
// returns the current hockServer instance
req.reply(statusCode, body, responseHeaders);
// returns the current hockServer instance
req.replyWithFile(statusCode, filePath, responseHeaders);
You can optionally tell hock to match multiple requests for the same route:
hockServer.put('/path/one', {
foo: 1,
bar: {
baz: true
biz: 'asdf1234'
}
})
.min(4)
.max(10)
.reply(202, {
status: 'OK'
})
Call many
if you need to handle at least one, possibly
many requests:
hockServer.put('/path/one', {
foo: 1,
bar: {
baz: true
biz: 'asdf1234'
}
})
.many() // min 1, max Unlimited
.reply(202, {
status: 'OK'
})
Provide custom min and max options to many
:
hockServer.put('/path/one', {
foo: 1,
bar: {
baz: true
biz: 'asdf1234'
}
})
.many({
min: 4,
max: 10
})
.reply(202, {
status: 'OK'
})
Set infinite number of requests with max(Infinity)
:
hockServer.put('/path/one', {
foo: 1,
bar: {
baz: true
biz: 'asdf1234'
}
})
.max(Infinity)
.reply(202, {
status: 'OK'
})
If you don't care how many or how few requests are served, you can use any
:
hockServer.put('/path/one', {
foo: 1,
bar: {
baz: true
biz: 'asdf1234'
}
})
.any() // equivalent to min(0), max(Infinity)
.reply(202, {
status: 'OK'
})
hockServer.done()
will verify the number of requests fits within the
minimum and maximum constraints specified by min
, max
, many
or any
:
hockServer.get('/').min(2)
request.get('/', function() {
hockServer.done(function(err) {
console.error(err) // error, only made one request
})
})
If the number of requests doesn't verify and you don't supply a callback
to hockServer.done()
it will throw!
As the reply
and replyWithFile
methods return the current hockServer, you can chain them together:
hockServer.put('/path/one', {
foo: 1,
bar: {
baz: true
biz: 'asdf1234'
}
})
.reply(202, {
status: 'OK'
})
.get('/my/file/should/be/here')
.replyWithFile(200, __dirname + '/foo.jpg');
When a request comes in, hock iterates through the queue in a First-in-first-out approach, so long as the request matches. The criteria for matching is based on the method and the url, and additionally the request body if the request is a PUT
or POST
. If you specify request headers, they will also be matched against before sending the response.
You can filter paths using regex or a custom function, this is useful for things like timestamps that get appended to urls from clients.
hockServer
.filteringPathRegEx(/timestamp=[^&]*/g, 'timestamp=123')
.get('/url?timestamp=123')
.reply(200, 'Hi!');
hockServer
.filteringPath(function (p) {
return '/url?timestamp=XXX';
})
.get('/url?timestamp=XXX')
.reply(200, 'Hi!');
FAQs
A mocking server for HTTP requests
The npm package hock receives a total of 19,627 weekly downloads. As such, hock popularity was classified as popular.
We found that hock demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers 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.
Security News
Corepack will be phased out from future Node.js releases following a TSC vote.
Research
Security News
Research uncovers Black Basta's plans to exploit package registries for ransomware delivery alongside evidence of similar attacks already targeting open source ecosystems.
Security News
Oxlint's beta release introduces 500+ built-in linting rules while delivering twice the speed of previous versions, with future support planned for custom plugins and improved IDE integration.