Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass headers to the body parsers interface #20

Merged
merged 2 commits into from
Apr 6, 2024

Conversation

skryukov
Copy link
Owner

@skryukov skryukov commented Apr 6, 2024

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

@skryukov skryukov force-pushed the pass-headers-to-body-parser branch from f77b657 to 91a1b7c Compare April 6, 2024 18:09
@skryukov skryukov merged commit ebeda47 into main Apr 6, 2024
8 checks passed
@skryukov skryukov deleted the pass-headers-to-body-parser branch April 6, 2024 18:17
@skryukov skryukov changed the title Pass headers to body parser Pass headers to the body parsers interface Apr 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant