Socket
Socket
Sign inDemoInstall

fastify

Package Overview
Dependencies
Maintainers
2
Versions
288
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify - npm Package Compare versions

Comparing version 0.17.0 to 0.18.0

test/inject.test.js

4

docs/Server-Methods.md

@@ -90,1 +90,5 @@ <h1 align="center">Fastify</h1>

The logger instance, check [here](https://github.com/fastify/fastify/blob/master/docs/Logging.md).
<a name="inject"></a>
#### inject
Fake http injection (for testing purposes) [here](https://github.com/fastify/fastify/blob/master/docs/Testing.md#inject).

@@ -71,5 +71,2 @@ <h1 align="center">Fastify</h1>

t.error(err)
// Will allow the program to exit if this is
// the only active server in the event system.
fastify.server.unref()

@@ -88,2 +85,3 @@ test('The server should start', t => {

t.deepEqual(JSON.parse(body), { hello: 'world' })
fastify.close()
})

@@ -93,1 +91,88 @@ })

```
<a name="inject"></a>
### Testing with http injection
Fastify supports fake http injection thanks to [shot](https://github.com/hapijs/shot).
You just need to use the api `inject`:
```js
fastify.inject({
method: String,
url: String,
payload: Object,
headers: Object
}, response => {
// your tests
})
```
Example:
```js
// server.js
const minimist = require('minimist')
const fastify = require('fastify')()
const schema = {
out: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
fastify.get('/', schema, function (request, reply) {
reply.send({ hello: 'world' })
})
function start (opts, callback) {
fastify.listen(opts.port, function (err) {
callback(err, fastify)
})
}
// In this way you can run the server both from the CLI and as a required module.
if (require.main === module) {
// Run the server with:
// $ node server.js -p 8080
start(minimist(process.argv.slice(2), {
integer: ['port'],
alias: {
port: 'p'
},
default: {
port: 3000
}
}), (err, instance) => {
if (err) throw err
console.log(`server listening on ${instance.server.address().port}`)
})
}
// note that now we are also exposing the fastify instance
module.exports = { start, fastify }
```
```js
// test.js
const t = require('tap')
const test = t.test
const request = require('request')
const fastify = require('./server').fastify
test('GET `/` route', t => {
t.plan(3)
fastify.inject({
method: 'GET',
url: '/'
}, res => {
t.strictEqual(res.statusCode, 200)
t.strictEqual(res.headers['content-length'], '' + body.length)
t.deepEqual(JSON.parse(res.payload), { hello: 'world' })
// even if the server is not running (inject does not run the server)
// at the end of your tests is highly recommended call `.close()`,
// in this way you will close all the connections to external services
fastify.close()
})
})
```

63

fastify.js

@@ -10,2 +10,3 @@ 'use strict'

const fastseries = require('fastseries')
const shot = require('shot')

@@ -47,2 +48,8 @@ const Reply = require('./lib/reply')

// true when Fastify is ready to go
var started = false
app.on('start', () => {
started = true
})
var server

@@ -99,2 +106,5 @@ if (options.https) {

// fake http injection (for testing purposes)
fastify.inject = inject
return fastify

@@ -179,4 +189,4 @@

instance = Object.create(instance)
instance._Reply = buildReply(instance._Reply)
instance._Request = buildRequest(instance._Request)
instance._Reply = Reply.buildReply(instance._Reply)
instance._Request = Request.buildRequest(instance._Request)
instance._contentTypeParser = ContentTypeParser.buildContentTypeParser(instance._contentTypeParser)

@@ -245,3 +255,12 @@ instance._hooks = Hooks.buildHooks(instance._hooks)

}
return route({ method, url, schema, handler, Reply: self._Reply, Request: self._Request, contentTypeParser: self._contentTypeParser, hooks: self._hooks })
return route({
method,
url,
schema,
handler,
Reply: self._Reply,
Request: self._Request,
contentTypeParser: self._contentTypeParser,
hooks: self._hooks
})
}

@@ -318,2 +337,14 @@

function inject (opts, cb) {
if (started) {
shot.inject(this, opts, cb)
return
}
this.ready(err => {
if (err) throw err
shot.inject(this, opts, cb)
})
}
function addHook (name, fn) {

@@ -333,28 +364,2 @@ this._hooks.add(name, fn)

// TODO: find a better solution than
// copy paste the code of the constructor
function buildReply (R) {
function _Reply (req, res, handle) {
this.res = res
this.handle = handle
this._req = req
this.sent = false
this._serializer = null
}
_Reply.prototype = new R()
return _Reply
}
function buildRequest (R) {
function _Request (params, req, body, query, log) {
this.params = params
this.req = req
this.body = body
this.query = query
this.log = log
}
_Request.prototype = new R()
return _Request
}
function defaultRoute (req, res, params) {

@@ -361,0 +366,0 @@ res.statusCode = 404

@@ -117,2 +117,15 @@ /* eslint-disable no-useless-return */

function buildReply (R) {
function _Reply (req, res, handle) {
this.res = res
this.handle = handle
this._req = req
this.sent = false
this._serializer = null
}
_Reply.prototype = new R()
return _Reply
}
module.exports = Reply
module.exports.buildReply = buildReply

@@ -11,2 +11,15 @@ 'use strict'

function buildRequest (R) {
function _Request (params, req, body, query, log) {
this.params = params
this.req = req
this.body = body
this.query = query
this.log = log
}
_Request.prototype = new R()
return _Request
}
module.exports = Request
module.exports.buildRequest = buildRequest
{
"name": "fastify",
"version": "0.17.0",
"version": "0.18.0",
"description": "Fast and low overhead web framework, for Node.js",

@@ -37,4 +37,4 @@ "main": "fastify.js",

"bluebird": "^3.5.0",
"cors": "^2.8.2",
"coveralls": "^2.12.0",
"cors": "^2.8.3",
"coveralls": "^2.13.1",
"dns-prefetch-control": "^0.1.0",

@@ -45,4 +45,4 @@ "express": "^4.15.2",

"frameguard": "^3.0.0",
"hapi": "^16.1.0",
"helmet": "^3.5.0",
"hapi": "^16.1.1",
"helmet": "^3.6.0",
"hide-powered-by": "^1.0.0",

@@ -52,10 +52,10 @@ "hsts": "^2.0.0",

"koa": "^2.2.0",
"pino": "^4.2.3",
"pino": "^4.5.1",
"pre-commit": "^1.2.2",
"request": "^2.81.0",
"snazzy": "^6.0.0",
"snazzy": "^7.0.0",
"split2": "^2.1.1",
"standard": "^10.0.1",
"standard": "^10.0.2",
"take-five": "^1.3.3",
"tap": "^10.3.0",
"tap": "^10.3.2",
"then-sleep": "^1.0.1",

@@ -65,13 +65,13 @@ "x-xss-protection": "^1.0.0"

"dependencies": {
"ajv": "^4.11.5",
"ajv": "^5.0.1",
"avvio": "^0.6.1",
"fast-json-stringify": "^0.10.4",
"fast-json-stringify": "^0.11.0",
"fast-safe-stringify": "^1.1.13",
"fastseries": "^1.7.2",
"find-my-way": "^0.2.0",
"find-my-way": "^0.2.2",
"middie": "^0.2.0",
"pino-http": "^2.4.2",
"pino-http": "^2.6.1",
"pump": "^1.0.2",
"wayfarer": "^6.5.0"
"shot": "^3.4.0"
}
}

@@ -11,3 +11,3 @@ <div align="center">

[![NPM version](https://img.shields.io/npm/v/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify)
[![NPM downloads](https://img.shields.io/npm/dm/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify)
[![NPM downloads](https://img.shields.io/npm/dm/fastify.svg?style=flat)](https://www.npmjs.com/package/fastify) [![Gitter](https://badges.gitter.im/gitterHQ/gitter.svg)](https://gitter.im/fastify)
</div>

@@ -14,0 +14,0 @@ <br />

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