parent
4b83ac920a
commit
cbb0d90fde
@ -1,59 +0,0 @@ |
|||||||
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') |
|
||||||
end |
|
||||||
end |
|
||||||
end |
|
||||||
|
|
@ -0,0 +1,18 @@ |
|||||||
|
RSpec.configure do |c| |
||||||
|
## |
||||||
|
# Session-based API authentication requires the X-requested-with header to be present. |
||||||
|
c.before(:each, type: :request) do |ex| |
||||||
|
unless ex.metadata[:skip_xhr_header] |
||||||
|
header('X-Requested-With', 'XMLHttpRequest') |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
c.before(:each, type: :feature) do |ex| |
||||||
|
unless ex.metadata[:skip_xhr_header] || ex.metadata[:js] |
||||||
|
page.driver.header('X-Requested-With', 'XMLHttpRequest') |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
|
||||||
|
|
@ -0,0 +1,16 @@ |
|||||||
|
module RackTestHelper |
||||||
|
include Rack::Test::Methods |
||||||
|
|
||||||
|
def app |
||||||
|
Rails.application |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
RSpec.configure do |config| |
||||||
|
# Use Rack::Test for regular request specs (esp. API requests) |
||||||
|
config.include RackTestHelper, type: :request |
||||||
|
|
||||||
|
# If desired, we can use the Rails IntegrationTest request spec |
||||||
|
# (more like a feature spec) with this type. |
||||||
|
config.include RSpec::Rails::RequestExampleGroup, type: :rails_request |
||||||
|
end |
Loading…
Reference in new issue