Skip to content

Commit

Permalink
Merge pull request #285 from hufman/use_tempfile
Browse files Browse the repository at this point in the history
Use a tempfile instead of a StringIO object
  • Loading branch information
gabrielfalcao committed May 26, 2016
2 parents 3f528b8 + 3b09b51 commit f93c32a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
21 changes: 17 additions & 4 deletions httpretty/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import json
import contextlib
import threading
import tempfile


from .compat import (
Expand Down Expand Up @@ -236,10 +237,17 @@ class HTTPrettyRequestEmpty(object):
headers = EmptyRequestHeaders()


class FakeSockFile(StringIO):
class FakeSockFile(object):
def __init__(self):
self.file = tempfile.TemporaryFile()
self._fileno = self.file.fileno()
def close(self):
self.socket.close()
StringIO.close(self)
self.file.close()
def fileno(self):
return self._fileno
def __getattr__(self, name):
return getattr(self.file, name)


class FakeSSLSocket(object):
Expand Down Expand Up @@ -322,14 +330,19 @@ def connect(self, address):
else:
raise UnmockedError()

def fileno(self):
if self.truesock:
return self.truesock.fileno()
return self.fd.fileno()

def close(self):
if not (self.is_http and self._closed):
if self.truesock:
self.truesock.close()
self._closed = True

def makefile(self, mode='r', bufsize=-1):
"""Returns this fake socket's own StringIO buffer.
"""Returns this fake socket's own tempfile buffer.
If there is an entry associated with the socket, the file
descriptor gets filled in with the entry data before being
Expand Down Expand Up @@ -358,7 +371,7 @@ def real_sendall(self, data, *args, **kw):
when HTTPretty identifies that someone is trying to send
non-http data.
The received bytes are written in this socket's StringIO
The received bytes are written in this socket's tempfile
buffer so that HTTPretty can return it accordingly when
necessary.
"""
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ def test_fakesock_socket_real_sendall(old_socket):
real_socket.recv.called.should.be.false

# And the buffer is empty
socket.fd.getvalue().should.equal(b'')
socket.fd.read().should.equal(b'')

# And connect was never called
real_socket.connect.called.should.be.false
Expand Down Expand Up @@ -356,7 +356,7 @@ def test_fakesock_socket_real_sendall_when_http(old_socket):
])

# And the buffer should contain the data from the server
socket.fd.getvalue().should.equal(b"response from server")
socket.fd.read().should.equal(b"response from server")

# And connect was called
real_socket.connect.called.should.be.true
Expand Down Expand Up @@ -391,7 +391,7 @@ def test_fakesock_socket_real_sendall_continue_eagain_when_http(socket, old_sock
])

# And the buffer should contain the data from the server
socket.fd.getvalue().should.equal(b"after error")
socket.fd.read().should.equal(b"after error")

# And connect was called
real_socket.connect.called.should.be.true
Expand Down Expand Up @@ -424,7 +424,7 @@ def test_fakesock_socket_real_sendall_socket_error_when_http(socket, old_socket)
real_socket.recv.assert_called_once_with(socket._bufsize)

# And the buffer should contain the data from the server
socket.fd.getvalue().should.equal(b"")
socket.fd.read().should.equal(b"")

# And connect was called
real_socket.connect.called.should.be.true
Expand Down Expand Up @@ -464,7 +464,7 @@ def test_fakesock_socket_real_sendall_when_http(POTENTIAL_HTTP_PORTS, old_socket
])

# And the buffer should contain the data from the server
socket.fd.getvalue().should.equal(b"response from foobar :)")
socket.fd.read().should.equal(b"response from foobar :)")


@patch('httpretty.core.old_socket')
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_httpretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def test_Entry_class_counts_multibyte_characters_in_bytes():
entry = Entry(HTTPretty.GET, 'http://example.com', 'こんにちは')
buf = FakeSockFile()
entry.fill_filekind(buf)
response = buf.getvalue()
response = buf.read()
expect(b'content-length: 15\n').to.be.within(response)


Expand Down

0 comments on commit f93c32a

Please sign in to comment.