我一直在嘗試使用webmock存根多部分請求一段時間,並沒有找到一個令人滿意的解決方案。存根webmock/rspec存根多部分請求
理想情況下,我想存根要求如下:
stub_request(:post, 'http://test.api.com').with(:body => { :file1 => File.new('filepath1'), file2 => File.new('filepath2') })
然而,這似乎並沒有工作和RSpec抱怨該請求沒有被掐滅。打印非殘留請求:
stub_request(:post, "http://test.api.com").
with(:body => "--785340\r\nContent-Disposition: form-data; name=\"file1\"; filename=\"filepath1\"\r\nContent-Type: text/plain\r\n\r\nhello\r\n--785340\r\nContent-Disposition: form-data; name=\"file2\"; filename=\"filepath2\"\r\nContent-Type: text/plain\r\n\r\nhello2\r\n--785340\r\n",
:headers => {'Accept'=>'*/*; q=0.5, application/xml', 'Accept-Encoding'=>'gzip, deflate', 'Content-Length'=>'664', 'Content-Type'=>'multipart/form-data; boundary=785340', 'User-Agent'=>'Ruby'}).
to_return(:status => 200, :body => "", :headers => {})
當然,由於邊界是動態生成的,因此我無法真正遵循此建議。任何想法如何我可以妥善存根這些請求?
謝謝! Bruno