-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Address routr api changes & Handle POST #837
Merged
drewpc
merged 15 commits into
redfin:master
from
SoprisApps:patch-address-routr-api-changes
Jan 18, 2017
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
104913a
Update the getRoute function call to pass the method object and no lo…
drewpc 8f8d19d
Update the getRoute() to use the getMethod() getter instead of .method.
drewpc 7e11f08
Update the compileClient function to handle an array of methods as op…
drewpc 9c69042
Add unit tests for various Routr method types.
drewpc ddb552f
Remove console.log statements.
drewpc a5255c9
Default to setting both "GET" and "HEAD" when "method" isn't set. Th…
drewpc 4acd981
Convert the test to use a RequestContext() stub instead of the whole …
drewpc cc87e11
Updated documentation.
drewpc e15b07a
Removed unecessary code.
drewpc 84f7993
Added a test to show a route not being found.
drewpc a6925a2
Added Many! More! Tests!
drewpc 2491c90
Updated the default to method setting to be undefined so that routr m…
drewpc 69172ed
Remove console.log statement.
drewpc f91c48e
Cleanup the compileClient check for method settings.
drewpc 15a136b
Fix comment.
drewpc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
6 changes: 6 additions & 0 deletions
6
packages/react-server/core/__tests__/context/navigator/DumbPage.js
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,6 @@ | ||
export default class DumbPage { | ||
getElements() { | ||
const routeName = this.getRequest().getRouteName(); | ||
return <div id="routeName">{routeName}</div>; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
packages/react-server/core/__tests__/context/navigator/NavigatorRoutes.js
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,66 @@ | ||
import DumbPage from "./DumbPage"; | ||
|
||
// This wrapper is taken from the compileClient function that writes the routes_(client|server).js file | ||
const pageWrapper = { | ||
default: () => { | ||
return { | ||
done: (cb) => { cb(DumbPage); }, | ||
}; | ||
}, | ||
}; | ||
|
||
const Routes = { | ||
"GetPage": { | ||
"path": ["/getPage"], | ||
"page": pageWrapper, | ||
"method": "get", | ||
}, | ||
"HeadPage": { | ||
"path": ["/headPage"], | ||
"page": pageWrapper, | ||
"method": "head", | ||
}, | ||
"PostPage": { | ||
"path": ["/postPage"], | ||
"page": pageWrapper, | ||
"method": "post", | ||
}, | ||
"PatchPage": { | ||
"path": ["/patchPage"], | ||
"page": pageWrapper, | ||
"method": "patch", | ||
}, | ||
"PutPage": { | ||
"path": ["/putPage"], | ||
"page": pageWrapper, | ||
"method": "put", | ||
}, | ||
"GetPageCaps": { | ||
"path": ["/getPageCaps"], | ||
"page": pageWrapper, | ||
"method": "GET", // this should be all caps because the req.getMethod() will return 'get'. | ||
}, | ||
"GetAndPostPage": { | ||
"path": ["/getAndPostPage"], | ||
"page": pageWrapper, | ||
"method": ["get", "post"], | ||
}, | ||
"AllMethodsPage": { | ||
"path": ["/allMethodsPage"], | ||
"page": pageWrapper, | ||
"method": undefined, | ||
}, | ||
"NullMethodsPage": { | ||
"path": ["/nullMethodsPage"], | ||
"page": pageWrapper, | ||
"method": null, | ||
}, | ||
"EmptyArrayMethodsPage": { | ||
"path": ["/emptyArrayMethodsPage"], | ||
"page": pageWrapper, | ||
"method": [], | ||
}, | ||
|
||
}; | ||
|
||
export default Routes; |
202 changes: 202 additions & 0 deletions
202
packages/react-server/core/__tests__/context/navigator/NavigatorSpec.js
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,202 @@ | ||
import Navigator from "../../../context/Navigator"; | ||
import History from "../../../components/History"; | ||
import ExpressServerRequest from "../../../ExpressServerRequest"; | ||
import NavigatorRoutes from "./NavigatorRoutes"; | ||
|
||
class RequestContextStub { | ||
constructor(options) { | ||
this.navigator = new Navigator(this, options); | ||
} | ||
navigate (request, type) { | ||
this.navigator.navigate(request, type); | ||
} | ||
framebackControllerWillHandle() { return false; } | ||
getMobileDetect() { return null; } | ||
} | ||
|
||
|
||
describe("Navigator", () => { | ||
let requestContext; | ||
const options = { | ||
routes: NavigatorRoutes, | ||
}; | ||
|
||
beforeEach(() => { | ||
requestContext = new RequestContextStub(options); | ||
}); | ||
afterEach(() => { | ||
requestContext = null; | ||
}); | ||
|
||
it("routes to a page using a method GET (caps)", (done) => { | ||
const req = { | ||
method: "get", | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: "/getPageCaps", | ||
}; | ||
const expressRequest = new ExpressServerRequest(req); | ||
|
||
const navigatedToPage = () => { | ||
expect(expressRequest.getRouteName()).toBe('GetPageCaps'); | ||
done(); | ||
}; | ||
requestContext.navigator.on('navigateDone', navigatedToPage); | ||
requestContext.navigator.on('page', navigatedToPage); | ||
|
||
requestContext.navigate(expressRequest, History.events.PAGELOAD); | ||
}); | ||
|
||
it("routes to a page that can handle GET and POST using a method GET", (done) => { | ||
const req = { | ||
method: "get", | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: "/getAndPostPage", | ||
}; | ||
const expressRequest = new ExpressServerRequest(req); | ||
|
||
const navigatedToPage = () => { | ||
expect(expressRequest.getRouteName()).toBe('GetAndPostPage'); | ||
done(); | ||
}; | ||
requestContext.navigator.on('navigateDone', navigatedToPage); | ||
requestContext.navigator.on('page', navigatedToPage); | ||
|
||
requestContext.navigate(expressRequest, History.events.PAGELOAD); | ||
}); | ||
|
||
it("routes to a page that can handle GET and POST using a method POST", (done) => { | ||
const req = { | ||
method: "post", | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: "/getAndPostPage", | ||
}; | ||
const expressRequest = new ExpressServerRequest(req); | ||
|
||
const navigatedToPage = () => { | ||
expect(expressRequest.getRouteName()).toBe('GetAndPostPage'); | ||
done(); | ||
}; | ||
requestContext.navigator.on('navigateDone', navigatedToPage); | ||
requestContext.navigator.on('page', navigatedToPage); | ||
|
||
requestContext.navigate(expressRequest, History.events.PAGELOAD); | ||
}); | ||
|
||
|
||
const methods = [ | ||
'get', | ||
'head', | ||
'put', | ||
'patch', | ||
'post', | ||
]; | ||
|
||
methods.forEach((testingMethod) => { | ||
methods.forEach((otherMethod) => { | ||
const req = { | ||
method: otherMethod, | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: `/${testingMethod}Page`, | ||
}; | ||
const expressRequest = new ExpressServerRequest(req); | ||
|
||
if (testingMethod === otherMethod) { | ||
it(`does route to a page expecting ${otherMethod} using a method ${testingMethod}`, (done) => { | ||
const navigatedToPage = (err) => { | ||
const httpStatus = err.status || 200; | ||
expect(httpStatus).toBe(200, `A route with method ${testingMethod} was not found when one should have been found.`); | ||
done(); | ||
}; | ||
requestContext.navigator.on('navigateDone', navigatedToPage); | ||
requestContext.navigator.on('page', navigatedToPage); | ||
requestContext.navigate(expressRequest, History.events.PAGELOAD); | ||
}); | ||
|
||
it("routes to a page that accepts all methods", (done) => { | ||
const allMethodReq = { | ||
method: otherMethod, | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: "/allMethodsPage", | ||
}; | ||
const allMethodExpressRequest = new ExpressServerRequest(allMethodReq); | ||
|
||
const navigatedToPage = (err) => { | ||
const httpStatus = err.status || 200; | ||
expect(httpStatus).toBe(200, `A route with method ${testingMethod} was not found for the AllMethodsPage.`); | ||
expect(allMethodExpressRequest.getRouteName()).toBe('AllMethodsPage'); | ||
done(); | ||
}; | ||
requestContext.navigator.on('navigateDone', navigatedToPage); | ||
requestContext.navigator.on('page', navigatedToPage); | ||
|
||
requestContext.navigate(allMethodExpressRequest, History.events.PAGELOAD); | ||
}); | ||
|
||
it("doesn't route to a page because the methods were set improperly with a 'null'", (done) => { | ||
const noMethodReq = { | ||
method: otherMethod, | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: "/nullMethodsPage", | ||
}; | ||
const noMethodExpressRequest = new ExpressServerRequest(noMethodReq); | ||
|
||
const navigatedToPage = (err) => { | ||
const httpStatus = err.status || 200; | ||
expect(httpStatus).toBe(404, `A route with method ${testingMethod} was found for the NullMethodsPage.`); | ||
done(); | ||
}; | ||
requestContext.navigator.on('navigateDone', navigatedToPage); | ||
requestContext.navigator.on('page', navigatedToPage); | ||
|
||
requestContext.navigate(noMethodExpressRequest, History.events.PAGELOAD); | ||
}); | ||
|
||
it("doesn't route to a page because the methods were set improperly with a '[]'", (done) => { | ||
const noMethodReq = { | ||
method: otherMethod, | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: "/emptyArrayMethodsPage", | ||
}; | ||
const noMethodExpressRequest = new ExpressServerRequest(noMethodReq); | ||
|
||
const navigatedToPage = (err) => { | ||
const httpStatus = err.status || 200; | ||
expect(httpStatus).toBe(404, `A route with method ${testingMethod} was found for the EmptyArrayMethodsPage.`); | ||
done(); | ||
}; | ||
requestContext.navigator.on('navigateDone', navigatedToPage); | ||
requestContext.navigator.on('page', navigatedToPage); | ||
|
||
requestContext.navigate(noMethodExpressRequest, History.events.PAGELOAD); | ||
}); | ||
} else { | ||
it(`doesn't route to a page expecting ${otherMethod} using a method ${testingMethod}`, (done) => { | ||
const navigatedToPage = (err) => { | ||
const httpStatus = err.status || 200; | ||
expect(httpStatus).toBe(404, `A route with method ${testingMethod} was found when one should not have been found.`); | ||
done(); | ||
}; | ||
requestContext.navigator.on('navigateDone', navigatedToPage); | ||
requestContext.navigator.on('page', navigatedToPage); | ||
requestContext.navigate(expressRequest, History.events.PAGELOAD); | ||
}); | ||
} | ||
}); | ||
|
||
}); | ||
|
||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add some failure cases here too? E.g. doesn't respond to a POST request when configured with PUT only &c. |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
THANK YOU FOR THESE TESTS!