diff --git a/tests/unit/test_httpretty.py b/tests/unit/test_httpretty.py index 63ba565..b0d46c1 100644 --- a/tests/unit/test_httpretty.py +++ b/tests/unit/test_httpretty.py @@ -26,7 +26,7 @@ # OTHER DEALINGS IN THE SOFTWARE. from __future__ import unicode_literals from sure import expect -from httpretty import HTTPretty, HTTPrettyError +from httpretty import HTTPretty, HTTPrettyError, core from httpretty.core import URIInfo, BaseClass, Entry, FakeSockFile from httpretty.http import STATUSES @@ -53,6 +53,47 @@ def test_httpretty_should_raise_proper_exception_on_inconsistent_length(): ) +def test_httpretty_should_raise_on_socket_send_when_uri_registered(): + """HTTPretty should raise a RuntimeError when the fakesocket is used in + an invalid usage. + """ + import socket + HTTPretty.enable() + + defaults = core.POTENTIAL_HTTP_PORTS[:] + HTTPretty.register_uri(HTTPretty.GET, + 'http://127.0.0.1:5000') + expect(core.POTENTIAL_HTTP_PORTS).to.be.equal([80, 443, 5000]) + + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect(('127.0.0.1', 5000)) + expect(sock.send).when.called_with('whatever').to.throw(RuntimeError) + sock.close() + + # restore the previous value + core.POTENTIAL_HTTP_PORTS = defaults + HTTPretty.reset() + HTTPretty.disable() + + +def test_httpretty_should_not_raise_on_socket_send_when_uri_not_registered(): + """HTTPretty should not raise a RuntimeError when the fakesocket is used in + an invalid usage. + """ + import socket + HTTPretty.enable() + + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0) + sock.setblocking(0) + expect(sock.sendto).when.called_with('whatever', + ('127.0.0.1', 53) + ).should_not.throw(RuntimeError) + + sock.close() + HTTPretty.reset() + HTTPretty.disable() + + def test_does_not_have_last_request_by_default(): 'HTTPretty.last_request is a dummy object by default' HTTPretty.reset()