Skip to content

Instantly share code, notes, and snippets.

@choffmeister
Last active October 25, 2015 10:16
Show Gist options
  • Save choffmeister/616df5dfcaee32affaa3 to your computer and use it in GitHub Desktop.
Save choffmeister/616df5dfcaee32affaa3 to your computer and use it in GitHub Desktop.
A simple mock router implementation
// The MIT License (MIT)
// Copyright (c) 2015 Christian Hoffmeister
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import Bluebird from 'bluebird';
export default class MockRouter {
static matchUrl(pattern, url) {
const variableNames = [];
const regexTransformed = pattern.replace(/\{([^\}]+)\}/g, (_, variableName) => {
variableNames.push(variableName);
return '([^\\/]+)';
});
const regex = new RegExp('^' + regexTransformed + '$');
const match = url.match(regex);
if (match) {
return variableNames.reduce((vals, vn, i) => {
vals[vn] = match[i + 1];
return vals;
}, {});
} else {
return null;
}
}
constructor() {
this.routes = [];
this.addRoute = this.addRoute.bind(this);
this.handle = this.handle.bind(this);
}
addRoute(method, pattern, callback) {
this.routes.push({
method: method,
pattern: pattern,
callback: callback
});
}
handle(method, url, payload) {
for (let i = 0, l = this.routes.length; i < l; i++) {
if (this.routes[i].method === method) {
const values = MockRouter.matchUrl(this.routes[i].pattern, url);
if (values) {
const result = this.routes[i].callback(values, payload);
return result instanceof Error
? Bluebird.delay(1000).then(() => Bluebird.reject(result))
: Bluebird.delay(1000).return(result);
}
}
}
return Bluebird.delay(1000).then(() => Bluebird.reject(new Error(`Could not match "${method} ${url}"`)));
}
}
// configure router
var router = new MockRouter();
router.addRoute('GET', '/test/{foo}', (values) => {
return {
test: values.foo
};
});
router.addRoute('POST', '/test2/{foo}', (values, payload) => {
return {
test1: foo,
test2: payload.name
};
});
// test router
console.log(router.handle('GET', '/test/apple')); // { test: "apple" }
console.log(router.handle('POST', '/test/apple')); // null
console.log(router.handle('GET', '/test2/apple')); // null
console.log(router.handle('POST', '/test2/apple', { name: 'pie' })); // { test2: "pie" }
@netzfisch
Copy link

Do we use that? Can we not go directly to the epagesJ-API?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment