🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

fixture-middleware

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fixture-middleware

Use fixture data in your express app!

1.0.1
latest
Source
npm
Version published
Weekly downloads
1
-50%
Maintainers
1
Weekly downloads
 
Created
Source

fixture-middleware — Use fixture data in your express app!

Motivation

Sometimes I want to mock certain paths of my API made with Express. Therefore, I created fixture-middleware which will use fixture data (mock) for any URL that matches a file inside the configured fixture directory.

Installation

npm install fixture-middleware --save-dev
yarn add fixture-middleware --dev

Usage

const path = require('path')
const express = require('express')
const fixtureMiddleware = require('fixture-middleware')

const app = express()
const fixtureDirectory = path.resolve(__dirname, './fixtures')

app.use(fixtureMiddleware(fixtureDirectory))

Matching URLs with fixture files

All URLs are normalized (all / will be replaced by --) and if a Accept headers is present it will be used to determine the file extension, otherwise the first matching file will be returned.

Example

Suppose we have the following files in the configured fixture directory:

`-- fixtures
    |-- users.json
    |-- users.html
    `-- users--active.json

The following request will match those files as follows

RequestResponse
URL: /users
Headers: (no accept header)
File: users.html
URL: /users
Headers: accept=application.json
File: users.json
URL: /users
Headers: accept=application.html
File: users.html
URL: /users/active
Headers: (no accept header)
File: users--active.json
URL: /users/active
Headers: accept=application.json
File: users--active.json

Important: Any unmatched request will be delegated to the next middleware.

Development

  • Clone and fork this repo.
  • Install dependencies: yarn or npm install.
  • Run tests.
  • Prepare a pull request.

Test

  • yarn test – to run all tests.
  • yarn test -- --watch – to run all tests in watch mode.

Publish

  • Bump package version: yarn version --new-version x.x.x -m 'Version %s.'.
  • Publish to NPM registry: npm publish.
  • Push new tag: git push origin --tags.

Made with :heart: by Rubens Mariuzzo.

MIT license

FAQs

Package last updated on 12 Dec 2017

Did you know?

Socket

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.

Install

Related posts