skryukov / skooma

Skooma is a Ruby library for validating API implementations against OpenAPI documents.
MIT License
140 stars 5 forks source link

Pass headers to the body parsers interface #20

Closed skryukov closed 6 months ago

skryukov commented 6 months ago

This PR adds a breaking change to the body parsers interface in order to provide a fix to the issue #16:

# docs/openapi.yaml
#...

paths:
  /upload:
    post:
      summary: Example of Active Storage upload
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              required: [file]
              properties:
                file: {}

      responses:
        "200":
           description: successful operation
        "422":
           $ref: '#/components/responses/ErrorResponse'
# spec/rails_helper.rb
#...

Skooma::BodyParsers.register("multipart/form-data", ->(body, headers:) do
    info = Rack::Multipart::Parser.parse(
      StringIO.new(body),
      headers["Content-Length"].to_i,
      headers["Content-Type"],
      ->(*) { String.new },
      Rack::Multipart::Parser::BUFSIZE,
      Rack::Utils.default_query_parser,
    )
    info.params
  end)
# spec/requests/upload_spec.rb
# ...
  describe "POST /upload" do
    subject { post("/upload", params:) }

    let(:test_file) { file_fixture("image.jpeg") }
    let(:file) { fixture_file_upload(test_file, "image/jpeg") }

    let(:params) { {file:} }

    it { is_expected.to conform_schema(200) }

    context "without file" do
      let(:file) { nil }

      it { is_expected.to conform_response_schema(422) }
    end
  end