Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: better stubbing #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions lib/tcr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,17 @@ def turned_off(&block)
# The monkey patch shim
class TCPSocket
class << self
alias_method :real_open, :open
[:new, :open].each do |m|
alias_method "real_#{m}", m

def open(address, port, *args)
if TCR.configuration.hook_tcp_ports.include?(port)
TCR::RecordableTCPSocket.new(address, port, TCR.cassette)
else
real_open(address, port)
define_method m do |*args|
if TCR.configuration.hook_tcp_ports.include?(args[1])
TCR::RecordableTCPSocket.new(TCR.cassette) do
send("real_#{m}", *args)
end
else
send("real_#{m}", *args)
end
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/tcr/recordable_tcp_socket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ class RecordableTCPSocket
attr_reader :live, :socket
attr_accessor :recording

def initialize(address, port, cassette)
def initialize(cassette)
raise TCR::NoCassetteError.new unless TCR.cassette

@read_lock = Queue.new

if cassette.recording?
@live = true
@socket = TCPSocket.real_open(address, port)
@socket = yield
else
@live = false
end
@recording = cassette.next_session
end

def read(bytes)
_read(:read, bytes)
def read(*args)
_read(:read, *args)
end

def gets(*args)
Expand Down
19 changes: 19 additions & 0 deletions spec/tcr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@
}.to change{ File.exists?("./test.json") }.from(false).to(true)
end

it "createas a cassette file on use" do
expect {
TCR.use_cassette("test") do
TCPSocket.new("smtp.mandrillapp.com", 2525)
end
}.to change{ File.exists?("./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)
Expand Down Expand Up @@ -214,6 +222,17 @@
end
end

it "stubs out Socket#read correctly" 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.length).to_not be_zero
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the stubbing of the IO#read method is not entirely correct because a different IO#read with different arguments could return a wrong chunk.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But at least I did not introduce a new issue :)

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)
Expand Down