Sha256: 2e2b05bd89a1b74ca95a9286006e68ec6a857618f8cd180ce3616ebc8ddba2db

Contents?: true

Size: 1.3 KB

Versions: 3

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

RSpec.describe "body parsers", type: :integration do
  it "parses JSON payload for non-GET requests" do
    with_project do
      generate_action
      enable_json_body_parser

      server do
        post "/books", %({"book": {"title": "CLI apps with Ruby"}}), "CONTENT_TYPE" => "app/json", "HTTP_ACCEPT" => "app/json"
        expect(last_response.body).to eq(%({"book":{"title":"CLI apps with Ruby"}}))
      end
    end
  end

  it "doesn't eval untrusted JSON" do
    with_project do
      generate_action
      enable_json_body_parser

      server do
        post "/books", %({"json_class": "Foo"}), "CONTENT_TYPE" => "app/json", "HTTP_ACCEPT" => "app/json"
        expect(last_response.body).to eq(%({"json_class":"Foo"}))
      end
    end
  end

  private

  def generate_action
    generate "action web books#create --url=/books --method=POST"

    rewrite "apps/web/controllers/books/create.rb", <<~EOF
      require 'hanami/utils/json'
      module Web::Controllers::Books
        class Create
          include Web::Action

          def call(params)
            self.body = Hanami::Utils::Json.generate(params.to_hash)
          end
        end
      end
    EOF
  end

  def enable_json_body_parser
    inject_line_after "apps/web/app.rb", "configure do", "body_parsers :json"
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hanami-2.0.0.beta2 spec/integration/body_parsers_spec.rb
hanami-2.0.0.beta1.1 spec/integration/body_parsers_spec.rb
hanami-2.0.0.beta1 spec/integration/body_parsers_spec.rb