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-4.0.12 lib/jets/spec_helpers/controllers.rb
jets-4.0.11 lib/jets/spec_helpers/controllers.rb
jets-4.0.10 lib/jets/spec_helpers/controllers.rb
jets-4.0.9 lib/jets/spec_helpers/controllers.rb
jets-4.0.8 lib/jets/spec_helpers/controllers.rb
jets-4.0.7 lib/jets/spec_helpers/controllers.rb
jets-4.0.6 lib/jets/spec_helpers/controllers.rb
jets-4.0.5 lib/jets/spec_helpers/controllers.rb
jets-4.0.4 lib/jets/spec_helpers/controllers.rb
jets-4.0.3 lib/jets/spec_helpers/controllers.rb
jets-4.0.2 lib/jets/spec_helpers/controllers.rb
jets-4.0.1 lib/jets/spec_helpers/controllers.rb
jets-4.0.0 lib/jets/spec_helpers/controllers.rb
jets-3.2.2 lib/jets/spec_helpers/controllers.rb
jets-3.2.1 lib/jets/spec_helpers/controllers.rb
jets.benforeva-3.0.17.pre.mount.pre.fix lib/jets/spec_helpers/controllers.rb
jets-3.2.0 lib/jets/spec_helpers/controllers.rb
jets-3.1.5 lib/jets/spec_helpers/controllers.rb
jets-3.1.4 lib/jets/spec_helpers/controllers.rb
jets-3.1.3 lib/jets/spec_helpers/controllers.rb