Skip to content

Commit

Permalink
Fix Action Mailbox assuming request.body present
Browse files Browse the repository at this point in the history
In Rack 3.1, `rack.intput` (`request.body`) is no longer guaranteed to
be present, so we can no longer unconditionally call `read` on it.

I tried adding a test:

```diff
diff --git a/actionmailbox/test/controllers/ingresses/relay/inbound_emails_controller_test.rb b/actionmailbox/test/controllers/ingresses/relay/inbound_emails_controller_test.rb
index dcf698ae4a..bec607ce99 100644
--- a/actionmailbox/test/controllers/ingresses/relay/inbound_emails_controller_test.rb
+++ b/actionmailbox/test/controllers/ingresses/relay/inbound_emails_controller_test.rb
@@ -31,6 +31,15 @@ class ActionMailbox::Ingresses::Relay::InboundEmailsControllerTest < ActionDispa
     assert_equal "[email protected]", inbound_email.message_id
   end

+  test "rejecting a request with no body" do
+    assert_no_difference -> { ActionMailbox::InboundEmail.count } do
+      post rails_relay_inbound_emails_url, headers: { "Authorization" => credentials, "Content-Type" => "message/rfc822" }
+    end
+
+    assert_response :unprocessable_entity
+  end
+
   test "rejecting an unauthorized inbound email" do
     assert_no_difference -> { ActionMailbox::InboundEmail.count } do
       post rails_relay_inbound_emails_url, headers: { "Content-Type" => "message/rfc822" },
```

However, this doesn't currently work because `IntegrationTest` ends up
calling `Rack::Test::Session#request` (which goes through
`Rack::MockRequest`) and forces `rack.input` to be present in the test.
  • Loading branch information
skipkayhil committed Jun 27, 2024
1 parent 1d3fb3c commit 8699a5d
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ class Ingresses::Relay::InboundEmailsController < ActionMailbox::BaseController
before_action :authenticate_by_password, :require_valid_rfc822_message

def create
ActionMailbox::InboundEmail.create_and_extract_message_id! request.body.read
if request.body
ActionMailbox::InboundEmail.create_and_extract_message_id! request.body.read
else
head :unproccessable_entity
end
end

private
Expand Down

0 comments on commit 8699a5d

Please sign in to comment.