Extend request helpers for rspec-rails request specs

Session-based API authentication requires the X-requested-with header to be present.
Since Integration tests of Rails do not offer adding a header to all requests as
Capybara or Rack::Test does, we simply extend the request helpers to do
so.
pull/5908/head
Oliver Günther 7 years ago
parent 92060508a9
commit 4b83ac920a
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 53
      spec/support/request_with_header.rb

@ -1,8 +1,55 @@
RSpec.configure do |c|
c.before(:each, type: :request) do |ex|
header('X-Requested-With', 'XMLHttpRequest') unless ex.metadata[:skip_xhr_header]
module APISessionAuthentication
def merge_with_header(args = {})
# Can't get example. in here
metadata = self.class.metadata
headers = args.fetch(:headers) { {} }
unless metadata[:skip_xhr_header]
headers.merge!('X-Requested-With' => 'XMLHttpRequest')
end
headers
end
def get(path, args = {})
args[:headers] = merge_with_header args
super
end
def post(path, args = {})
args[:headers] = merge_with_header args
super
end
def put(path, args = {})
args[:headers] = merge_with_header args
super
end
def delete(path, args = {})
args[:headers] = merge_with_header args
super
end
def patch(path, args = {})
args[:headers] = merge_with_header args
super
end
def head(path, args = {})
args[:headers] = merge_with_header args
super
end
end
RSpec.configure do |c|
##
# Session-based API authentication requires the X-requested-with header to be present.
# Since Integration tests of Rails do not offer adding a header to all requests as
# Capybara or Rack::Test does, we simply extend the request helpers to do so.
c.include APISessionAuthentication, type: :request
c.before(:each, type: :feature) do |ex|
unless ex.metadata[:skip_xhr_header] || ex.metadata[:js]
page.driver.header('X-Requested-With', 'XMLHttpRequest')

Loading…
Cancel
Save