Sha256: 2540f9d07d1e966f644e24aed37e4889fae3763ac78e01b17ead5fb541c83782

Contents?: true

Size: 1.61 KB

Versions: 1

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require 'rack'
require 'json'

module CapybaraMock
  # Stub DSL
  class Stub
    def initialize(method, url)
      @method = method.to_s.upcase
      @url = url
      with
      to_return
    end

    def with(query: {}, headers: {}, body: nil)
      @query = query
      @headers = headers
      @body = body
      self
    end

    def to_return(status: 200, headers: {}, body: '', &block)
      @response = block || [status, headers, body]
      self
    end

    def call(request)
      if @response.respond_to?(:call)
        @response.call(**request)
      else
        @response
      end
    end

    def match?(request)
      match_method?(request) &&
        match_url?(request) &&
        match_query?(request) &&
        match_headers?(request) &&
        match_body?(request)
    end

    def match_method?(request)
      @method == request[:method]
    end

    def match_url?(request)
      if @url.is_a?(Regexp)
        @url.match?(request[:url])
      else
        @url == request[:url]
      end
    end

    def match_query?(request)
      @query == request[:query].slice(*@query.keys)
    end

    def match_headers?(request)
      @headers == request[:headers].slice(*@headers.keys)
    end

    def match_body?(request)
      return true unless @body

      decode_body(@body) == decode_body(request[:body])
    end

    private

    def decode_body(body)
      case @headers['Content-Type']
      when %r{application/x-www-form-urlencoded}
        Rack::Utils.parse_nested_query(body)
      when %r{application/json}
        JSON.parse(body)
      else
        body
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
capybara_mock-0.1.0 lib/capybara_mock/stub.rb