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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
require 'spec_helper'
describe WebMock::ResponseFactory do
describe "response_for" do
it "should create response with options passed as arguments" do
options = {body: "abc", headers: {a: :b}}
expect(WebMock::Response).to receive(:new).with(options).and_return(@response = double(WebMock::Response))
expect(WebMock::ResponseFactory.response_for(options)).to eq(@response)
end
it "should create dynamic response for argument responding to call" do
callable = double(call: {body: "abc"})
expect(WebMock::DynamicResponse).to receive(:new).with(callable).and_return(@response = double(WebMock::Response))
expect(WebMock::ResponseFactory.response_for(callable)).to eq(@response)
end
end
end
describe WebMock::Response do
before(:each) do
@response = WebMock::Response.new(headers: {'A' => 'a'})
end
it "should raise an error when initialized with unknown option" do
expect { WebMock::Response.new(foo: "bar") }.to raise_error('Unknown key: "foo". Valid keys are: "headers", "status", "body", "exception", "should_timeout"')
end
it "should report normalized headers" do
allow(WebMock::Util::Headers).to receive(:normalize_headers).with({'A' => 'a'}.freeze).and_return('B' => 'b')
@response = WebMock::Response.new(headers: {'A' => 'a'})
expect(@response.headers).to eq({'B' => 'b'})
end
describe "status" do
it "should have 200 code and empty message by default" do
expect(@response.status).to eq([200, ""])
end
it "should return assigned status" do
@response = WebMock::Response.new(status: 500)
expect(@response.status).to eq([500, ""])
end
it "should return assigned message" do
@response = WebMock::Response.new(status: [500, "Internal Server Error"])
expect(@response.status).to eq([500, "Internal Server Error"])
end
end
describe "raising error" do
it "should raise error if any assigned" do
@response = WebMock::Response.new(exception: ArgumentError)
expect {
@response.raise_error_if_any
}.to raise_error(ArgumentError, "Exception from WebMock")
end
it "should raise error if any assigned as instance" do
@response = WebMock::Response.new(exception: ArgumentError.new("hello world"))
expect {
@response.raise_error_if_any
}.to raise_error(ArgumentError, "hello world")
end
it "should raise error if any assigned as string" do
@response = WebMock::Response.new(exception: "hello world")
expect {
@response.raise_error_if_any
}.to raise_error("hello world")
end
it "should not raise error if no error assigned" do
@response.raise_error_if_any
end
end
describe "timeout" do
it "should know if it should timeout" do
@response = WebMock::Response.new(should_timeout: true)
expect(@response.should_timeout).to be_truthy
end
it "should not timeout by default" do
@response = WebMock::Response.new
expect(@response.should_timeout).to be_falsey
end
end
describe "body" do
it "should return empty body by default" do
expect(@response.body).to eq('')
end
it "should return unfrozen string empty body by default" do
expect(@response.body).to_not be_frozen
end
it "shoud return utf-8 encoded string by default" do
expect(@response.body.encoding).to eq(Encoding::UTF_8)
end
it "should report body if assigned" do
@response = WebMock::Response.new(body: "abc")
expect(@response.body).to eq("abc")
end
it "should report string even if existing file path was provided" do
@response = WebMock::Response.new(body: __FILE__)
expect(@response.body).to eq(__FILE__)
end
it "should report content of a IO object if provided" do
@response = WebMock::Response.new(body: File.new(__FILE__))
expect(@response.body).to eq(File.read(__FILE__))
end
it "should report many times content of a IO object if provided" do
@response = WebMock::Response.new(body: File.new(__FILE__))
expect(@response.body).to eq(File.read(__FILE__))
expect(@response.body).to eq(File.read(__FILE__))
end
it "should work with Pathnames" do
@response = WebMock::Response.new(body: Pathname.new(__FILE__))
expect(@response.body).to eq(File.read(__FILE__))
end
# Users of webmock commonly make the mistake of stubbing the response
# body to return a hash, to prevent this:
#
it "should error if given a non-allowed type: a hash" do
expect { WebMock::Response.new(body: Hash.new) }.to \
raise_error(
WebMock::Response::InvalidBody,
"must be one of: [Proc, IO, Pathname, String, Array], but you've used a Hash. Please convert it by calling .to_json .to_xml, or otherwise convert it to a string."
)
end
it "should error if given a non-allowed type: something that is not a hash" do
expect { WebMock::Response.new(body: 123) }.to \
raise_error(WebMock::Response::InvalidBody,
"must be one of: [Proc, IO, Pathname, String, Array]. 'Integer' given.")
end
end
describe "from raw response" do
describe "when input is a StringIO" do
before(:each) do
@io = StringIO.new(File.read(CURL_EXAMPLE_OUTPUT_PATH))
@response = WebMock::Response.new(@io)
end
it "should read status" do
expect(@response.status).to eq([202, "OK"])
end
it "should read headers" do
expect(@response.headers).to eq(
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8",
"Content-Length"=>"419",
"Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png"
)
end
it "should read body" do
expect(@response.body.size).to eq(419)
end
it "should close IO" do
expect(@io).to be_closed
end
end
describe "when input is IO" do
before(:each) do
@file = File.new(CURL_EXAMPLE_OUTPUT_PATH)
@response = WebMock::Response.new(@file)
end
it "should read status" do
expect(@response.status).to eq([202, "OK"])
end
it "should read headers" do
expect(@response.headers).to eq({
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8",
"Content-Length"=>"419",
"Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png"
})
end
it "should read body" do
expect(@response.body.size).to eq(419)
end
it "should close IO" do
expect(@file).to be_closed
end
end
describe "when input is String" do
before(:each) do
@input = File.read(CURL_EXAMPLE_OUTPUT_PATH)
@response = WebMock::Response.new(@input)
end
it "should read status" do
expect(@response.status).to eq([202, "OK"])
end
it "should read headers" do
expect(@response.headers).to eq({
"Date"=>"Sat, 23 Jan 2010 01:01:05 GMT",
"Content-Type"=>"text/html; charset=UTF-8",
"Content-Length"=>"419",
"Connection"=>"Keep-Alive",
"Accept"=>"image/jpeg, image/png"
})
end
it "should read body" do
expect(@response.body.size).to eq(419)
end
it "should work with transfer-encoding set to chunked" do
@input.gsub!("Content-Length: 419", "Transfer-Encoding: chunked")
@response = WebMock::Response.new(@input)
expect(@response.body.size).to eq(419)
end
end
describe "with dynamically evaluated options" do
before(:each) do
@request_signature = WebMock::RequestSignature.new(:post, "www.example.com", body: "abc", headers: {'A' => 'a'})
end
it "should have evaluated body" do
@response = WebMock::Response.new(body: lambda {|request| request.body})
expect(@response.evaluate(@request_signature).body).to eq("abc")
end
it "should have evaluated headers" do
@response = WebMock::Response.new(headers: lambda {|request| request.headers})
expect(@response.evaluate(@request_signature).headers).to eq({'A' => 'a'})
end
it "should have evaluated status" do
@response = WebMock::Response.new(status: lambda {|request| 302})
expect(@response.evaluate(@request_signature).status).to eq([302, ""])
end
end
end
describe WebMock::DynamicResponse do
describe "evaluating response options" do
it "should evaluate new response with evaluated options" do
request_signature = WebMock::RequestSignature.new(:post, "www.example.com", body: "abc", headers: {'A' => 'a'})
response = WebMock::DynamicResponse.new(lambda {|request|
{
body: request.body,
headers: request.headers,
status: 302
}
})
evaluated_response = response.evaluate(request_signature)
expect(evaluated_response.body).to eq("abc")
expect(evaluated_response.headers).to eq({'A' => 'a'})
expect(evaluated_response.status).to eq([302, ""])
end
it "should be equal to static response after evaluation" do
request_signature = WebMock::RequestSignature.new(:post, "www.example.com", body: "abc")
response = WebMock::DynamicResponse.new(lambda {|request| {body: request.body}})
evaluated_response = response.evaluate(request_signature)
expect(evaluated_response).to eq(WebMock::Response.new(body: "abc"))
end
describe "when raw response is evaluated" do
before(:each) do
@files = {
"www.example.com" => File.new(CURL_EXAMPLE_OUTPUT_PATH)
}
@request_signature = WebMock::RequestSignature.new(:get, "www.example.com")
end
describe "as a file" do
it "should return response" do
response = WebMock::DynamicResponse.new(lambda {|request| @files[request.uri.host.to_s] })
expect(response.evaluate(@request_signature).body.size).to eq(419)
end
end
describe "as a string" do
it "should return response" do
response = WebMock::DynamicResponse.new(lambda {|request| @files[request.uri.host.to_s].read })
expect(response.evaluate(@request_signature).body.size).to eq(419)
end
end
end
end
end
end
|