-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for multipart/form
- Loading branch information
Showing
7 changed files
with
144 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'pact/matchers/unix_diff_formatter' | ||
require 'pact/matchers/differ' | ||
|
||
module Pact | ||
module Matchers | ||
class MultipartFormDiffFormatter | ||
|
||
def initialize diff, options = {} | ||
@options = options | ||
@body_diff = diff[:body] | ||
@non_body_diff = diff.reject{ |k, v| k == :body } | ||
@colour = options.fetch(:colour, false) | ||
@differ = Pact::Matchers::Differ.new(@colour) | ||
end | ||
|
||
def self.call diff, options = {} | ||
new(diff, options).call | ||
end | ||
|
||
def call | ||
Pact::Matchers::UnixDiffFormatter::MESSAGES_TITLE + "\n" + non_body_diff_string + "\n" + body_diff_string | ||
end | ||
|
||
def non_body_diff_string | ||
if @non_body_diff.any? | ||
Pact::Matchers::ExtractDiffMessages.call(@non_body_diff).collect{ | message| "* #{message}" }.join("\n") | ||
else | ||
"" | ||
end | ||
end | ||
|
||
def body_diff_string | ||
if @body_diff | ||
@differ.diff_as_string(@body_diff.expected, @body_diff.actual) | ||
else | ||
"" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'uri' | ||
require 'pact/shared/text_differ' | ||
|
||
module Pact | ||
class MultipartFormDiffer | ||
def self.call expected, actual, options = {} | ||
require 'pact/matchers' # avoid recursive loop between this file and pact/matchers | ||
expected_boundary = expected.split.first | ||
actual_boundary = actual.split.first | ||
actual_with_hardcoded_boundary = actual.gsub(actual_boundary, expected_boundary) | ||
TextDiffer.call(expected, actual_with_hardcoded_boundary, options) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
|
||
Description of differences | ||
-------------------------------------- | ||
* Wrong header | ||
|
||
@@ -1,2 +1,2 @@ | ||
-bar | ||
+foo |
36 changes: 36 additions & 0 deletions
36
spec/lib/pact/matchers/multipart_form_diff_formatter_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require 'pact/matchers/multipart_form_diff_formatter' | ||
|
||
module Pact | ||
module Matchers | ||
describe MultipartFormDiffFormatter do | ||
describe ".call" do | ||
subject { MultipartFormDiffFormatter.call(diff, options)} | ||
|
||
let(:diff) do | ||
{ | ||
headers: header_diff, | ||
body: body_diff | ||
} | ||
end | ||
|
||
let(:header_diff) do | ||
{ | ||
"Content-Type" => Difference.new("foo", "bar", "Wrong header") | ||
} | ||
end | ||
|
||
let(:body_diff) do | ||
Difference.new("foo", "bar", "A message") | ||
end | ||
|
||
let(:options) { {} } | ||
|
||
let(:expected_output) { File.read("spec/fixtures/multipart-form-diff.txt")} | ||
|
||
it "formats the diff" do | ||
expect(subject).to eq expected_output | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'pact/shared/multipart_form_differ' | ||
|
||
module Pact | ||
describe MultipartFormDiffer do | ||
|
||
describe ".call" do | ||
|
||
let(:expected_body) do | ||
"-------------RubyMultipartPost-1e4912957c7bb64de3c444568326663b\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-1e4912957c7bb64de3c444568326663b--\r\n\r\n" | ||
end | ||
|
||
let(:actual_body) do | ||
"-------------RubyMultipartPost-1e4912957c7bb64de3c4445683266XXX\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-1e4912957c7bb64de3c4445683266XXX--\r\n\r\n" | ||
end | ||
|
||
let(:options) do | ||
{} | ||
end | ||
|
||
subject { MultipartFormDiffer.call(expected_body, actual_body, options) } | ||
|
||
context "when the bodies are the same apart from the boundary" do | ||
it "returns an empty diff" do | ||
expect(subject).to eq({}) | ||
end | ||
end | ||
|
||
context "when the bodies are not the same" do | ||
let(:actual_body) do | ||
"-------------RubyMultipartPost-1e4912957c7bb64de3c4445683266XXX\r\nContent-Disposition: form-data; name=\"file\"; filename=\"bar.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-1e4912957c7bb64de3c4445683266XXX--\r\n\r\n" | ||
end | ||
|
||
it "returns a text diff" do | ||
expect(subject).to_not eq({}) | ||
end | ||
end | ||
end | ||
end | ||
end |