Sha256: 20d00352047b1843f8a71bf5937e8ef85c95d41ba3b2457ec009ce15a18ccf8a

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

require "rack/test"
require "inheritable_accessors/inheritable_hash_accessor"

module SerialSpec
  module RequestResponse
    extend ActiveSupport::Concern
    include Rack::Test::Methods
    include InheritableAccessors::InheritableHashAccessor

    included do
      include ::SerialSpec::RequestResponse::DSL
      extend  ::SerialSpec::RequestResponse::DSL

      inheritable_hash_accessor :request_opts
      inheritable_hash_accessor :request_params
      inheritable_hash_accessor :request_envs
    end

    def perform_request!
      env = request_envs.merge(:method => request_method, :params => request_params.to_hash)
      env = current_session.send :env_for, request_path, env

      current_session.send :process_request, request_path, env
    end

    module DSL

      def request_path(new_path=nil)
        if new_path
          request_opts[:path] = new_path
        else
          path = request_opts[:path]
          return path if path
          raise "You must configure a path"
        end
      end

      # GET, POST, PUT, DELETE, OPTIONS, HEAD
      def request_method(new_method=nil)
        if new_method
          request_opts[:method] = new_method
        else
          methud = request_opts[:method]
          return methud if methud
          raise "You must configure a request method"
        end
      end


    end

    module Helpers
      extend ActiveSupport::Concern

      def status
        response.status
      end

      def headers
        response.headers
      end

      def response
        last_response
      end

      def body
        @body ||= begin
          JSON.parse(response.body)
        end
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
serial-spec-0.1.1 lib/serial_spec/request_response.rb