-
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
Changes from 8 commits
104913a
8f8d19d
7e11f08
9c69042
ddb552f
a5255c9
4acd981
cc87e11
e15b07a
84f7993
a6925a2
2491c90
69172ed
f91c48e
15a136b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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>; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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 = { | ||
"BasicPage": { | ||
"path": ["/basicPage"], | ||
"page": pageWrapper, | ||
}, | ||
"BasicPageCaps": { | ||
"path": ["/basicPageCaps"], | ||
"page": pageWrapper, | ||
"method": "GET", // this should be all caps because the req.getMethod() will return 'get'. | ||
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. the fact that case sensitivity matters smells to me 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. This is testing that case sensitivity doesn't matter in the Routr package. In versions <0.1.1, it did a case sensitive match. >=0.1.1, it added a |
||
}, | ||
"PostPage": { | ||
"path": ["/postPage"], | ||
"page": pageWrapper, | ||
"method": "post", | ||
}, | ||
"GetAndPostPage": { | ||
"path": ["/getAndPostPage"], | ||
"page": pageWrapper, | ||
"method": ["get", "post"], | ||
}, | ||
}; | ||
|
||
export default Routes; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import Navigator from "../../../context/Navigator"; | ||
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. THANK YOU FOR THESE TESTS! |
||
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 RequestContext(options); | ||
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. don't leave in commented-out code without an explanation |
||
requestContext = new RequestContextStub(options); | ||
}); | ||
afterEach(() => { | ||
requestContext = null; | ||
}); | ||
|
||
it("routes to a basic page using an unspecified method get", (done) => { | ||
const req = { | ||
method: "get", | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: "/basicPage", | ||
}; | ||
const expressRequest = new ExpressServerRequest(req); | ||
|
||
requestContext.navigator.on('page', () => { | ||
expect(expressRequest.getRouteName() === 'BasicPage'); | ||
done(); | ||
}); | ||
|
||
requestContext.navigate(expressRequest, History.events.PAGELOAD); | ||
}); | ||
|
||
it("routes to a basic page using an unspecified method HEAD", (done) => { | ||
const req = { | ||
method: "head", | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: "/basicPage", | ||
}; | ||
const expressRequest = new ExpressServerRequest(req); | ||
|
||
requestContext.navigator.on('page', () => { | ||
expect(expressRequest.getRouteName() === 'BasicPage'); | ||
done(); | ||
}); | ||
|
||
requestContext.navigate(expressRequest, History.events.PAGELOAD); | ||
}); | ||
|
||
it("routes to a page using a method GET (caps)", (done) => { | ||
const req = { | ||
method: "get", | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: "/basicPageCaps", | ||
}; | ||
const expressRequest = new ExpressServerRequest(req); | ||
|
||
requestContext.navigator.on('page', () => { | ||
expect(expressRequest.getRouteName() === 'BasicPageCaps'); | ||
done(); | ||
}); | ||
|
||
requestContext.navigate(expressRequest, History.events.PAGELOAD); | ||
}); | ||
|
||
it("routes to a page using a method POST", (done) => { | ||
const req = { | ||
method: "post", | ||
protocol: "http", | ||
secure: false, | ||
hostname: "localhost", | ||
url: "/postPage", | ||
}; | ||
const expressRequest = new ExpressServerRequest(req); | ||
|
||
requestContext.navigator.on('page', () => { | ||
expect(expressRequest.getRouteName() === 'PostPage'); | ||
done(); | ||
}); | ||
|
||
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); | ||
|
||
requestContext.navigator.on('page', () => { | ||
expect(expressRequest.getRouteName() === 'GetAndPostPage'); | ||
done(); | ||
}); | ||
|
||
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); | ||
|
||
requestContext.navigator.on('page', () => { | ||
expect(expressRequest.getRouteName() === 'GetAndPostPage'); | ||
done(); | ||
}); | ||
|
||
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,7 +50,7 @@ class Navigator extends EventEmitter { | |
|
||
this._haveInitialized = true; | ||
|
||
var route = this.router.getRoute(request.getUrl(), {navigate: {path:request.getUrl(), type:type}}); | ||
var route = this.router.getRoute(request.getUrl(), {method: request.getMethod()}); | ||
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. I found myself wondering: how come we don't need The only thing I can think of offhand that might break here, is if I looked at the commit in 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. I'm confident in all of the changes here except this 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. I don't understand why it was necessary either, since it didn't really seem to be used. It might have been used with |
||
|
||
if (route) { | ||
logger.debug(`Mapped ${request.getUrl()} to route ${route.name}`); | ||
|
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.
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.
Not strictly necessary and if people disagree, it can easily be changed back. I like to have parity between GET/HEAD in most cases.
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.
Does react-server even understand
head
, or does it just return the same thing as withget
? (You'd think I'd know the answer to this...)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.
Oh... is it common to have an API respond to, e.g.
POST
orHEAD
? (i.e., should we be auto-addinghead
, to everything-ish?)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.
As it stands right now,
react-server
throws a 404 error for all HEAD requests because routes being passed toroutr
don't send themethod
attribute with it. Therefore,routr
defaults to 'GET' and checks the request.head !== get
, therefore there's no route found for HEAD/POST/PUT/PATCH/etc.