-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathtcr_spec.rb
540 lines (464 loc) · 16.7 KB
/
tcr_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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
require "spec_helper"
require "tcr"
require "net/protocol"
require "net/http"
require "net/imap"
require "net/smtp"
require "net/ldap"
require "tcr/net/ldap"
require "mail"
RSpec.describe TCR do
before(:each) do
TCR.configuration.reset_defaults!
end
around(:each) do |example|
File.unlink("test.json") if File.exist?("test.json")
File.unlink("test.yaml") if File.exist?("test.yaml")
File.unlink("test.marshal") if File.exist?("test.marshal")
example.run
File.unlink("test.json") if File.exist?("test.json")
File.unlink("test.yaml") if File.exist?("test.yaml")
File.unlink("test.marshal") if File.exist?("test.marshal")
end
describe ".configuration" do
it "has a default cassette location configured" do
TCR.configuration.cassette_library_dir.should == "fixtures/tcr_cassettes"
end
it "has an empty list of hook ports by default" do
TCR.configuration.hook_tcp_ports.should == []
end
it "defaults to erroring on read/write mismatch access" do
TCR.configuration.block_for_reads.should be_falsey
end
it "defaults to hit all to false" do
TCR.configuration.hit_all.should be_falsey
end
end
describe ".configure" do
context "with cassette_library_dir option" do
it "configures cassette location" do
expect {
TCR.configure { |c| c.cassette_library_dir = "some/dir" }
}.to change{ TCR.configuration.cassette_library_dir }.from("fixtures/tcr_cassettes").to("some/dir")
end
end
context "with hook_tcp_ports option" do
it "configures tcp ports to hook" do
expect {
TCR.configure { |c| c.hook_tcp_ports = [2525] }
}.to change{ TCR.configuration.hook_tcp_ports }.from([]).to([2525])
end
end
context "with block_for_reads option" do
before(:each) {
TCR.configure { |c|
c.hook_tcp_ports = [9999]
c.cassette_library_dir = '.'
}
}
it "configures allowing a blocking read mode" do
expect {
TCR.configure { |c| c.block_for_reads = true }
}.to change{ TCR.configuration.block_for_reads }.from(false).to(true)
end
it "blocks read thread until data is available instead of raising mismatch error" do
TCR.configure { |c| c.block_for_reads = true }
reads = Queue.new
TCR.use_cassette("spec/fixtures/block_for_reads") do
sock = TCPSocket.open("google.com", 9999)
t = Thread.new do
reads << sock.gets
end
expect(reads.size).to eq(0)
sock.print("hello\n")
t.value
expect(reads.size).to eq(1)
end
end
context "when disabled" do
it "raises mismatch error" do
TCR.use_cassette("spec/fixtures/block_for_reads") do
sock = TCPSocket.open("google.com", 9999)
expect {
Timeout::timeout(1) { sock.gets }
}.to raise_error(TCR::DirectionMismatchError)
end
end
end
end
context "with hit_all option" do
it "configures to check if all sessions were hit" do
expect {
TCR.configure { |c| c.hit_all = true }
}.to change{ TCR.configuration.hit_all }.from(false).to(true)
end
end
end
describe ".turned_off" do
it "requires a block to call" do
expect {
TCR.turned_off
}.to raise_error(ArgumentError)
end
it "returns the value" do
expect(TCR.turned_off { :foobar }).to eq(:foobar)
end
it "disables hooks within the block" do
TCR.configure { |c| c.hook_tcp_ports = [2525] }
TCR.turned_off do
TCR.configuration.hook_tcp_ports.should == []
end
end
it "makes real TCPSocket.open calls even when hooks are setup" do
TCR.configure { |c| c.hook_tcp_ports = [2525] }
expect(TCPSocket).to receive(:real_open)
TCR.turned_off do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
end
end
end
describe ".use_cassette" do
before(:each) {
TCR.configure { |c|
c.hook_tcp_ports = [2525]
c.cassette_library_dir = "."
}
}
it "MUST be used when connecting to hooked ports (or else raises an error)" do
TCR.configure { |c| c.hook_tcp_ports = [2525] }
expect {
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
}.to raise_error(TCR::NoCassetteError)
end
it "returns the value" do
expect(TCR.use_cassette("test") { :foobar }).to eq(:foobar)
end
it "requires a block to call" do
expect {
TCR.use_cassette("test")
}.to raise_error(ArgumentError)
end
context "when path to cassette does not exist" do
let(:unique_dirname) { Dir.entries(".").sort.last.next }
around do |example|
expect(Dir.exist?(unique_dirname)).to be(false)
example.run
FileUtils.rm_rf(unique_dirname)
end
it "creates it" do
expect { TCR.use_cassette("#{unique_dirname}/foo/bar/test") { } }.not_to raise_error
end
end
context "when path to cassette is not writable" do
let(:unique_dirname) { Dir.entries(".").sort.last.next }
around do |example|
FileUtils.mkdir(unique_dirname)
FileUtils.chmod("u-w", unique_dirname)
expect { FileUtils.touch("#{unique_dirname}/foo") }.to raise_error(Errno::EACCES)
example.run
FileUtils.rm_rf(unique_dirname)
end
it "raises error BEFORE block runs" do
allow(TCPSocket).to receive(:open)
expect do
TCR.use_cassette("#{unique_dirname}/foo") do
TCPSocket.open("smtp.mandrillapp.com", 2525)
end
end.to raise_error(Errno::EACCES)
expect(TCPSocket).not_to have_received(:open)
end
end
it "resets the cassette after use" do
expect(TCR.cassette).to be_nil
TCR.use_cassette("test") { }
expect(TCR.cassette).to be_nil
end
it "resets the cassette after an error" do
expect(TCR.cassette).to be_nil
expect {
TCR.use_cassette("test") { raise "Whoops!" }
}.to raise_error("Whoops!")
expect(TCR.cassette).to be_nil
end
context "when configured for JSON format" do
it "creates a cassette file on use" do
expect {
TCR.use_cassette("test") do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
end
}.to change{ File.exist?("./test.json") }.from(false).to(true)
end
it "records the tcp session data into the file" do
TCR.use_cassette("test") do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
io = Net::InternetMessageIO.new(tcp_socket)
line = io.readline
tcp_socket.close
end
cassette_contents = File.open("test.json") { |f| f.read }
cassette_contents.include?("220 smtp.mandrillapp.com ESMTP").should == true
end
end
context "when configured for YAML format" do
before { TCR.configure { |c| c.format = "yaml" } }
it "creates a cassette file on use with yaml" do
expect {
TCR.use_cassette("test") do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
end
}.to change{ File.exist?("./test.yaml") }.from(false).to(true)
end
it "records the tcp session data into the yaml file" do
TCR.use_cassette("test") do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
io = Net::InternetMessageIO.new(tcp_socket)
line = io.readline
tcp_socket.close
end
cassette_contents = File.open("test.yaml") { |f| f.read }
cassette_contents.include?("---").should == true
cassette_contents.include?("220 smtp.mandrillapp.com ESMTP").should == true
end
end
context "when configured for Marshal format" do
before { TCR.configure { |c| c.format = "marshal" } }
it "creates a cassette file on use with marshal" do
expect {
TCR.use_cassette("test") do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
end
}.to change{ File.exist?("./test.marshal") }.from(false).to(true)
end
it "records the tcp session data into the marshalled file" do
TCR.use_cassette("test") do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
io = Net::InternetMessageIO.new(tcp_socket)
line = io.readline
tcp_socket.close
end
unmarshalled_cassette = Marshal.load(File.read("test.marshal"))
expect(unmarshalled_cassette).to be_a(Array)
expect(unmarshalled_cassette.first.last.last).to eq("220 smtp.mandrillapp.com ESMTP\r\n")
end
context "when Encoding.default_internal == Encoding::UTF_8 (as in Rails)" do
before { Encoding.default_internal = Encoding::UTF_8 }
let(:invalid_unicode_string) { "\u0001\u0001\u0004€" }
it "doesn't fail on cassettes with binary content" do
expect do
TCR.use_cassette("test") do
TCR.cassette.instance_variable_get(:@sessions) << ["read", invalid_unicode_string]
end
end.not_to raise_error
end
end
end
it "plays back tcp sessions without opening a real connection" do
expect(TCPSocket).to_not receive(:real_open)
TCR.use_cassette("spec/fixtures/mandrill_smtp") do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
io = Net::InternetMessageIO.new(tcp_socket)
line = io.readline.should include("220 smtp.mandrillapp.com ESMTP")
end
end
it "raises an error if the recording gets out of order (i.e., we went to write but it expected a read)" do
expect {
TCR.use_cassette("spec/fixtures/mandrill_smtp") do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
io = Net::InternetMessageIO.new(tcp_socket)
io.write("hi")
end
}.to raise_error(TCR::DirectionMismatchError)
end
it "stubs out Socket#gets" do
TCR.configure { |c|
c.hook_tcp_ports = [993]
c.block_for_reads = true
}
expect {
TCR.use_cassette("spec/fixtures/google_imap") do
conn = Net::IMAP.new("imap.gmail.com", 993, true)
conn.login("ben.olive@example.net", "password")
conn.examine(Net::IMAP.encode_utf7("INBOX"))
conn.disconnect
end
}.not_to raise_error
end
it "stubs out Socket#read" do
TCR.configure { |c|
c.hook_tcp_ports = [23]
}
TCR.use_cassette("spec/fixtures/starwars_telnet") do
sock = TCPSocket.open("towel.blinkenlights.nl", 23)
expect(sock.read(50).length).to eq(50)
sock.close
end
end
it "supports ssl sockets" do
TCR.configure { |c| c.hook_tcp_ports = [443] }
http = Net::HTTP.new("www.google.com", 443)
http.use_ssl = true
expect {
TCR.use_cassette("spec/fixtures/google_https") do
http.request(Net::HTTP::Get.new("/"))
end
}.not_to raise_error
end
it "can stub the full session of a real server accepting a real email over SMTPS with STARTTLS" do
TCR.configure { |c|
c.hook_tcp_ports = [587]
c.block_for_reads = true
}
raw_contents = File.open("spec/fixtures/email_with_image.eml"){ |f| f.read }
message = Mail::Message.new(raw_contents)
smtp_auth_parameters = { address: "smtp.gmail.com", port: 587, user_name: "dummy", password: "dummy", enable_starttls_auto: true, authentication: :login}
message.delivery_method(:smtp, smtp_auth_parameters)
expect{
TCR.use_cassette("spec/fixtures/smtp-success") do
message.deliver
end
}.not_to raise_error
end
context "multiple connections" do
it "records multiple sessions per cassette" do
TCR.use_cassette("test") do
smtp = Net::SMTP.start("smtp.mandrillapp.com", 2525)
smtp.finish
smtp = Net::SMTP.start("mail.smtp2go.com", 2525)
smtp.finish
end
cassette_contents = File.open("test.json") { |f| f.read }
cassette_contents.include?("smtp.mandrillapp.com ESMTP").should == true
cassette_contents.include?("mail.smtp2go.com ESMTP").should == true
end
it "plays back multiple sessions per cassette in order" do
TCR.use_cassette("spec/fixtures/multitest") do
tcp_socket = TCPSocket.open("smtp.mandrillapp.com", 2525)
io = Net::InternetMessageIO.new(tcp_socket)
line = io.readline.should include("smtp.mandrillapp.com ESMTP")
tcp_socket = TCPSocket.open("mail.smtp2go.com", 2525)
io = Net::InternetMessageIO.new(tcp_socket)
line = io.readline.should include("mail.smtp2go.com ESMTP")
end
end
context "when a cassette is recorded with connections opens concurrently" do
let(:ports) { ["Apple", "Banana"].map { |payload| spawn_server(payload) } }
before(:each) do
TCR.configure { |c|
c.hook_tcp_ports = ports
c.cassette_library_dir = "."
}
end
before(:each) do
TCR.use_cassette("test") do
apple = TCPSocket.open("127.0.0.1", ports.first)
banana = TCPSocket.open("127.0.0.1", ports.last)
banana.gets
apple.gets
banana.close
apple.close
end
end
it "replays the sessions in the order they were created" do
TCR.use_cassette("test") do
apple = TCPSocket.open("127.0.0.1", ports.first)
expect(apple.gets).to eq("Apple")
end
end
end
it "raises an error if you try to playback more sessions than you previously recorded" do
expect {
TCR.use_cassette("spec/fixtures/multitest-smtp") do
smtp = Net::SMTP.start("smtp.mandrillapp.com", 2525, starttls: false)
smtp = Net::SMTP.start("mail.smtp2go.com", 2525, starttls: false)
smtp = Net::SMTP.start("mail.smtp2go.com", 2525, starttls: false)
end
}.to raise_error(TCR::NoMoreSessionsError)
end
it "raises an error if you try to playback less sessions than you previously recorded" do
expect {
TCR.use_cassette("spec/fixtures/multitest-extra-smtp", hit_all: true) do
smtp = Net::SMTP.start("smtp.mandrillapp.com", 2525, starttls: false)
end
}.to raise_error(TCR::ExtraSessionsError)
end
end
end
context "when TCPSocket.open raises an error during recording" do
before do
TCR.configure { |c|
c.format = "yaml" # JSON borks on binary strings
c.hook_tcp_ports = [143]
c.cassette_library_dir = "."
}
# record cassette
TCR.use_cassette("test") do
expect { Net::IMAP.new(nil) }.to raise_error(SystemCallError)
end
end
it "records error to cassette" do
expect(File.exist?('test.yaml')).to be(true)
expect(File.read('test.yaml')).not_to be_empty
end
it "re-raises the error during replay" do
TCR.use_cassette("test") do
expect { Net::IMAP.new(nil) }.to raise_error(SystemCallError)
end
end
end
it "replaces sockets created with Socket.tcp" do
TCR.configure { |c|
c.hook_tcp_ports = [80]
c.cassette_library_dir = "."
}
TCR.use_cassette("test") do
sock = Socket.tcp("google.com", 80)
expect(sock).to be_a(TCR::RecordableTCPSocket)
end
end
it "handles frozen Strings" do
TCR.configure { |c|
c.hook_tcp_ports = [443]
c.cassette_library_dir = "."
}
TCR.use_cassette("test") do
sock = TCPSocket.open("google.com", 443)
sock.print("hello\n".freeze)
end
end
it "supports Net::LDAP connections" do
TCR.configure { |c|
c.hook_tcp_ports = [389]
c.format = 'marshal'
c.cassette_library_dir = "."
}
expect {
TCR.use_cassette("spec/fixtures/ldap") do
ldap = Net::LDAP.new(
host: 'ldap.forumsys.com',
port: 389,
)
ldap.auth 'cn=read-only-admin,dc=example,dc=com', 'password'
ldap.search(
base: 'DC=example,DC=com',
filter: '(ou=mathematicians,dc=example,dc=com)',
scope: Net::LDAP::SearchScope_WholeSubtree,
return_result: false
) do |_entry|
break
end
end
}.not_to raise_error
end
context "when port is not in hook_tcp_ports" do
it "it honors Socket.tcp keyword arguments" do
TCR.configure { |c|
c.hook_tcp_ports = [8080]
c.cassette_library_dir = "."
}
TCR.use_cassette("test") do
sock = Socket.tcp("google.com", 80, connect_timeout: 1, resolv_timeout: 1)
expect(sock).to be_a(Socket)
end
end
end
end