@loopback/http-caching-proxy
A caching HTTP proxy for integration tests. NOT SUITABLE FOR PRODUCTION USE!
Last updated 2 days ago by rfeng .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @loopback/http-caching-proxy 
SYNC missed versions from official npm registry.

@loopback/http-caching-proxy

A caching HTTP proxy for integration tests.

NOT SUITABLE FOR PRODUCTION USE!

Overview

Testing applications connecting to backend REST/SOAP services can be difficult: The backend service may be slow, apply rate limiting, etc. Integration tests become too slow in such case, which makes test-first development impractical.

This can be addressed by setting up a snapshot-based mock server or using a caching HTTP client, but both of these solutions come with severe disadvantages:

  • When using a snapshot-based mock server, we must ensure that snapshots are up-to-date with the actual backend implementation.

  • Caching at HTTP-client side requires non-trivial changes of the application code.

A filesystem-backed caching HTTP proxy offers a neat solution that combines caching and snapshots:

  • The first request is forwarded to the actual backend and the response is stored as a snapshot.
  • Subsequent requests are served by the proxy using the cached snapshot.
  • Snapshot older than a configured time are discarded and the first next request will fetch the real response from the backend.

Installation

npm install --save-dev  @loopback/http-caching-proxy

Basic use

Import the module at the top of your test file.

import {HttpCachingProxy} from '@loopback/http-caching-proxy';

Create a proxy instance during test-suite setup (typically in Mocha's before hook):

const proxy = new HttpCachingProxy({
  // directory where to store recorded snapshots - required
  cachePath: path.resolve(__dirname, '.proxy-cache'),
  // port where to listen - 0 by default
  port: 0,
  // how often to re-validate snapshots (in milliseconds) - one day by default
  ttl: 24 * 60 * 60 * 1000,
});
await proxy.start();

In your tests, configure the client library to use the caching proxy. Below is an example configuration for axios:

const parsed = new URL(proxy.url);
const proxyConfig = {
  host: parsed.hostname,
  port: parseInt(parsed.port),
  protocol: parsed.protocol,
  auth: {
    username: parsed.username,
    password: parsed.password,
  },
};
const request = axios.create({
  // Axios does not support proxy url directly
  proxy: proxyConfig,
});

Finally, stop the proxy when the test suite is done (typically in Mocha's after hook):

await proxy.stop();

API Documentation

See the auto-generated documentation at loopback.io

Alternative solutions for HTTP-based integration testing

A caching proxy is great if you want your tests to talk to the real backend service. There are many cases where such behavior is not desirable and the tests must run fully isolated. If that's your situation, then please consider using a tool that can record and replay HTTP interactions, for example:

Just make sure you have a process in place to verify that your recorded interactions are staying up to date with the actual behavior of the backend service!

Contributions

Tests

Run npm test from the root folder.

Contributors

See all contributors.

License

MIT

Current Tags

  • 0.3.0                                ...           dp3 (2 years ago)
  • 2.1.6                                ...           latest (2 days ago)

81 Versions

  • 2.1.6                                ...           2 days ago
  • 2.1.5                                ...           11 days ago
  • 2.1.4                                ...           11 days ago
  • 2.1.3                                ...           23 days ago
  • 2.1.2                                ...           a month ago
  • 2.1.1                                ...           a month ago
  • 2.1.0                                ...           a month ago
  • 2.0.4                                ...           2 months ago
  • 2.0.3                                ...           2 months ago
  • 2.0.2                                ...           2 months ago
  • 2.0.1                                ...           2 months ago
  • 2.0.0                                ...           3 months ago
  • 1.3.0                                ...           4 months ago
  • 1.2.5                                ...           4 months ago
  • 1.2.4                                ...           5 months ago
  • 1.2.3                                ...           6 months ago
  • 1.2.2                                ...           6 months ago
  • 1.2.1                                ...           7 months ago
  • 1.2.0                                ...           7 months ago
  • 1.1.16                                ...           8 months ago
  • 1.1.15                                ...           8 months ago
  • 1.1.14                                ...           8 months ago
  • 1.1.13                                ...           8 months ago
  • 1.1.12                                ...           9 months ago
  • 1.1.11                                ...           9 months ago
  • 1.1.10                                ...           9 months ago
  • 1.1.9                                ...           9 months ago
  • 1.1.8                                ...           10 months ago
  • 1.1.7                                ...           10 months ago
  • 1.1.6                                ...           10 months ago
  • 1.1.5                                ...           a year ago
  • 1.1.4                                ...           a year ago
  • 1.1.3                                ...           a year ago
  • 1.1.2                                ...           a year ago
  • 1.1.1                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.22                                ...           a year ago
  • 1.0.21                                ...           a year ago
  • 1.0.20                                ...           a year ago
  • 1.0.19                                ...           a year ago
  • 1.0.18                                ...           a year ago
  • 1.0.17                                ...           a year ago
  • 1.0.16                                ...           a year ago
  • 1.0.15                                ...           a year ago
  • 1.0.14                                ...           a year ago
  • 1.0.13                                ...           a year ago
  • 1.0.12                                ...           a year ago
  • 1.0.11                                ...           a year ago
  • 1.0.10                                ...           a year ago
  • 1.0.9                                ...           a year ago
  • 1.0.8                                ...           a year ago
  • 1.0.7                                ...           a year ago
  • 1.0.6                                ...           a year ago
  • 1.0.5                                ...           a year ago
  • 1.0.4                                ...           a year ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 0.3.14                                ...           2 years ago
  • 0.3.13                                ...           2 years ago
  • 0.3.12                                ...           2 years ago
  • 0.3.11                                ...           2 years ago
  • 0.3.10                                ...           2 years ago
  • 0.3.9                                ...           2 years ago
  • 0.3.8                                ...           2 years ago
  • 0.3.7                                ...           2 years ago
  • 0.3.6                                ...           2 years ago
  • 0.3.5                                ...           2 years ago
  • 0.3.4                                ...           2 years ago
  • 0.3.3                                ...           2 years ago
  • 0.3.2                                ...           2 years ago
  • 0.3.1                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.7                                ...           2 years ago
  • 0.2.6                                ...           2 years ago
  • 0.2.5                                ...           2 years ago
  • 0.2.4                                ...           2 years ago
  • 0.2.3                                ...           2 years ago
  • 0.2.2                                ...           2 years ago
  • 0.2.1                                ...           2 years ago
Downloads
Today 0
This Week 103
This Month 161
Last Day 1
Last Week 29
Last Month 70
Dependencies (5)
Dev Dependencies (9)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |