Sha256: 60a177f2067350a144c4bf82ebc657746822bd16ea4f7f83f40848a79e09a3b1

Contents?: true

Size: 1.48 KB

Versions: 43

Compression:

Stored size: 1.48 KB

Contents

module Jets::SpecHelpers
  module Controllers
    include Jets::Router::Helpers # must be at the top because response is overridden later

    attr_reader :response
    # Note: caching it like this instead of within the initialize results in the headers not being cached
    # See: https://community.rubyonjets.com/t/is-jets-spechelpers-controllers-request-being-cached/244/2
    def request
      @request ||= Request.new(:get, '/', {}, Params.new)
    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.query_params = params.delete(:query)

      if request.method == :get
        request.params.body_params = {}
        request.params.query_params ||= params.delete(:params)
        request.params.query_params ||= params
      else
        request.params.body_params = params.delete(:body)
        request.params.body_params ||= params.delete(:params)
        request.params.body_params ||= params
      end

      request.params.query_params ||= {}

      request.params.path_params = params

      @response = request.dispatch!
    end
  end
end

Version data entries

43 entries across 43 versions & 2 rubygems

Version Path
jets-3.1.2 lib/jets/spec_helpers/controllers.rb
jets-3.1.1 lib/jets/spec_helpers/controllers.rb
jets-3.1.0 lib/jets/spec_helpers/controllers.rb
jets-3.0.23 lib/jets/spec_helpers/controllers.rb
jets-3.0.22 lib/jets/spec_helpers/controllers.rb
jets-3.0.21 lib/jets/spec_helpers/controllers.rb
jets-3.0.20 lib/jets/spec_helpers/controllers.rb
jets-3.0.19 lib/jets/spec_helpers/controllers.rb
jets-3.0.18 lib/jets/spec_helpers/controllers.rb
jets-3.0.17 lib/jets/spec_helpers/controllers.rb
jets-3.0.16 lib/jets/spec_helpers/controllers.rb
jets-3.0.15 lib/jets/spec_helpers/controllers.rb
jets-3.0.14 lib/jets/spec_helpers/controllers.rb
jets-3.0.13 lib/jets/spec_helpers/controllers.rb
jets-3.0.12 lib/jets/spec_helpers/controllers.rb
jets-3.0.11 lib/jets/spec_helpers/controllers.rb
jets-3.0.10 lib/jets/spec_helpers/controllers.rb
jets-3.0.9 lib/jets/spec_helpers/controllers.rb
jets-3.0.8 lib/jets/spec_helpers/controllers.rb
jets-3.0.7 lib/jets/spec_helpers/controllers.rb