Sha256: fa498a350842beb8ecd1f20ac4f4403ed9b9f56249c9f56a8a76645dde42daa6
Contents?: true
Size: 1.71 KB
Versions: 1
Compression:
Stored size: 1.71 KB
Contents
require 'rack/utils' require 'rspec-api/dsl/request/status' require 'rspec-api/dsl/request/headers' require 'rspec-api/dsl/request/body' module DSL module Request extend ActiveSupport::Concern def response # To be overriden by more specific modules OpenStruct.new # body: nil, status: nil, headers: {} end def response_body JSON response_body_without_callbacks rescue JSON::ParserError, JSON::GeneratorError nil end def response_headers response.headers || {} end def response_status response.status end def request_params {} # To be overriden by more specific modules end module ClassMethods def respond_with(status_symbol, &block) status_code = to_code status_symbol context 'responds with a status code that' do should_match_status_expectations(status_code) end context 'responds with headers that' do should_match_headers_expectations(status_code) end context 'responds with a body that' do should_match_body_expectations(status_code, &block) end end private def to_code(status_symbol) Rack::Utils.status_code status_symbol end def has_entity_body?(status_code) !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include? status_code end def success?(status_code) has_entity_body?(status_code) && status_code < 400 end end private def response_body_without_callbacks body = response.body # TODO: extract the 'a_callback' constant callback_pattern = %r[a_callback\((.*?)\)] body =~ callback_pattern ? body.match(callback_pattern)[1] : body end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rspec-api-0.2.0 | lib/rspec-api/dsl/request.rb |