Module: DSL::Request::ClassMethods
- Defined in:
- lib/rspec-api/dsl/request/body.rb,
lib/rspec-api/dsl/request/status.rb,
lib/rspec-api/dsl/request/headers.rb,
lib/rspec-api/dsl/request/request.rb,
lib/rspec-api/dsl/request/response.rb
Instance Method Summary collapse
-
#respond_with(status_symbol, &block) ⇒ Object
Runs a set of expectations on the result of the last API request.
-
#should_respond_with_expected_body(options = {}) ⇒ Object
Creates an example group for expectations on the response body of the last API request and runs it to verify that it matches best practices: * if response is succesful and has a body - the body should be a JSON-marshalled Array or Hash - if request has a callback parameter, the body should be JSONP - if request has a sort parameter, the body should be sorted - if request has a filter parameter, the body should be filtered - if custom expectations are passed, they should pass - if some attributes are expected, the body should include them.
-
#should_respond_with_expected_headers(options = {}) ⇒ Object
Creates an example group for expectations on the response headers of last API request and runs it to verify that it matches best practices: * if request has entity body, the Content-Type header should be JSON * if request has pages, the Link header should have a ‘rel=prev’ link.
-
#should_respond_with_status(status) ⇒ Object
Creates an example group for expectations on the HTTP status code of the last API request and runs it to verify that it matches
status.
Instance Method Details
#respond_with(status_symbol, &block) ⇒ Object
Runs a set of expectations on the result of the last API request. The most basic way to call ‘respond_with` is with a status code:
respond_with :ok
This simple line will run a number of expectations, based on best practices that any RESTful API is expected to match:
-
the returned HTTP status will be matched against 200 OK
-
the response headers will be checked for Content-Type etc.
-
the respone body will be checked for attributes etc.
Additionally, the user can specify custom expectations for the response by passing a block to the method:
respond_with :ok do |response|
expect(response).to be_successful?
end
In this case, after all the implicit expectations are run, the JSON-parsed response body is passed to the block to make sure that (in the example above), the body is a hash with a ‘color’ key and the ‘green’ value
30 31 32 33 34 35 |
# File 'lib/rspec-api/dsl/request/response.rb', line 30 def respond_with(status_symbol, &block) should_respond_with_status status_symbol should_respond_with_expected_headers should_respond_with_expected_body should_match_custom_response_expectations &block if block_given? end |
#should_respond_with_expected_body(options = {}) ⇒ Object
Creates an example group for expectations on the response body of the last API request and runs it to verify that it matches best practices:
-
if response is succesful and has a body
-
the body should be a JSON-marshalled Array or Hash
-
if request has a callback parameter, the body should be JSONP
-
if request has a sort parameter, the body should be sorted
-
if request has a filter parameter, the body should be filtered
-
if custom expectations are passed, they should pass
-
if some attributes are expected, the body should include them
-
18 19 20 21 22 23 24 25 26 |
# File 'lib/rspec-api/dsl/request/body.rb', line 18 def should_respond_with_expected_body( = {}) context 'responds with a body that' do it { should_be_valid_json [:type] } it { should_be_wrapped_by [:callbacks] } it { should_be_sorted_by [:sorts] } it { should_be_filtered_by [:filters] } it { should_have_attributes [:attributes] } end end |
#should_respond_with_expected_headers(options = {}) ⇒ Object
Creates an example group for expectations on the response headers of last API request and runs it to verify that it matches best practices:
-
if request has entity body, the Content-Type header should be JSON
-
if request has pages, the Link header should have a ‘rel=prev’ link
14 15 16 17 18 19 |
# File 'lib/rspec-api/dsl/request/headers.rb', line 14 def should_respond_with_expected_headers( = {}) context 'responds with headers that' do it { should_include_content_type :json } it { should_have_prev_page_link [:page] } end end |
#should_respond_with_status(status) ⇒ Object
Creates an example group for expectations on the HTTP status code of the last API request and runs it to verify that it matches status.
11 12 13 14 15 |
# File 'lib/rspec-api/dsl/request/status.rb', line 11 def should_respond_with_status(status) context 'responds with a status code that' do it { expect(response).to have_status status } end end |