Comparing version 0.19.0 to 0.19.1
const path = require('path'); | ||
const { | ||
decodedPortRegex, | ||
decodedProtocolRegex, | ||
encodedPortRegex, | ||
encodedProtocolRegex | ||
} = require('./constants'); | ||
function resolveFilePath(capturePath, url) { | ||
@@ -61,2 +68,11 @@ const fileName = url.replace(/\//g, '|'); | ||
// Restore any special protocol or port characters that were possibly tilde-replaced. | ||
const decodeProtocolAndPort = str => | ||
str.replace(encodedProtocolRegex, '$1://').replace(encodedPortRegex, '$1:'); | ||
const encodeProtocolAndPort = str => | ||
str.replace(decodedPortRegex, '$1~').replace(decodedProtocolRegex, '$1~~~'); | ||
exports.decodeProtocolAndPort = decodeProtocolAndPort; | ||
exports.encodeProtocolAndPort = encodeProtocolAndPort; | ||
exports.getDataForRecordToFixtures = getDataForRecordToFixtures; | ||
@@ -63,0 +79,0 @@ exports.replaceFixtureWithRequireInJson = replaceFixtureWithRequireInJson; |
@@ -9,2 +9,3 @@ 'use strict'; | ||
const routeHandler = require('./routeHandler'); | ||
const { decodeProtocolAndPort, encodeProtocolAndPort } = require('./helpers'); | ||
@@ -53,5 +54,7 @@ function isEqualMethod(method1, method2) { | ||
const routePathnameIsAbsoluteUrl = isAbsoluteUrl(pathname.toString().replace(/^\//, '')); | ||
const decodedPathname = decodeProtocolAndPort(pathname); | ||
if (routePathnameIsAbsoluteUrl) { | ||
const reqPathnameIsAbsoluteUrl = isAbsoluteUrl(decodedPathname.toString().replace(/^\//, '')); | ||
if (reqPathnameIsAbsoluteUrl) { | ||
// eslint-disable-next-line no-lonely-if | ||
@@ -111,13 +114,15 @@ if (!route.pathFn(pathname)) return false; | ||
// so we can collect the Express-style parameters via `matchKeys`. | ||
if (route.pathRegExp) { | ||
if (route.pathRegExp && route.matchKeys) { | ||
const match = req.path.match(route.pathRegExp); | ||
const params = {}; | ||
if (match) { | ||
const params = {}; | ||
route.matchKeys.forEach((key, i) => { | ||
params[key.name] = match[i + 1]; | ||
params[i] = match[i + 1]; | ||
}); | ||
route.matchKeys.forEach((key, i) => { | ||
params[key.name] = match[i + 1]; | ||
params[i] = match[i + 1]; | ||
}); | ||
req.params = params; | ||
req.params = params; | ||
} | ||
} | ||
@@ -153,5 +158,8 @@ | ||
// Encode absolute URL protocol and port characters to tildes to prevent colons from being interpreted as Express parameters. | ||
const paramEncodedPathname = encodeProtocolAndPort(pathname); | ||
const matchKeys = []; | ||
// `pathToRegExp` mutates `matchKeys` to contain a list of named parameters | ||
const pathRegExp = pathToRegExp(pathname, matchKeys); | ||
const pathRegExp = pathToRegExp(paramEncodedPathname, matchKeys); | ||
@@ -163,3 +171,3 @@ const query = Object.assign({}, url.query, _query); | ||
path, | ||
pathFn: p => pathRegExp.test(p), | ||
pathFn: p => pathRegExp.test(encodeProtocolAndPort(p)), | ||
pathname, | ||
@@ -166,0 +174,0 @@ pathRegExp, |
const request = require('request'); | ||
const isAbsoluteUrl = require('is-absolute-url'); | ||
const { isEmpty } = require('lodash'); | ||
const { handleContentType } = require('./lib/helpers'); | ||
const { decodeProtocolAndPort, handleContentType } = require('./lib/helpers'); | ||
@@ -9,8 +9,10 @@ const now = () => new Date().getTime(); | ||
const openingSlashRegex = /^\//; | ||
const leadProtocolRegex = /^(https?)%3A%2F%2F/; | ||
const leadUrlEncodedProtocolRegex = /^(https?)%3A%2F%2F/; | ||
const makeRequestUrl = req => | ||
req.originalUrl | ||
.replace(openingSlashRegex, '') | ||
.replace(leadProtocolRegex, (match, p1) => `${p1}://`); | ||
decodeProtocolAndPort( | ||
req.originalUrl | ||
.replace(openingSlashRegex, '') | ||
.replace(leadUrlEncodedProtocolRegex, (match, p1) => `${p1}://`) | ||
); | ||
@@ -17,0 +19,0 @@ const makeRequestOptions = req => { |
{ | ||
"name": "mockyeah", | ||
"version": "0.19.0", | ||
"version": "0.19.1", | ||
"description": "A powerful service mocking, recording, and playback utility.", | ||
@@ -86,3 +86,3 @@ "main": "index.js", | ||
"private": false, | ||
"gitHead": "22fbf7bcd4f2e41d282cd9c324a150caf33ab7c2" | ||
"gitHead": "a4f7660dd9117ee330652b1820e1e74c89cf934e" | ||
} |
@@ -550,3 +550,6 @@ 'use strict'; | ||
// e.g. http://localhost:4041/http://example.com/some/service | ||
cb => remoteReq.get(path1).expect(200, cb) | ||
cb => remoteReq.get(path1).expect(200, cb), | ||
// Assert paths are routed the correct responses | ||
cb => proxyReq.get(path1).expect(200, cb) | ||
], | ||
@@ -557,2 +560,82 @@ done | ||
it('should record and playback call using full URLs, including custom-encoded', function(done) { | ||
this.timeout = 10000; | ||
const captureName = 'test-some-fancy-capture-full-urls'; | ||
// Construct remote service urls | ||
const path1 = '/http://example.com/some/service/one'; | ||
const path2 = '/http://www.example.com/some/service/one'; | ||
const path3 = '/http://www.example.com:80/some/service/one'; | ||
const path1encoded = '/http~~~example.com/some/service/one'; | ||
const path2encoded = '/http~~~www.example.com/some/service/one'; | ||
const path3encoded = '/http~~~www.example.com~80/some/service/one'; | ||
// Mount remote service end points | ||
remote.get(path1, { text: 'first' }); | ||
remote.get(path2, { text: 'second' }); | ||
remote.get(path3, { text: 'third' }); | ||
// Initiate recording and playback series | ||
async.series( | ||
[ | ||
// Initiate recording | ||
cb => { | ||
proxy.record(captureName); | ||
cb(); | ||
}, | ||
// Invoke requests to remote services through proxy | ||
// e.g. http://localhost:4041/http://example.com/some/service | ||
cb => proxyReq.get(path1).expect(200, 'first', cb), | ||
cb => proxyReq.get(path2).expect(200, 'second', cb), | ||
cb => proxyReq.get(path3).expect(200, 'third', cb), | ||
cb => proxyReq.get(path1encoded).expect(200, 'first', cb), | ||
cb => proxyReq.get(path2encoded).expect(200, 'second', cb), | ||
cb => proxyReq.get(path3encoded).expect(200, 'third', cb), | ||
// Stop recording | ||
cb => { | ||
proxy.recordStop(cb); | ||
}, | ||
// Assert capture file exists | ||
cb => { | ||
fs.statSync(getCaptureFilePath(captureName)); | ||
cb(); | ||
}, | ||
// Reset proxy services and play captured capture | ||
cb => { | ||
proxy.reset(); | ||
cb(); | ||
}, | ||
cb => { | ||
proxy.play(captureName); | ||
cb(); | ||
}, | ||
// Test remote url paths and their sub paths route to the same services | ||
// Assert remote url paths are routed the correct responses | ||
// e.g. http://localhost:4041/http://example.com/some/service | ||
cb => remoteReq.get(path1).expect(200, 'first', cb), | ||
cb => remoteReq.get(path2).expect(200, 'second', cb), | ||
cb => remoteReq.get(path3).expect(200, 'third', cb), | ||
// Assert paths are routed the correct responses | ||
cb => proxyReq.get(path1).expect(200, 'first', cb), | ||
cb => proxyReq.get(path2).expect(200, 'second', cb), | ||
cb => proxyReq.get(path3).expect(200, 'third', cb), | ||
cb => proxyReq.get(path1encoded).expect(200, 'first', cb), | ||
cb => proxyReq.get(path2encoded).expect(200, 'second', cb), | ||
cb => proxyReq.get(path3encoded).expect(200, 'third', cb) | ||
], | ||
done | ||
); | ||
}); | ||
it('should record and playback call with playAll', function(done) { | ||
@@ -559,0 +642,0 @@ this.timeout = 10000; |
@@ -97,2 +97,23 @@ 'use strict'; | ||
describe('custom-encoded URLs', () => { | ||
it('should support registering full URLs and matching request with custom-encoded URLs', done => { | ||
mockyeah.get(`/http://localhost:${proxiedPort}/foo?ok=yes`, { text: 'bar', status: 500 }); | ||
async.series( | ||
[ | ||
cb => | ||
supertest(proxiedApp) | ||
.get('/foo') | ||
.expect(200, cb), | ||
cb => request.get(`/http~~~localhost~${proxiedPort}/foo?ok=yes`).expect(500, 'bar', cb) | ||
], | ||
done | ||
); | ||
}); | ||
it('should support proxying custom-encoded URLs', done => { | ||
request.get(`/http~~~localhost~${proxiedPort}/foo`).expect(200, done); | ||
}); | ||
}); | ||
it('should support proxying other URLs', done => { | ||
@@ -99,0 +120,0 @@ request.get(`/http://localhost:${proxiedPort}/foo?ok=yes`).expect(200, done); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
536788
129
5005