diff options
| author | Gabriel Falcao <gabriel@nacaolivre.org> | 2013-02-14 12:32:44 -0500 |
|---|---|---|
| committer | Gabriel Falcao <gabriel@nacaolivre.org> | 2013-02-14 12:32:44 -0500 |
| commit | 2eb529d8d9fe237a946b8ecdf769555324fdd6ff (patch) | |
| tree | 7dd895fe186ea9ebd084a5f697b81f41831a6ca2 | |
| parent | 50ec5d6d1a4148a1eb7f8d50ef06b81f6e377564 (diff) | |
| download | httpretty-2eb529d8d9fe237a946b8ecdf769555324fdd6ff.tar.gz | |
raising a RuntimeError when unexpected socket methods are called,
that will leverage openning issues more easily. refs #29
| -rw-r--r-- | httpretty/__init__.py | 21 | ||||
| -rw-r--r-- | tests/functional/test_debug.py | 104 |
2 files changed, 120 insertions, 5 deletions
diff --git a/httpretty/__init__.py b/httpretty/__init__.py index 45da83c..dfb0c3c 100644 --- a/httpretty/__init__.py +++ b/httpretty/__init__.py @@ -23,9 +23,10 @@ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR # OTHER DEALINGS IN THE SOFTWARE. -version = '0.5.6' +version = '0.5.7' import re +import inspect import socket import functools import itertools @@ -38,7 +39,7 @@ import types PY3 = sys.version_info[0] == 3 if PY3: text_type = str - binary_type = bytes + byte_type = bytes import io StringIO = io.StringIO @@ -47,7 +48,7 @@ if PY3: return self.__str__() else: text_type = unicode - binary_type = str + byte_type = str import StringIO StringIO = StringIO.StringIO @@ -101,7 +102,7 @@ def utf8(s): if isinstance(s, text_type): s = s.encode('utf-8') - return binary_type(s) + return byte_type(s) def parse_requestline(s): @@ -306,7 +307,17 @@ class fakesock(object): self._entry = entry def debug(*a, **kw): - import debug + frame = inspect.stack()[0][0] + lines = map(utf8, traceback.format_stack(frame)) + + message = [ + "HTTPretty intercepted and unexpected socket method call.", + ("Please open an issue at " + "'https://github.com/gabrielfalcao/HTTPretty/issues'"), + "And paste the following traceback:\n", + "".join(lines), + ] + raise RuntimeError("\n".join(message)) def settimeout(self, new_timeout): self.timeout = new_timeout diff --git a/tests/functional/test_debug.py b/tests/functional/test_debug.py new file mode 100644 index 0000000..edf2ad8 --- /dev/null +++ b/tests/functional/test_debug.py @@ -0,0 +1,104 @@ +# #!/usr/bin/env python +# -*- coding: utf-8 -*- + +# <HTTPretty - HTTP client mock for Python> +# Copyright (C) <2011-2012> Gabriel Falcão <gabriel@nacaolivre.org> +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation +# files (the "Software"), to deal in the Software without +# restriction, including without limitation the rights to use, +# copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the +# Software is furnished to do so, subject to the following +# conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +# OTHER DEALINGS IN THE SOFTWARE. +from __future__ import unicode_literals +import socket +from sure import scenario, expect +from httpretty import httprettified + + +def create_socket(context): + context.sock = socket.socket( + socket.AF_INET, + socket.SOCK_STREAM, + socket.IPPROTO_TCP, + ) + + +@httprettified +@scenario(create_socket) +def test_httpretty_debugs_socket_send(context): + u"HTTPretty should debug socket.send" + + expect(context.sock.send).when.called.to.throw( + RuntimeError, + "HTTPretty intercepted and unexpected socket method call." + ) + + +@httprettified +@scenario(create_socket) +def test_httpretty_debugs_socket_sendto(context): + u"HTTPretty should debug socket.sendto" + + expect(context.sock.sendto).when.called.to.throw( + RuntimeError, + "HTTPretty intercepted and unexpected socket method call." + ) + + +@httprettified +@scenario(create_socket) +def test_httpretty_debugs_socket_recv(context): + u"HTTPretty should debug socket.recv" + + expect(context.sock.recv).when.called.to.throw( + RuntimeError, + "HTTPretty intercepted and unexpected socket method call." + ) + + +@httprettified +@scenario(create_socket) +def test_httpretty_debugs_socket_recvfrom(context): + u"HTTPretty should debug socket.recvfrom" + + expect(context.sock.recvfrom).when.called.to.throw( + RuntimeError, + "HTTPretty intercepted and unexpected socket method call." + ) + + +@httprettified +@scenario(create_socket) +def test_httpretty_debugs_socket_recv_into(context): + u"HTTPretty should debug socket.recv_into" + + expect(context.sock.recv_into).when.called.to.throw( + RuntimeError, + "HTTPretty intercepted and unexpected socket method call." + ) + + +@httprettified +@scenario(create_socket) +def test_httpretty_debugs_socket_recvfrom_into(context): + u"HTTPretty should debug socket.recvfrom_into" + + expect(context.sock.recvfrom_into).when.called.to.throw( + RuntimeError, + "HTTPretty intercepted and unexpected socket method call." + ) |
