Skip to content

Commit

Permalink
feat: add support for multipart/form requests
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Aug 6, 2018
1 parent c1bb89a commit 7a16ab1
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pact.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'webrick'
gem.add_runtime_dependency 'term-ansicolor', '~> 1.0'

gem.add_runtime_dependency 'pact-support', '~> 1.4'
gem.add_runtime_dependency 'pact-support', '~> 1.7'
gem.add_runtime_dependency 'pact-mock_service', '~> 2.0'

gem.add_development_dependency 'rake', '~> 10.0.3'
Expand Down
90 changes: 90 additions & 0 deletions spec/features/consumer_with_file_upload_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
require 'net/http'
require 'pact/consumer'
require 'pact/consumer/rspec'
require 'faraday'
load 'pact/consumer/world.rb'

describe "A consumer with a file upload", :pact => true do

before :all do
Pact.clear_configuration
Pact.clear_consumer_world
Pact.service_consumer "Consumer with a file upload" do
has_pact_with "A file upload service" do
mock_service :file_upload_service do
verify false
port 7777
end
end
end
end

let(:file_to_upload) { File.absolute_path("./spec/support/text.txt") }
let(:payload) { { file: Faraday::UploadIO.new(file_to_upload, 'text/plain') } }

let(:connection) do
Faraday.new(file_upload_service.mock_service_base_url + "/files") do |builder|
builder.request :multipart
builder.request :url_encoded
builder.adapter :net_http
end
end

let(:do_request) { connection.post { |req| req.body = payload } }

describe "when the content matches" do

let(:body) do
"-------------RubyMultipartPost-05e76cbc2adb42ac40344eb9b35e98bc\r\nContent-Disposition: form-data; name=\"file\"; filename=\"text.txt\"\r\nContent-Length: 14\r\nContent-Type: text/plain\r\nContent-Transfer-Encoding: binary\r\n\r\nThis is a file\r\n-------------RubyMultipartPost-05e76cbc2adb42ac40344eb9b35e98bc--\r\n\r\n"
end

it "returns the mocked response and verification passes" do
file_upload_service.
upon_receiving("a request to upload a file").with({
method: :post,
path: '/files',
body: body,
headers: {
"Content-Type" => Pact.term(/multipart\/form\-data/, "multipart/form-data; boundary=-----------RubyMultipartPost-05e76cbc2adb42ac40344eb9b35e98bc"),
"Content-Length" => Pact.like("299")
}
}).
will_respond_with({
status: 200
})

do_request

file_upload_service.verify("when the content matches")
end
end

describe "when the content does not match" do

let(:body) do
"-------------RubyMultipartPost-05e76cbc2adb42ac40344eb9b35e98bc\r\nContent-Disposition: form-data; name=\"file\"; filename=\"TEXT.txt\"\r\nContent-Length: 14\r\nContent-Type: text/plain\r\nContent-Transfer-Encoding: binary\r\n\r\nThis is a file\r\n-------------RubyMultipartPost-05e76cbc2adb42ac40344eb9b35e98bc--\r\n\r\n"
end

it "the verification fails" do
file_upload_service.
upon_receiving("a request to upload another file").with({
method: :post,
path: '/files',
query: "foo=bar",
body: body,
headers: {
"Content-Type" => Pact.term(/multipart\/form\-data/, "multipart/form-data; boundary=-----------RubyMultipartPost-05e76cbc2adb42ac40344eb9b35e98bc"),
"Content-Length" => Pact.like("299"),
"Missing" => "header"
}
}).
will_respond_with({
status: 200
})

do_request

expect { file_upload_service.verify("when the content matches") }.to raise_error /do not match/
end
end
end
1 change: 1 addition & 0 deletions spec/support/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This is a file

0 comments on commit 7a16ab1

Please sign in to comment.