Skip to content

Commit

Permalink
raising exception when the developer register a content-length bigger…
Browse files Browse the repository at this point in the history
… than actual body size. closes #1
  • Loading branch information
gabrielfalcao committed Feb 8, 2011
1 parent 249374b commit a796971
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions httpretty/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# OTHER DEALINGS IN THE SOFTWARE.
import re
import socket
import warnings

from datetime import datetime
from StringIO import StringIO
Expand All @@ -43,6 +44,9 @@
except ImportError:
socks = None

class HTTPrettyError(Exception):
pass

class FakeSockFile(StringIO):
def read(self, amount=None):
amount = amount or self.len
Expand Down Expand Up @@ -194,6 +198,7 @@ def __init__(self, method, uri, body, adding_headers=None, forcing_headers=None,
self.method = method
self.uri = uri
self.body = body
self.body_length = len(body or '')
self.adding_headers = adding_headers or {}
self.forcing_headers = forcing_headers or {}
self.status = int(status)
Expand All @@ -202,6 +207,30 @@ def __init__(self, method, uri, body, adding_headers=None, forcing_headers=None,
name = "-".join(k.split("_")).capitalize()
self.adding_headers[name] = v

self.validate()

def validate(self):
content_length_keys = 'Content-Length', 'content-cength'
for key in content_length_keys:
got = self.adding_headers.get(key, self.forcing_headers.get(key, None))
if got is None:
continue

try:
igot = int(got)
except ValueError:
warnings.warn('HTTPretty got to register the Content-Length header with "%r" which is not a number' % got)

if igot > self.body_length:
raise HTTPrettyError(
'HTTPretty got inconsistent parameters. The header Content-Length you registered expects size "%d" '
'but the body you registered for that has actually length "%d".\n'
'Fix that, or if you really want that, call register_uri with "fill_with" callback.' % (
igot, self.body_length
)
)


def __repr__(self):
return r'<Entry %s %s getting %d>' % (self.method, self.uri, self.status)

Expand Down

0 comments on commit a796971

Please sign in to comment.