Last active
April 11, 2017 15:32
-
-
Save Ross-Hunter/85efd826f038fd81910e800834c3d323 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module HttpHelpers | |
def authenticate user | |
params = { email: user.email, password: 'test_password' } | |
post '/auth', params | |
@auth_token = json_body.dig :meta, :'auth-token' | |
end | |
def authed_get endpoint, opts = {} | |
get endpoint, opts, auth_header | |
end | |
def authed_post endpoint, opts = {} | |
post endpoint, jsonapify(opts), auth_header | |
end | |
def authed_patch endpoint, opts = {} | |
patch endpoint, jsonapify(opts), auth_header | |
end | |
def authed_delete endpoint, opts = {} | |
delete endpoint, opts, auth_header | |
end | |
private | |
def auth_header | |
{ 'Authorization' => @auth_token, | |
'CONTENT_TYPE' => 'application/vnd.api+json' } | |
end | |
private | |
def jsonapify opts | |
data = opts.slice(:type, :id, :attributes, :relationships) | |
data = data.deep_transform_keys { |key| key.to_s.dasherize.to_sym } | |
{ data: data }.to_json | |
end | |
end | |
RSpec.configure do |config| | |
config.include HttpHelpers | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment