From 7a16ab169fb1f23b77d2bb4c608da3a3b6ae907d Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 7 Aug 2018 07:36:02 +1000 Subject: [PATCH] feat: add support for multipart/form requests --- pact.gemspec | 2 +- .../consumer_with_file_upload_spec.rb | 90 +++++++++++++++++++ spec/support/text.txt | 1 + 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 spec/features/consumer_with_file_upload_spec.rb create mode 100644 spec/support/text.txt diff --git a/pact.gemspec b/pact.gemspec index 80fd36c0..b8f0fcb4 100644 --- a/pact.gemspec +++ b/pact.gemspec @@ -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' diff --git a/spec/features/consumer_with_file_upload_spec.rb b/spec/features/consumer_with_file_upload_spec.rb new file mode 100644 index 00000000..41687003 --- /dev/null +++ b/spec/features/consumer_with_file_upload_spec.rb @@ -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 diff --git a/spec/support/text.txt b/spec/support/text.txt new file mode 100644 index 00000000..91aaf469 --- /dev/null +++ b/spec/support/text.txt @@ -0,0 +1 @@ +This is a file \ No newline at end of file