Sha256: c18a611dfa45063825c3e212b0eb730da8a2058328dbbe8c43d882b05e12ad6b

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 KB

Contents

require 'active_support/hash_with_indifferent_access'
require 'hyperion_test/kim/matcher'

class Hyperion
  class Kim
    module Matchers
      # Some useful matchers to include in your code

      def res(resource_pattern)
        regex = resource_pattern.gsub(/:([^\/]+)/, "(?<\\1>[^\\/]+)")
        Matcher.new do |req|
          m = req.path.match(regex)
          m && req.merge_params(m.names.zip(m.captures).to_h)
        end
      end

      def verb(verb_to_match)
        Matcher.new do |req|
          req.verb.to_s.upcase == verb_to_match.to_s.upcase
        end
      end

      def req_headers(required_headers)
        Matcher.new do |req|
          required_headers.each_pair.all? do |(k, v)|
            hash_includes?(req.headers.to_h, k, v)
          end
        end
      end

      def req_params(required_params)
        Matcher.new do |req|
          required_params.each_pair.all? do |(k, v)|
            hash_includes?(req.params.to_h, k, v)
          end
        end
      end

      private

      def hash_includes?(h, k, v)
        (h.keys.include?(k.to_s) || h.keys.include?(k.to_sym)) && (v.nil? || (h[k.to_s] || h[k.to_sym]) == v)
      end

      extend self
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
hyperion_http-0.6.0 lib/hyperion_test/kim/matchers.rb
hyperion_http-0.5.0 lib/hyperion_test/kim/matchers.rb
hyperion_http-0.3.0 lib/hyperion_test/kim/matchers.rb
hyperion_http-0.2.4 lib/hyperion_test/kim/matchers.rb
hyperion_http-0.2.3 lib/hyperion_test/kim/matchers.rb
hyperion_http-0.2.2 lib/hyperion_test/kim/matchers.rb
hyperion_http-0.2.1 lib/hyperion_test/kim/matchers.rb