Sha256: 3904ae6a6baa94bff34618dbd3e3f9c884240e7c9c35515c7212c1a0b928b81e

Contents?: true

Size: 1.44 KB

Versions: 24

Compression:

Stored size: 1.44 KB

Contents

# frozen_string_literal: true

require 'base64'

module Jets
  module SpecHelpers
    autoload :Params, 'jets/spec_helpers/params'
    autoload :Request, 'jets/spec_helpers/request'
    autoload :Response, 'jets/spec_helpers/response'

    attr_reader :request, :response
    def initialize(*)
      super
      @request = Request.new(:get, '/', {}, Params.new)
      @response = nil # will be set after http_call
    end

    rest_methods = %w[get post put patch delete]
    rest_methods.each do |meth|
      define_method(meth) do |path, **params|
        http_call(method: meth, path: path, **params)
      end
      # Example:
      # def get(path, **params)
      #   http_call(method: :get, path: path, **params)
      # end
    end

    def http_call(method:, path:, **params)
      request.method = method.to_sym
      request.path = path
      request.headers.deep_merge!(params.delete(:headers) || {})

      request.params.body_params = params.delete(:params) || params || {}

      request.params.query_params = params.delete(:query)
      request.params.query_params ||= params if request.method == :get
      request.params.query_params ||= {}

      request.params.path_params = params

      @response = request.dispatch!
    end

    def fixture_path(filename)
      "#{Jets.root}/spec/fixtures/#{filename}"
    end

    def fixture_file(filename)
      File.new(fixture_path(filename))
    end
  end
end

RSpec.configure do |c|
  c.include Jets::SpecHelpers
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
jets-1.9.7 lib/jets/spec_helpers.rb
jets-1.9.6 lib/jets/spec_helpers.rb
jets-1.9.5 lib/jets/spec_helpers.rb
jets-1.9.4 lib/jets/spec_helpers.rb
jets-1.9.3 lib/jets/spec_helpers.rb
jets-1.9.2 lib/jets/spec_helpers.rb
jets-1.9.1 lib/jets/spec_helpers.rb
jets-1.9.0 lib/jets/spec_helpers.rb
jets-1.8.14 lib/jets/spec_helpers.rb
jets-1.8.13 lib/jets/spec_helpers.rb
jets-1.8.12 lib/jets/spec_helpers.rb
jets-1.8.11 lib/jets/spec_helpers.rb
jets-1.8.10 lib/jets/spec_helpers.rb
jets-1.8.9 lib/jets/spec_helpers.rb
jets-1.8.8 lib/jets/spec_helpers.rb
jets-1.8.7 lib/jets/spec_helpers.rb
jets-1.8.6 lib/jets/spec_helpers.rb
jets-1.8.5 lib/jets/spec_helpers.rb
jets-1.8.4 lib/jets/spec_helpers.rb
jets-1.8.3 lib/jets/spec_helpers.rb