Skip to content

Commit 1b1755c

Browse files
committed
Add proxy failures option for debug
1 parent c40a9cc commit 1b1755c

5 files changed

Lines changed: 34 additions & 13 deletions

File tree

bin/vcr.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,24 @@ var argv = require('yargs')
2222
.alias('r', 'record')
2323
.describe('r', 'Record proxied responses to fixtures dir')
2424

25+
.boolean('proxyFailedResponses')
26+
.alias('a', 'proxyFailedResponses')
27+
.describe('a', 'Record and proxy response independently on status code, including 5xx, 4xx, 3xx. Nice for debug.')
28+
2529
.default('port', 8100)
2630

2731
.example('-f ./fixtures -p https://ur.l/base -r', 'Load fixtures from directory, proxy not found fixtures to ur.l/base and success responses record back to fixtures directory')
2832
.argv;
2933

3034
var app = express();
3135
var fixturesDir = path.join(process.cwd(), argv.fixturesDir);
36+
var options = {
37+
realApiBaseUrl: argv.proxy,
38+
outputDir: argv.record && fixturesDir,
39+
proxyFailedResponses: argv.proxyFailedResponses,
40+
};
3241

33-
app.use(server([fixturesDir], argv.proxy, argv.record && fixturesDir))
42+
app.use(server([fixturesDir], options))
3443
app.listen(argv.port, function(err) {
3544
if (err) {
3645
return console.error(err);

src/__tests__/server.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ afterAll(async () => {
2424
});
2525

2626
describe('Stub server', () => {
27-
const app = server([path.join(__dirname, 'fixtures')], 'https://jsonplaceholder.typicode.com');
27+
const app = server([path.join(__dirname, 'fixtures')], {realApiBaseUrl: 'https://jsonplaceholder.typicode.com'});
2828

2929
it('should respond with index', async () =>
3030
await request(app)
@@ -213,7 +213,7 @@ describe('Stub server in proxy mode', async () => {
213213
});
214214

215215
it('should proxy requests and keep correct encoding - gzip', async () => {
216-
const appserver = server(fixtureDirs, 'http://localhost:5000', outputFixturesDir);
216+
const appserver = server(fixtureDirs, {realApiBaseUrl: 'http://localhost:5000', outputDir: outputFixturesDir});
217217
await request.agent(appserver)
218218
.get('/mocked')
219219
.set('accept-encoding', 'gzip')
@@ -223,7 +223,7 @@ describe('Stub server in proxy mode', async () => {
223223
});
224224

225225
it('should proxy requests and keep correct encoding - deflate', async () => {
226-
const appserver = server(fixtureDirs, 'http://localhost:5000', outputFixturesDir);
226+
const appserver = server(fixtureDirs, {realApiBaseUrl: 'http://localhost:5000', outputDir: outputFixturesDir});
227227
await request.agent(appserver)
228228
.get('/mocked')
229229
.set('accept-encoding', 'deflate')
@@ -233,7 +233,7 @@ describe('Stub server in proxy mode', async () => {
233233
});
234234

235235
it('should proxy requests and keep correct encoding - gzip, deflate', async () => {
236-
const appserver = server(fixtureDirs, 'http://localhost:5000', outputFixturesDir);
236+
const appserver = server(fixtureDirs, {realApiBaseUrl: 'http://localhost:5000', outputDir: outputFixturesDir});
237237
await request.agent(appserver)
238238
.get('/mocked')
239239
.set('accept-encoding', 'deflate, gzip')
@@ -243,7 +243,7 @@ describe('Stub server in proxy mode', async () => {
243243
});
244244

245245
it('should save correctly decoded fixture to fixturePath ', async () => {
246-
const appserver = server(fixtureDirs, 'http://localhost:5000', outputFixturesDir);
246+
const appserver = server(fixtureDirs, {realApiBaseUrl: 'http://localhost:5000', outputDir: outputFixturesDir});
247247

248248
await request.agent(appserver)
249249
.get('/mocked')
@@ -261,7 +261,7 @@ describe('Stub server in proxy mode', async () => {
261261
});
262262

263263
it('should proxy and save custom variant when cookie record_fixture_variant is set but default fixture is present', async () => {
264-
const appserver = server(fixtureDirs, 'http://localhost:5000', outputFixturesDir);
264+
const appserver = server(fixtureDirs, {realApiBaseUrl: 'http://localhost:5000', outputDir: outputFixturesDir});
265265

266266
await request.agent(appserver)
267267
.get('/mocked')

src/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ const app = express();
77
const PORT = process.env.PORT || 3000;
88

99
// const realApiUrl = 'https://js-developer-second-round.herokuapp.com/api/v1';
10-
const realApiUrl = 'http://localhost:5000';
1110

12-
app.use(server([path.join(__dirname, 'fixtures')], realApiUrl, path.join(__dirname, 'generatedFixtures')));
11+
const options = {
12+
realApiBaseUrl: 'http://localhost:5000',
13+
outputDir: path.join(__dirname, 'generatedFixtures'),
14+
proxyFailedResponses: false,
15+
};
16+
17+
app.use(server([path.join(__dirname, 'fixtures')], options));
1318

1419
app.listen(PORT, function(err: Error) {
1520
if (err) {

src/middlewares/proxy/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import writeFixture from './writeFixture';
77
import {IncomingMessage} from 'http';
88
import {Request, Response, NextFunction, RequestHandler} from 'express';
99

10-
export default (realApiBaseUrl: string, outputDir?: string): RequestHandler =>
10+
export default (realApiBaseUrl: string, outputDir?: string, proxyFailedResponses?: boolean): RequestHandler =>
1111
(req: Request, res: Response, next: NextFunction): void => {
1212
if (req.path === '/') return next();
1313

@@ -19,7 +19,8 @@ export default (realApiBaseUrl: string, outputDir?: string): RequestHandler =>
1919
.on('error', (e: Error) => next(e))
2020
.on('response', (proxyRes: IncomingMessage) => {
2121
// response from real API, if not OK, pass control to next
22-
if (!proxyRes.statusCode || proxyRes.statusCode < 200 || proxyRes.statusCode >= 300) {
22+
const isFailStatusCode = !proxyRes.statusCode || proxyRes.statusCode < 200 || proxyRes.statusCode >= 300;
23+
if (!proxyFailedResponses && isFailStatusCode) {
2324
console.log(`${chalk.magenta('[Stub server]')} proxy request to ${chalk.yellow(realApiBaseUrl + req.path)} ended up with ${chalk.red(`${proxyRes.statusCode}`)}`);
2425
return next();
2526
}

src/server.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ import {Endpoint, extractEndpoints} from './endpoints';
1111
import {findEndpoint, findFixture, extract, extractVariantsFromRequest} from './matcher';
1212
import {Request, Response, NextFunction} from 'express';
1313

14-
export default (fixtureDirs: string[] = [], realApiBaseUrl?: string, outputDir?: string) => {
14+
interface Options {
15+
realApiBaseUrl?: string;
16+
outputDir?: string;
17+
proxyFailedResponses?: boolean;
18+
}
19+
20+
export default (fixtureDirs: string[] = [], { realApiBaseUrl, outputDir, proxyFailedResponses }: Options = {}) => {
1521
const app = express();
1622
app.use(cors());
1723
app.use(cookieParser());
@@ -74,7 +80,7 @@ export default (fixtureDirs: string[] = [], realApiBaseUrl?: string, outputDir?:
7480

7581
// Proxy to real API
7682
if (realApiBaseUrl) {
77-
app.use(proxyMiddleware(realApiBaseUrl, outputDir));
83+
app.use(proxyMiddleware(realApiBaseUrl, outputDir, proxyFailedResponses));
7884
}
7985

8086
// Fallback path for displaying all possible endpoints

0 commit comments

Comments
 (0)