I am very new to RSpec. I'm writing a test for a REST API that we're making in Rails. The API consists of one action with the following specification for the requests it will accept:
A request will only be accepted if:
- It is made using HTTPS
- It is sent as a POST request
- It is sent with the following parameters: value_name, value, item_id
For example, a valid request is:
POST /new_api/action HTTPS/1.0
snip
Content-Type: application/x-www-form-urlencoded
Content-Length: 52
value_name=Contract+Id&value=abcdef123456&item_id=38
The specification states that if a request is sent that meets these requirements, the server will respond with a 200
code. If a request is sent that doesn't meet these requirements, the server will respond with a 400
code.
With these requirements, I would like to write RSpec tests that ensure the action responds with the proper code depending on whether the request:
POST
or a different request typeThis is psuedo-Ruby for one of the tests I want to write:
describe 'New REST API' do
it 'should accept valid requests'
request = mock a request that
uses HTTPS
has these parameters: {:value_name => "name",\
:value => 1, :item_id => 2}
end
post name_of_new_action using request
response.status.should be(200)
end
end
What are the proper hooks in RSpec to:
We're using Rails 3.2.13 and Ruby 1.8.7 (this is an older app that we're maintaining). Our Gemfile has
gem 'rspec-rails', '~> 2.11.0'
gem 'guard-rspec', '~> 1.2.1'
gem 'capybara', '~> 1.1.2'
gem 'database_cleaner', '~> 1.1.1'
gem 'factory_girl_rails', '~> 1.7.0
Everything you need is native in Rails integration test lib, even no need Rspec.
It is made using HTTPS
get test_path, nil, {'HTTPS'=>'on'}
It is sent as a POST request
post test_path, {foo: 'bar', fff: 'bbb'}
It is sent with the following parameters: value_name, value, item_id, in HTTPS.
post test_path, {value_name: 'abc', value: 'foo', item_id: 'bar'}, {'HTTPS'=>'on'}
Check response need Rspec
expect(response).to be_success
expect(response.status).to eq(200)
expect(response.body).to match(/some string in body/)