From 3e0cd6442aa3dd5ecc08af4851e68545121737ab Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Mon, 29 Oct 2012 17:33:10 +1300 Subject: [PATCH] Add .spec methods for Request and Response objects. --- libpathod/language.py | 18 +++++++++++------- test/test_language.py | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/libpathod/language.py b/libpathod/language.py index 2dceefa50..1c010c917 100644 --- a/libpathod/language.py +++ b/libpathod/language.py @@ -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): diff --git a/test/test_language.py b/test/test_language.py index c453766cb..219b2909d 100644 --- a/test/test_language.py +++ b/test/test_language.py @@ -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):