-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register all requests with the request handler, expose server.canHandle
- Loading branch information
Showing
5 changed files
with
191 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Server } from "miragejs"; | ||
|
||
describe("External | Shared | Server | canHandle", () => { | ||
let server; | ||
|
||
beforeEach(() => { | ||
server = new Server({ | ||
environment: "test" | ||
}); | ||
}); | ||
|
||
afterEach(function() { | ||
server.shutdown(); | ||
}); | ||
|
||
test("its true when a route is defined", () => { | ||
server.get("/movies", () => { | ||
return [1, 2, 3]; | ||
}); | ||
|
||
expect(server.canHandle("get", "/movies")).toBeTrue(); | ||
}); | ||
|
||
test("its false when a route is not defined", () => { | ||
expect(server.canHandle("get", "/movies")).toBeFalse(); | ||
}); | ||
|
||
test("it works for namespaces", () => { | ||
server.namespace = "api"; | ||
server.get("/movies", () => { | ||
return [1, 2, 3]; | ||
}); | ||
|
||
expect(server.canHandle("get", "/api/movies")).toBeTrue(); | ||
expect(server.canHandle("get", "/movies")).toBeFalse(); | ||
}); | ||
|
||
test("it works for urls on a different origin", () => { | ||
server.urlPrefix = "https://example.com"; | ||
server.get("/movies", () => { | ||
return [1, 2, 3]; | ||
}); | ||
|
||
expect(server.canHandle("get", "https://example.com/movies")).toBeTrue(); | ||
expect(server.canHandle("get", "/movies")).toBeFalse(); | ||
}); | ||
|
||
test("it works with dynamic segments", () => { | ||
server.get("/movies/:id", () => { | ||
return { id: 1 }; | ||
}); | ||
|
||
expect(server.canHandle("get", "/movies/1")).toBeTrue(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import RouteRecognizer from "route-recognizer"; | ||
import urlParse from "url-parse"; | ||
|
||
const defaultOrigin = Symbol(); | ||
|
||
/** | ||
A class for storing and looking up information about route handlers registered with Mirage. | ||
@class RequestHandler | ||
@private | ||
*/ | ||
export default class RequestHandler { | ||
constructor(server) { | ||
this.server = server; | ||
this.originRoutes = {}; | ||
} | ||
|
||
/** | ||
A single group of route recognizers won't work because route recognizer has no concept of an origin. So we're going to use this function to group all of the recognizers by their origin. | ||
@private | ||
*/ | ||
recognizersFor(url) { | ||
let { origin } = urlParse(url); | ||
let key = origin || defaultOrigin; | ||
|
||
if (!this.originRoutes[key]) { | ||
let routes = { | ||
GET: new RouteRecognizer(), | ||
POST: new RouteRecognizer(), | ||
PUT: new RouteRecognizer(), | ||
PATCH: new RouteRecognizer(), | ||
DELETE: new RouteRecognizer(), | ||
OPTIONS: new RouteRecognizer() | ||
}; | ||
routes.delete = routes.del; | ||
this.originRoutes[key] = routes; | ||
} | ||
|
||
return this.originRoutes[key]; | ||
} | ||
|
||
/** | ||
The recognizer for the given verb and url | ||
@private | ||
*/ | ||
recognizerFor(verb, url) { | ||
let recognizers = this.recognizersFor(url); | ||
let method = verb.toUpperCase(); | ||
return recognizers[method]; | ||
} | ||
|
||
/** | ||
Adds a route handler. | ||
@param {string} verb The HTTP verb to handle | ||
@param {string} url The URL to attach the handler to | ||
@param {handler} function The handler | ||
@public | ||
*/ | ||
register(verb, url, handler) { | ||
let { pathname } = urlParse(url); | ||
this.recognizerFor(verb, url).add([{ path: pathname, handler }]); | ||
} | ||
|
||
/** | ||
@private | ||
*/ | ||
routesFor(verb, url) { | ||
let method = verb.toUpperCase(); | ||
let recognizer = this.recognizerFor(method, url); | ||
let { pathname } = urlParse(url); | ||
return recognizer.recognize(pathname) || []; | ||
} | ||
|
||
/** | ||
Checks whether or not the verb and url can be handled by one of the registered handlers | ||
@return {boolean} Is the verb/url registered | ||
@public | ||
*/ | ||
canHandle(verb, url) { | ||
return this.routesFor(verb, url).length > 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters