json - Set request headers for Rspec and Rack::Test in Ruby on Rails -
i'm trying test login , logout json endpoints application using rspec. using devise , devise_token_auth gems in order build json endpoints authentication.
i can log user in, when logging out there needs several request headers present logout function find correct user , complete.
i've tried add headers current rack session, seems drop them when request created. here code far...
helper method (spec/support/api_helper.rb):
def login_user user = create(:user) post '/api/v1/auth/sign_in', email: user.email, password: user.password, format: :json return { 'token-type' => 'bearer', 'uid' => last_response.headers['uid'], 'access-token' => last_response.headers['access-token'], 'client' => last_response.headers['client'], 'expiry' => last_response.headers['expiry'] } end
my rspec example (spec/api/v1/authentication_spec.rb):
describe 'delete /api/v1/auth/sign_out' 'should destroy current session , log out' login_user delete '/api/v1/auth/sign_out', {}, login_user expect(last_response.status).to eq 200 expect(parse_json(last_response.body['success'])).to eq true end end
the output when trying request user log out delete http verb:
=> #"sameorigin", "x-xss-protection"=>"1; mode=block", "x-content-type-options"=>"nosniff", "content-type"=>"application/json; charset=utf-8", "cache-control"=>"no-cache", "x-request-id"=>"754c89bb-7a8f-4c83-b32b-dc9ed3404863", "x-runtime"=>"0.010023"}, @errors="", @body_string=nil, @status=401, @header={"x-frame-options"=>"sameorigin", "x-xss-protection"=>"1; mode=block", "x-content-type-options"=>"nosniff", "content-type"=>"application/json; charset=utf-8", "cache-control"=>"no-cache", "x-request-id"=>"754c89bb-7a8f-4c83-b32b-dc9ed3404863", "x-runtime"=>"0.010023", "content-length"=>"37"}, @chunked=false, @writer=#, @block=nil, @length=37, @body=["{\"errors\":[\"authorized users only.\"]}"]>
i have tried adding headers current rack session below:
header 'uid', login_user['uid'] header 'token-type', 'bearer' header 'access-token', login_user['access-token'] header 'client', login_user['client'] header 'expiry', login_user['expiry']
does know why headers being dropped rack session when new request made? how else can add headers rack session?
i thing doing should work. can set header header('name', 'value').
anyways, should check post '/api/v1/auth/sign_in', email: user.email, password: user.password, format: :json response.
the user.password should hashed, if sent won't logged in.
Comments
Post a Comment