Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

WebMock for TCP Socket connections?

601 views
Skip to first unread message

Andrew Havens

unread,
Feb 17, 2015, 6:21:24 PM2/17/15
to webmoc...@googlegroups.com
Hello,

I am a happy webmock user. However, I recently came across a situation where webmock did not seem to meet my needs. I am working on building an API client for an API that uses TCP sockets. I have embarrassingly little experience working with sockets, but reviewing the code that is used to interact with the API, it seems like it might not be too difficult to mock. Here's a snippet of the code:

# connect to the API
@connection = TCPSocket.new(@server, @port)
@socket = OpenSSL::SSL::SSLSocket.new(@connection) if @connection

# send a command to the API
if @socket and not @socket.closed?
  @socket.write("SOME API COMMAND")
end

# read the response
line = @socket.gets # actually loop until all lines are read

# close the connection


@socket.close if @socket and not @socket.closed?
@connection.close if @connection and not @connection.closed?

My question is whether or not webmock would be interested in supporting these types of API requests, or would be better suited for a different library. Or better yet, is this kind of mocking already implemented in a gem that I don't yet know about?

Thanks,
Andrew Havens

Bartosz Blimke

unread,
Feb 18, 2015, 4:43:19 AM2/18/15
to webmoc...@googlegroups.com
Hi Andrew,

I don't know of any gem that would be able to stub tcp connections.

WebMock is only for stubbing http requests and it's not planned to add raw tcp support.

The problem that WebMock solves, it that it allows writing tests without thinking on how the underlying api works.
No matter what http client you use, you have unified interface for stubbing and expecting requests.

When stubbing raw tcp requests, you'd need to know what kind of raw data is being sent. There would be not much difference between having an api that does the stubbing or just stubbing calls to TCPSocket directly. 
There is also not too many options for making TCP requests in Ruby to make it worth having unified stubbing api.
I think that's the reason there is no WebMock for tcp requests yet.

Bartosz








--

---
You received this message because you are subscribed to the Google Groups "webmock-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webmock-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages