Sha256: 62369f592ab610e1fb2487cc4df5ce6655d7927ea55f7fdbdfaf51b7afc49097

Contents?: true

Size: 1.26 KB

Versions: 36

Compression:

Stored size: 1.26 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.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

36 entries across 36 versions & 1 rubygems

Version Path
jets-2.3.19 lib/jets/spec_helpers/controllers.rb
jets-2.3.18 lib/jets/spec_helpers/controllers.rb
jets-2.3.17 lib/jets/spec_helpers/controllers.rb
jets-2.3.16 lib/jets/spec_helpers/controllers.rb
jets-2.3.15 lib/jets/spec_helpers/controllers.rb
jets-2.3.14 lib/jets/spec_helpers/controllers.rb
jets-2.3.13 lib/jets/spec_helpers/controllers.rb
jets-2.3.12 lib/jets/spec_helpers/controllers.rb
jets-2.3.11 lib/jets/spec_helpers/controllers.rb
jets-2.3.10 lib/jets/spec_helpers/controllers.rb
jets-2.3.9 lib/jets/spec_helpers/controllers.rb
jets-2.3.8 lib/jets/spec_helpers/controllers.rb
jets-2.3.7 lib/jets/spec_helpers/controllers.rb
jets-2.3.6 lib/jets/spec_helpers/controllers.rb
jets-2.3.5 lib/jets/spec_helpers/controllers.rb
jets-2.3.4 lib/jets/spec_helpers/controllers.rb
jets-2.3.3 lib/jets/spec_helpers/controllers.rb
jets-2.3.2 lib/jets/spec_helpers/controllers.rb
jets-2.3.1 lib/jets/spec_helpers/controllers.rb
jets-2.3.0 lib/jets/spec_helpers/controllers.rb