Sha256: 387459b3a6d37d641403083b2944d92defd7c6b607a0f84cdc14ce66218e2574

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

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.exclude? 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.1.2 lib/rspec-api/dsl/request.rb