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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
require 'spec_helper'
describe WebMock::RequestSignature do
describe "initialization" do
it "assign the uri to be the normalized uri" do
expect(WebMock::Util::URI).to receive(:normalize_uri).and_return("www.example.kom")
signature = WebMock::RequestSignature.new(:get, "www.example.com")
expect(signature.uri).to eq("www.example.kom")
end
it "assigns the uri without normalization if uri is already a URI" do
expect(WebMock::Util::URI).not_to receive(:normalize_uri)
uri = Addressable::URI.parse("www.example.com")
signature = WebMock::RequestSignature.new(:get, uri)
expect(signature.uri).to eq(uri)
end
it "assigns normalized headers" do
allow(WebMock::Util::Headers).to receive(:normalize_headers).with({'A' => 'a'}.freeze).and_return('B' => 'b')
expect(
WebMock::RequestSignature.new(:get, "www.example.com", headers: {'A' => 'a'}).headers
).to eq({'B' => 'b'})
end
it "assign the body" do
expect(WebMock::RequestSignature.new(:get, "www.example.com", body: "abc").body).to eq("abc")
end
it "symbolizes the method" do
expect(WebMock::RequestSignature.new('get', "www.example.com", body: "abc").method).to eq(:get)
end
end
describe "#to_s" do
it "describes itself" do
expect(WebMock::RequestSignature.new(:get, "www.example.com",
body: "abc", headers: {'A' => 'a', 'B' => 'b'}).to_s).to eq(
"GET http://www.example.com/ with body 'abc' with headers {'A'=>'a', 'B'=>'b'}"
)
end
end
describe "#hash" do
it "reporst same hash for two signatures with the same values" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com",
body: "abc", headers: {'A' => 'a', 'B' => 'b'})
signature2 = WebMock::RequestSignature.new(:get, "www.example.com",
body: "abc", headers: {'A' => 'a', 'B' => 'b'})
expect(signature1.hash).to eq(signature2.hash)
end
it "reports different hash for two signatures with different method" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com")
signature2 = WebMock::RequestSignature.new(:put, "www.example.com")
expect(signature1.hash).not_to eq(signature2.hash)
end
it "reports different hash for two signatures with different uri" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com")
signature2 = WebMock::RequestSignature.new(:get, "www.example.org")
expect(signature1.hash).not_to eq(signature2.hash)
end
it "reports different hash for two signatures with different body" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com", body: "abc")
signature2 = WebMock::RequestSignature.new(:get, "www.example.com", body: "def")
expect(signature1.hash).not_to eq(signature2.hash)
end
it "reports different hash for two signatures with different headers" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com",
headers: {'A' => 'a'})
signature2 = WebMock::RequestSignature.new(:get, "www.example.com",
headers: {'A' => 'A'})
expect(signature1.hash).not_to eq(signature2.hash)
end
end
[:==, :eql?].each do |method|
describe method do
it "is true for two signatures with the same values" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com",
body: "abc", headers: {'A' => 'a', 'B' => 'b'})
signature2 = WebMock::RequestSignature.new(:get, "www.example.com",
body: "abc", headers: {'A' => 'a', 'B' => 'b'})
expect(signature1.send(method, signature2)).to be_truthy
end
it "is false for two signatures with different method" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com")
signature2 = WebMock::RequestSignature.new(:put, "www.example.com")
expect(signature1.send(method, signature2)).to be_falsey
end
it "is false for two signatures with different uri" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com")
signature2 = WebMock::RequestSignature.new(:get, "www.example.org")
expect(signature1.send(method, signature2)).to be_falsey
end
it "is false for two signatures with different body" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com", body: "abc")
signature2 = WebMock::RequestSignature.new(:get, "www.example.com", body: "def")
expect(signature1.send(method, signature2)).to be_falsey
end
it "is false for two signatures with different headers" do
signature1 = WebMock::RequestSignature.new(:get, "www.example.com",
headers: {'A' => 'a'})
signature2 = WebMock::RequestSignature.new(:get, "www.example.com",
headers: {'A' => 'A'})
expect(signature1.send(method, signature2)).to be_falsey
end
end
end
subject { WebMock::RequestSignature.new(:get, "www.example.com") }
describe "#url_encoded?" do
it "returns true if the headers are urlencoded" do
subject.headers = { "Content-Type" => "application/x-www-form-urlencoded" }
expect(subject.url_encoded?).to be true
end
it "returns true if the headers are urlencoded with a specified charset" do
subject.headers = { "Content-Type" => "application/x-www-form-urlencoded; charset=UTF-8" }
expect(subject.url_encoded?).to be true
end
it "returns false if the headers are NOT urlencoded" do
subject.headers = { "Content-Type" => "application/made-up-format" }
expect(subject.url_encoded?).to be false
end
it "returns false when no content type header is present" do
subject.headers = { "Some-Header" => "some-value" }
expect(subject.url_encoded?).to be false
end
it "returns false when no headers are set" do
subject.headers = nil
expect(subject.url_encoded?).to be false
end
end
describe "#json_headers?" do
it "returns true if the headers are json" do
subject.headers = { "Content-Type" => "application/json" }
expect(subject.json_headers?).to be true
end
it "returns true if the headers are json with a specified charset" do
subject.headers = { "Content-Type" => "application/json; charset=UTF-8" }
expect(subject.json_headers?).to be true
end
it "returns false if the headers are NOT json" do
subject.headers = { "Content-Type" => "application/made-up-format" }
expect(subject.json_headers?).to be false
end
it "returns false when no content type header is present" do
subject.headers = { "Some-Header" => "some-value" }
expect(subject.json_headers?).to be false
end
it "returns false when no headers are set" do
subject.headers = nil
expect(subject.json_headers?).to be false
end
end
end
|