Skip to content

Commit 7a16ab1

Browse files
committed
feat: add support for multipart/form requests
1 parent c1bb89a commit 7a16ab1

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

pact.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Gem::Specification.new do |gem|
2828
gem.add_runtime_dependency 'webrick'
2929
gem.add_runtime_dependency 'term-ansicolor', '~> 1.0'
3030

31-
gem.add_runtime_dependency 'pact-support', '~> 1.4'
31+
gem.add_runtime_dependency 'pact-support', '~> 1.7'
3232
gem.add_runtime_dependency 'pact-mock_service', '~> 2.0'
3333

3434
gem.add_development_dependency 'rake', '~> 10.0.3'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
require 'net/http'
2+
require 'pact/consumer'
3+
require 'pact/consumer/rspec'
4+
require 'faraday'
5+
load 'pact/consumer/world.rb'
6+
7+
describe "A consumer with a file upload", :pact => true do
8+
9+
before :all do
10+
Pact.clear_configuration
11+
Pact.clear_consumer_world
12+
Pact.service_consumer "Consumer with a file upload" do
13+
has_pact_with "A file upload service" do
14+
mock_service :file_upload_service do
15+
verify false
16+
port 7777
17+
end
18+
end
19+
end
20+
end
21+
22+
let(:file_to_upload) { File.absolute_path("./spec/support/text.txt") }
23+
let(:payload) { { file: Faraday::UploadIO.new(file_to_upload, 'text/plain') } }
24+
25+
let(:connection) do
26+
Faraday.new(file_upload_service.mock_service_base_url + "/files") do |builder|
27+
builder.request :multipart
28+
builder.request :url_encoded
29+
builder.adapter :net_http
30+
end
31+
end
32+
33+
let(:do_request) { connection.post { |req| req.body = payload } }
34+
35+
describe "when the content matches" do
36+
37+
let(:body) do
38+
"-------------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"
39+
end
40+
41+
it "returns the mocked response and verification passes" do
42+
file_upload_service.
43+
upon_receiving("a request to upload a file").with({
44+
method: :post,
45+
path: '/files',
46+
body: body,
47+
headers: {
48+
"Content-Type" => Pact.term(/multipart\/form\-data/, "multipart/form-data; boundary=-----------RubyMultipartPost-05e76cbc2adb42ac40344eb9b35e98bc"),
49+
"Content-Length" => Pact.like("299")
50+
}
51+
}).
52+
will_respond_with({
53+
status: 200
54+
})
55+
56+
do_request
57+
58+
file_upload_service.verify("when the content matches")
59+
end
60+
end
61+
62+
describe "when the content does not match" do
63+
64+
let(:body) do
65+
"-------------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"
66+
end
67+
68+
it "the verification fails" do
69+
file_upload_service.
70+
upon_receiving("a request to upload another file").with({
71+
method: :post,
72+
path: '/files',
73+
query: "foo=bar",
74+
body: body,
75+
headers: {
76+
"Content-Type" => Pact.term(/multipart\/form\-data/, "multipart/form-data; boundary=-----------RubyMultipartPost-05e76cbc2adb42ac40344eb9b35e98bc"),
77+
"Content-Length" => Pact.like("299"),
78+
"Missing" => "header"
79+
}
80+
}).
81+
will_respond_with({
82+
status: 200
83+
})
84+
85+
do_request
86+
87+
expect { file_upload_service.verify("when the content matches") }.to raise_error /do not match/
88+
end
89+
end
90+
end

spec/support/text.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a file

0 commit comments

Comments
 (0)