-
Notifications
You must be signed in to change notification settings - Fork 558
/
request_body_diff_spec.rb
90 lines (70 loc) · 2.37 KB
/
request_body_diff_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
require 'spec_helper'
RSpec.describe WebMock::RequestBodyDiff do
subject { WebMock::RequestBodyDiff.new(request_signature, request_stub) }
let(:uri) { "http://example.com" }
let(:method) { "GET" }
let(:request_stub) { WebMock::RequestStub.new(method, uri) }
let(:request_signature) { WebMock::RequestSignature.new(method, uri) }
let(:request_stub_body) { { "key" => "value"} }
let(:request_signature_body) { {"key" => "different value"}.to_json }
let(:request_pattern) {
WebMock::RequestPattern.new(
method, uri, {body: request_stub_body}
)
}
before :each do
request_stub.request_pattern = request_pattern
request_signature.headers = {"Content-Type" => "application/json"}
request_signature.body = request_signature_body
end
describe "#body_diff" do
context "request signature is unparseable json" do
let(:request_signature_body) { "youcan'tparsethis!" }
it "returns an empty hash" do
expect(subject.body_diff).to eq({})
end
end
context "request stub body as unparseable json" do
let(:request_stub_body) { "youcan'tparsethis!" }
it "returns an empty hash" do
expect(subject.body_diff).to eq({})
end
end
context "request stub body pattern is hash" do
let(:request_stub_body) { { "key" => "value"} }
it "generates a diff" do
expect(subject.body_diff).to eq(
[["~", "key", "different value", "value"]]
)
end
end
context "request signature doesn't have json headers" do
before :each do
request_signature.headers = {"Content-Type" => "application/xml"}
end
it "returns an empty hash" do
expect(subject.body_diff).to eq({})
end
end
context "request stub body pattern is a string" do
let(:request_stub_body) { { "key" => "value"}.to_json }
it "generates a diff" do
expect(subject.body_diff).to eq(
[["~", "key", "different value", "value"]]
)
end
end
context "stub request has no request pattern" do
let(:request_signature_body) { nil }
it "returns an empty hash" do
expect(subject.body_diff).to eq({})
end
end
context "stub request has no body pattern" do
let(:request_stub_body) { nil }
it "returns an empty hash" do
expect(subject.body_diff).to eq({})
end
end
end
end