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

servertest-dv

v1.2.3

Published

A simple HTTP server testing tool

Downloads

9

Readme

servertest

A simple HTTP server testing tool

NPM NPM

Why?

servertest exists because supertest does way too much and gets in the way when you need to do anything novel (the typical "framework" problem).

servertest doesn't do any assertions for you, you simply hand it an HTTP server and it manages the start / stop lifecycle and makes a request for you, passing you back the results. servertest is built on hyperquest as an HTTP client and supports the same options as hyperquest and will stream if you need to (in and/or out).

Examples

var server = http.createServer(function (req, res) {
  res.end('OK')
})

test('simple web server', function (t) {
  servertest(server, '/', { encoding: 'utf8' }, function (err, res) {
    t.ifError(err, 'no error')
    t.equal(res.statusCode, 200, 'correct statusCode')
    t.equal(res.body, 'OK', 'correct body content')
    t.end()
  })
})

Even JSON encoding can be handled for you:

var server = http.createServer(function (req, res) {
  res.end(JSON.stringify({ ok: 'mate' }))
})

test('json web server', function (t) {
  servertest(server, '/', { encoding: 'json' }, function (err, res) {
    t.ifError(err, 'no error')
    t.equal(res.statusCode, 200, 'correct statusCode')
    t.deepEqual(res.body, { ok: 'mate' }, 'correct body content')
    t.end()
  })
})

And of course it's streams all the way:

// uppercasing server, post it a string and it'll return
// an uppercased version of it
var server = http.createServer(function (req, res) {
  req.pipe(through2(function (chunk, enc, callback) {
    callback(null, chunk.toString().toUpperCase())
  })).pipe(res)
})

test('duplex uppercasing server', function (t) {
  // servertest is a duplex stream when posting data
  var serverStream = servertest(server, '/', { method: 'POST' })

  // pipe data to the POST request
  fs.createReadStream(__filename).pipe(serverStream)

  // pipe data from the response
  serverStream.pipe(bl(function (err, data) {
    t.ifError(err, 'no error')

    var expected = fs.readFileSync(__filename, 'utf8').toUpperCase()
    t.equal(data.toString(), expected, 'uppercased data')
    t.end()
  }))
})

Of course this assumes that you have easy access to your http.Server object in your tests. Normally you will want to expose it on your server directly on your main server start script and not perform the listen() yourself if it's not being run as the "main":

index.js

module.exports = function () {
  return http.createServer(handler)
}

if (require.main === module) {
  module.exports().listen(port, function (err) {
    console.log('Server started on port %d', port)
  })
}

Then you can do this:

test.js

var server = require('./index')

test('test server', function (t) {
  servertest(server(), '/path/to/test', function (err, data) {
  	// ...
  })
})

Or you could reuse the same server object but that's probably not so savoury for testing.

API

servertest(server, uri, options, callback)

Full arguments form, taking an http.Server instance (or similar object that performs a listen()), the uri to append to http://localhost and the random port number assigned on the listen(), the options object which is mostly passed on to hyperquest. The callback function will receive either an Error as the first argument or a special response object that contains data about the response, see below

servertest(server, uri, callback)

The options argument is optional, default options will be used for hyperquest, including assuming this is a GET request.

var stream = servertest(server, uri, { method: 'POST' }, callback)

A common POST request form whereby you have a WritableStream you can write data to (either via a pipe() or simply write() and end()).

var stream = servertest(server, uri, { method: 'POST' })

Don't use a callback function to receive the data. Instead, the stream is a DuplexStream which has some metadata on the stream object (including the request object direct from hyperquest) and you will need to pipe() it to a WritableStream (or read() or on('data'), whatever you prefer).

options

  • 'encoding': the only option servertest currently cares about. If you provide 'utf8' the callback will receive a String rather than a Buffer. If you provide 'json' the data received from the server will be passed through JSON.parse() and any exceptions will be returned as the Error argument to the callback.

hyperquest uses the following options:

  • 'method': request method, defaults to 'GET'
  • 'headers': an Object ({}) defining headers to set on the request
  • 'auth': if HTTP authentication is required, must be of the form 'user:pass'

Plus a bunch more for HTTPS.

response

The callback receives a special response object containing data from the server. It will have the following properties:

  • 'headers': an Object containing a mapping of the header keys and values received from the server
  • 'statusCode': the status code of the response from the server
  • 'body': the response body. By default it will be a Buffer. If you use 'utf8' as the 'encoding' you'll get a String and if you use 'json' as the 'encoding' you'll get whatever JSON.parse() gives you for the response string.

error

when servertest has an encoding error like this case, you can access a response with the default encoding UTF-8 or utf8 from error.response, this should give you, the users, much clue as to what your program went wrong.

License

servertest is Copyright (c) 2014 Rod Vagg @rvagg and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE.md file for more details.