Skip to content

Instantly share code, notes, and snippets.

@poc7667
Created November 30, 2015 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poc7667/edd252dac217c666b6f4 to your computer and use it in GitHub Desktop.
Save poc7667/edd252dac217c666b6f4 to your computer and use it in GitHub Desktop.
routes_spec.rb

How to add a rspec test for API test with http basic auth

Let's say if you put api controller under app/controllers/api/v1/xxx_controller.rb

In most case, you should create spec/controllers/api/v1/xxx_controller_spec.rb

here's a sample script for spec test.

#.//spec/requests/api/v1/routes_spec.rb
require 'rails_helper'
describe "Messages API" do
before(:all) do
Rails.application.load_tasks
Rake::Task['routes:update'].invoke
username = ENV['API_ACCESS_USERNAME']
passwd = ENV['API_ACCESS_PASSWORD']
@basic_auth = {
'HTTP_AUTHORIZATION' => ActionController::HttpAuthentication::Basic.encode_credentials(username, passwd)
}
@sample_route_param = {
"from_city": "TOKYO",
"to_city": "TAIPEI",
"start_date": "2015-12-28"
}
end
it 'sends a list of messages' do
get '/api/v1/flights',@sample_route_param, @basic_auth
json = JSON.parse(response.body)
expect(response).to be_success
expect(json.length).to be > 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment