How to test 400 (bad request) error?

37 views
Skip to first unread message

Kris.S

unread,
Dec 14, 2017, 1:27:13 PM12/14/17
to rspec
I'd like to write a spec for testing requests with invalid encoding parameters. The expected behaviour is that the 400 error page is rendered.

module ErrorResponses

 
# See: https://github.com/rails/rails/pull/11289#issuecomment-118612393
 
def respond_without_detailed_exceptions
    env_config
= Rails.application.env_config
    original_show_exceptions
= env_config["action_dispatch.show_exceptions"]
    original_show_detailed_exceptions
= env_config["action_dispatch.show_detailed_exceptions"]
    env_config
["action_dispatch.show_exceptions"] = true
    env_config
["action_dispatch.show_detailed_exceptions"] = false
   
yield
 
ensure
    env_config
["action_dispatch.show_exceptions"] = original_show_exceptions
    env_config
["action_dispatch.show_detailed_exceptions"] = original_show_detailed_exceptions
 
end
end

RSpec.configure do |config|
  config
.include ErrorResponses
end



require 'rails_helper'

RSpec.describe 'Errors', type: :feature do
  around
:each do |example|
    respond_without_detailed_exceptions
do
      example
.run
   
end
 
end

  context
'with invalid request parameter' do
    it
'renders 400' do
      visit
'/home?test=%%aa%%'
      expect
(page.status_code).to eq 400
   
end
 
end
end


The problem I'm having is that Rack would throw Rack::QueryParser::InvalidParameterError exception when calling visit '/home?test=%%aa%%', and the 400 error is never rendered from the spec.

Any help would be appreciated. Thanks.
Reply all
Reply to author
Forward
0 new messages