Add .spec methods for Request and Response objects.

This commit is contained in:
Aldo Cortesi 2012-10-29 17:33:10 +13:00
parent 747eafd107
commit 3e0cd6442a
2 changed files with 26 additions and 7 deletions

View File

@ -242,7 +242,7 @@ class ValueGenerate(_Value):
class ValueFile(_Value):
def __init__(self, path):
self.path = path
self.path = str(path)
@classmethod
def expr(klass):
@ -807,6 +807,9 @@ class _Response(_Message):
)
return resp
def spec(self):
return ":".join([i.spec() for i in self.tokens])
class _Request(_Message):
comps = (
@ -849,26 +852,27 @@ class _Request(_Message):
)
return resp
def spec(self):
return ":".join([i.spec() for i in self.tokens])
class CraftedRequest(_Request):
def __init__(self, spec, tokens):
_Request.__init__(self, tokens)
self.spec, self.tokens = spec, tokens
def serve(self, fp, settings, host):
d = _Request.serve(self, fp, settings, host)
d["spec"] = self.spec
d["spec"] = self.spec()
return d
class CraftedResponse(_Response):
def __init__(self, spec, tokens):
_Response.__init__(self, tokens)
self.spec, self.tokens = spec, tokens
def serve(self, fp, settings):
d = _Response.serve(self, fp, settings, None)
d["spec"] = self.spec
d["spec"] = self.spec()
return d
@ -910,7 +914,7 @@ def parse_response(settings, s):
May raise ParseException or FileAccessDenied
"""
try:
s.decode("ascii")
s = s.decode("ascii")
except UnicodeError:
raise ParseException("Spec must be valid ASCII.", 0, 0)
if s.startswith(FILESTART):
@ -926,7 +930,7 @@ def parse_request(settings, s):
May raise ParseException or FileAccessDenied
"""
try:
s.decode("ascii")
s = s.decode("ascii")
except UnicodeError:
raise ParseException("Spec must be valid ASCII.", 0, 0)
if s.startswith(FILESTART):

View File

@ -406,6 +406,13 @@ class TestParseRequest:
assert r.path.string().endswith("bar")
assert r.actions
def test_spec(self):
def rt(s):
s = language.parse_request({}, s).spec()
assert language.parse_request({}, s).spec() == s
rt("get:/foo")
rt("get:/foo:da")
class TestParseResponse:
def test_parse_err(self):
@ -439,6 +446,14 @@ class TestParseResponse:
r = language.parse_response({}, "400:b@100g")
assert r.length({}, None)
def test_spec(self):
def rt(s):
s = language.parse_response({}, s).spec()
assert language.parse_response({}, s).spec() == s
rt("400:b@100g")
rt("400")
rt("400:da")
class TestWriteValues:
def test_send_chunk(self):