-
Notifications
You must be signed in to change notification settings - Fork 4
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
Checks that post params is not empty for update and create #369
Conversation
…e and create If there are no params, determines whether the request body was empty or if it just was not parsed properly, and responds with approprtiate error code depending on the situation. If the body was empty, includes an error link to the new action. If the body was not empty, responds with unsupported_media_type.
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.
Great changes.
Can I get you to make those small tweaks and then we'll merge.
Please run the entire test suite to double check as the CI is broken 😕
# an empty body parsed as json will have the form {model => {}} | ||
# json body with application/x-www-form-urlencoded content type will have the form {json_string => {}} | ||
# json or form encoded with text/plain content type will have the form {} | ||
if request.POST.values.all? { |val| val.nil? || (val.is_a?(Hash) && val.empty?) } |
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.
Simplify this predicate by using .blank?
# json or form encoded with text/plain content type will have the form {} | ||
if request.POST.values.all? { |val| val.nil? || (val.is_a?(Hash) && val.empty?) } | ||
|
||
if (request.body.string == "") |
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.
More idiomatic ruby would use .blank?
or .empty?
. Also I believe the parentheses are not needed here
message = "Failed to parse the request body. Ensure that it is formed correctly and matches the content-type (#{request.content_type})" | ||
end | ||
end | ||
render_error(status, message,nil,'validate_contains_params', options) |
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.
spacing around commas
spec/requests/dataset_items_spec.rb
Outdated
@env['HTTP_AUTHORIZATION'] = admin_token | ||
|
||
@create_dataset_item_url = "/datasets/" + dataset.id.to_s + "/items" | ||
@update_dataset_item_url = "/datasets/" + dataset_item.dataset_id.to_s + "/items/" + dataset_item.id.to_s |
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.
Can we use url_helpers
here? Or any rails method for generating the route?
If not, if you use string interpolation (which should do the .to_s
part for you).
@env['CONTENT_TYPE'] = "application/json" | ||
params = nil | ||
post @create_dataset_item_url, params, @env | ||
expect(response).to have_http_status(400) |
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.
should this test have a new resource link as well (like the test below)?
put @update_dataset_item_url, params, @env | ||
expect(response).to have_http_status(200) | ||
end | ||
|
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.
the update route doesn't get the empty body (400 response) tests?
Fixes #366. Fixes #361.
If there are no params, determines whether the request body was empty or if it just was not parsed properly, and responds with approprtiate error code depending on the situation.
If the body was empty, includes an error link to the new action.
If the body was not empty, responds with unsupported_media_type.