|
18 | 18 | require "faraday/multipart" |
19 | 19 |
|
20 | 20 | require "protocol/http/body/file" |
| 21 | +require "protocol/multipart" |
21 | 22 |
|
22 | 23 | describe Async::HTTP::Faraday::Adapter do |
23 | 24 | def get_response(url = bound_url, path = "/index", adapter_options: {}) |
@@ -59,7 +60,6 @@ def get_response(url = bound_url, path = "/index", adapter_options: {}) |
59 | 60 | end |
60 | 61 |
|
61 | 62 | with "a local http server" do |
62 | | - include Sus::Fixtures::Async::ReactorContext |
63 | 63 | include Sus::Fixtures::Async::HTTP::ServerContext |
64 | 64 |
|
65 | 65 | with "basic http server" do |
@@ -266,14 +266,45 @@ def get_response(url = bound_url, path = "/index", adapter_options: {}) |
266 | 266 |
|
267 | 267 | expect(response).to be(:success?) |
268 | 268 | 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 |
269 | 300 |
|
270 | 301 | it "can use a multi-part post body" do |
271 | | - connection = Faraday.new do |builder| |
| 302 | + connection = Faraday.new(bound_url) do |builder| |
272 | 303 | builder.request :multipart |
273 | 304 | builder.adapter :async_http |
274 | 305 | end |
275 | 306 |
|
276 | | - response = connection.post("https://httpbin.org/post") do |request| |
| 307 | + response = connection.post("/") do |request| |
277 | 308 | request.body = {"myfile" => Faraday::Multipart::FilePart.new(StringIO.new("file content"), "text/plain", "file.txt")} |
278 | 309 | end |
279 | 310 |
|
|
0 commit comments