Socket
Socket
Sign inDemoInstall

create-test-server

Package Overview
Dependencies
74
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 2.2.0 to 2.3.0

3

package.json
{
"name": "create-test-server",
"version": "2.2.0",
"version": "2.3.0",
"description": "Creates a minimal Express server for testing",

@@ -39,2 +39,3 @@ "main": "src/index.js",

"dependencies": {
"body-parser": "^1.18.2",
"create-cert": "^1.0.2",

@@ -41,0 +42,0 @@ "express": "^4.15.3",

@@ -42,4 +42,4 @@ # create-test-server

// You can return a body directly too
server.get('/foo', 'foo');
server.get('/foo', () => 'foo');
server.get('/bar', () => 'foo');
server.get('/foo', 'bar');

@@ -50,2 +50,9 @@ // server.url + '/foo' and server.sslUrl + '/foo' will respond with 'bar'

The following `Content-Type` headers will be parsed and exposed via `req.body`:
- JSON (`application/json`)
- Text (`text/plain`)
- URL-encoded form (`application/x-www-form-urlencoded`)
- Buffer (`application/octet-stream`)
`createTestServer()` has a Promise based API that pairs well with a modern asynchronous test runner such as [AVA](https://github.com/avajs/ava).

@@ -52,0 +59,0 @@

@@ -8,2 +8,3 @@ 'use strict';

const createCert = require('create-cert');
const bodyParser = require('body-parser');

@@ -27,2 +28,6 @@ const createTestServer = opts => createCert(opts && opts.certificate)

app.set('etag', false);
app.use(bodyParser.json({ type: 'application/json' }));
app.use(bodyParser.text({ type: 'text/plain' }));
app.use(bodyParser.urlencoded({ type: 'application/x-www-form-urlencoded' }));
app.use(bodyParser.raw({ type: 'application/octet-stream' }));

@@ -29,0 +34,0 @@ app.caCert = keys.caCert;

@@ -0,1 +1,2 @@

import querystring from 'querystring';
import test from 'ava';

@@ -81,2 +82,62 @@ import got from 'got';

test('server automatically parses JSON request body', async t => {
const server = await createTestServer();
const object = { foo: 'bar' };
server.post('/echo', (req, res) => {
t.deepEqual(req.body, object);
res.end();
});
await got.post(server.url + '/echo', {
headers: { 'content-type': 'application/json' },
body: JSON.stringify(object)
});
});
test('server automatically parses text request body', async t => {
const server = await createTestServer();
const text = 'foo';
server.post('/echo', (req, res) => {
t.deepEqual(req.body, text);
res.end();
});
await got.post(server.url + '/echo', {
headers: { 'content-type': 'text/plain' },
body: text
});
});
test('server automatically parses URL-encoded form request body', async t => {
const server = await createTestServer();
const object = { foo: 'bar' };
server.post('/echo', (req, res) => {
t.deepEqual(req.body, object);
res.end();
});
await got.post(server.url + '/echo', {
headers: { 'content-type': 'application/x-www-form-urlencoded' },
body: querystring.stringify(object)
});
});
test('server automatically parses binary request body', async t => {
const server = await createTestServer();
const buffer = Buffer.from('foo');
server.post('/echo', (req, res) => {
t.deepEqual(req.body, buffer);
res.end();
});
await got.post(server.url + '/echo', {
headers: { 'content-type': 'application/octet-stream' },
body: buffer
});
});
test('opts.certificate is passed through to createCert()', async t => {

@@ -83,0 +144,0 @@ const server = await createTestServer({ certificate: 'foo.bar' });

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc