-
-
Notifications
You must be signed in to change notification settings - Fork 910
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
Fix route matcher accepts a format as a symbol or string #693
Changes from all commits
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 |
---|---|---|
|
@@ -48,6 +48,26 @@ | |
to(action: 'show', some: 'other', params: 'here') | ||
end | ||
end | ||
|
||
context 'when route has a default format' do | ||
it 'accepts' do | ||
expect(controller_with_defined_routes). | ||
to route(:post, "/#{controller_path}"). | ||
to(action: 'create', format: 'json') | ||
end | ||
|
||
it 'accepts when format is specified as a symbol' do | ||
expect(controller_with_defined_routes). | ||
to route(:post, "/#{controller_path}"). | ||
to(action: 'create', format: :json) | ||
end | ||
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. What if you leave the format out? Seems we need a test for that too.
|
||
|
||
it 'rejects when format is unspecified' do | ||
expect(controller_with_defined_routes). | ||
not_to route(:post, "/#{controller_path}"). | ||
to(action: 'create') | ||
end | ||
end | ||
end | ||
|
||
context 'when controller and action are specified as a joined string' do | ||
|
@@ -64,6 +84,20 @@ | |
to("#{controller_path}#show", id: 1) | ||
end | ||
end | ||
|
||
context 'when route has the format' do | ||
it 'accepts' do | ||
expect(controller_with_defined_routes). | ||
to route(:post, "/#{controller_path}"). | ||
to("#{controller_path}#create", format: 'json') | ||
end | ||
|
||
it 'rejects when format is unspecified' do | ||
expect(controller_with_defined_routes). | ||
not_to route(:post, "/#{controller_path}"). | ||
to(action: 'create') | ||
end | ||
end | ||
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. Same thing here, seems we need to test that it rejects if format is not specified. |
||
end | ||
|
||
def controller_with_defined_routes | ||
|
@@ -76,6 +110,9 @@ def controller_with_defined_routes | |
define_routes do | ||
get "/#{_controller_path}", to: "#{_controller_path}#index" | ||
get "/#{_controller_path}/:id", to: "#{_controller_path}#show" | ||
post "/#{_controller_path}", | ||
to: "#{_controller_path}#create", | ||
defaults: { format: :json } | ||
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. Align the elements of a hash literal if they span more than one line. |
||
end | ||
|
||
controller | ||
|
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.
This works obviously, although I wonder if it makes a little more sense to say "leave this value alone if the key is :format". In other words change this line to just
value
.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.
We need the
value.to_sym
if we want accept the format as string or symbol.Since we accept controller and action as string and symbols, I think is a good idea we accept the
format
as string and symbols too. What you think?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.
I could go either way. This is fine.
I wish there were a better way to say this actually. The only reason we stringify is for the params I believe, the other "special" params like controller, action, format, etc. don't need to be touched. Ah well.