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

@heise/request-promise-native-record

v1.0.13

Published

Records and plays back responses from remote web services so you can test your code in peace.

Downloads

98

Readme

request-promise-native-record

Build Status Greenkeeper badge Dependency Status JavaScript Style Guide

When writing unit tests for APIs that consume remote web services, your goal is to test your code, not the network or remote web service. request-promise-native-record records the answers of these web services and plays them back later.

If a recording does not exist or the environment variable HTTP_MODE is set to record, HTTP responses are written to files when running your tests. You may want to check these files into your VCS. If recordings are available, they will be used automatically for further test runs. No network traffic will occur from now on.

Installation

yarn add --dev @heise/request-promise-native-record

or

npm install --save-dev @heise/request-promise-native-record

Usage

lib/my-api.js:

const request = require('request-promise-native')

class Api {
  static getFoo() {
    return request.get('http://example.com/')
  }
}

module.exports = Api

test/my-api.js:

const assert = require('assert')
const record = require('@heise/request-promise-native-record')

record.start({folder: '/tmp'})
const Api = require('../lib/my-api')

describe('my description', () => {
  it('should test my api', async () => {
    let response = await Api.getFoo() // 1. call: network request
    let response = await Api.getFoo() // 2. call: read from fs
    assert.ok(response.includes('Example Domain'))
  })
})

Generated file:

$ cat /tmp/14ba12b98882bca3bc00abff8735175a2544a9c1aa64794e85503198d84595b5.json 
"<!doctype html>\n<html>\n<head>[...]"

Storing/Retrieving response headers, status codes and response body

To save the full response instead of just the response body, use the option resolveWithFullResponse. The Authorization header is removed from the headers object before writing to the hard disk.

lib/my-api.js:

const request = require('request-promise-native')

class Api {
  static getFoo() {
    return request.get('http://example.com/', {resolveWithFullResponse: true})
  }
}

module.exports = Api

Limitations

  • Currently only request-promise-native is supported as HTTP lib.
  • Currently only request.get() is supported.
  • The generated files are stored flat in the specified directory. You may have to clean up the files by yourself.