Skip to content

Commit 77bd72e

Browse files
committed
Use local server for multi-part test.
1 parent ec663c3 commit 77bd72e

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

gems.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,7 @@
3232
gem "sus-fixtures-async"
3333
gem "sus-fixtures-async-http"
3434

35+
gem "protocol-multipart"
36+
3537
gem "openssl", "~> 4.0"
3638
end

test/async/http/faraday/adapter.rb

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
require "faraday/multipart"
1919

2020
require "protocol/http/body/file"
21+
require "protocol/multipart"
2122

2223
describe Async::HTTP::Faraday::Adapter do
2324
def get_response(url = bound_url, path = "/index", adapter_options: {})
@@ -59,7 +60,6 @@ def get_response(url = bound_url, path = "/index", adapter_options: {})
5960
end
6061

6162
with "a local http server" do
62-
include Sus::Fixtures::Async::ReactorContext
6363
include Sus::Fixtures::Async::HTTP::ServerContext
6464

6565
with "basic http server" do
@@ -266,14 +266,45 @@ def get_response(url = bound_url, path = "/index", adapter_options: {})
266266

267267
expect(response).to be(:success?)
268268
end
269+
end
270+
271+
with "a multi-part post body" do
272+
include Sus::Fixtures::Async::HTTP::ServerContext
273+
274+
let(:app) do
275+
Protocol::HTTP::Middleware.for do |request|
276+
# Parse the multipart body and extract file content
277+
content_type = request.headers["content-type"]
278+
boundary = content_type[/boundary=(.+)$/, 1]
279+
280+
body_content = request.body.read
281+
readable = StringIO.new(body_content)
282+
parser = Protocol::Multipart::Parser.new(readable, boundary)
283+
284+
files = {}
285+
286+
parser.each do |part|
287+
# Extract the field name from Content-Disposition header
288+
if part.headers["content-disposition"] =~ /name="([^"]+)"/
289+
name = $1
290+
content = String.new
291+
part.each { |chunk| content << chunk }
292+
files[name] = content
293+
end
294+
end
295+
296+
response_body = JSON.generate({"files" => files})
297+
Protocol::HTTP::Response[200, {"content-type" => "application/json"}, [response_body]]
298+
end
299+
end
269300

270301
it "can use a multi-part post body" do
271-
connection = Faraday.new do |builder|
302+
connection = Faraday.new(bound_url) do |builder|
272303
builder.request :multipart
273304
builder.adapter :async_http
274305
end
275306

276-
response = connection.post("https://httpbin.org/post") do |request|
307+
response = connection.post("/") do |request|
277308
request.body = {"myfile" => Faraday::Multipart::FilePart.new(StringIO.new("file content"), "text/plain", "file.txt")}
278309
end
279310

0 commit comments

Comments
 (0)