Sha256: 92d55ad38ac46998fa5fca773211a7ff29597e33d8e062ecf0b9817027c88b57

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

module Jets::SpecHelpers
  module Controllers
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jets-2.0.3 lib/jets/spec_helpers/controllers.rb