diff options
| author | Aldo Cortesi <aldo@nullcube.com> | 2012-10-29 17:33:10 +1300 |
|---|---|---|
| committer | Aldo Cortesi <aldo@nullcube.com> | 2012-10-29 17:33:10 +1300 |
| commit | 3e0cd6442aa3dd5ecc08af4851e68545121737ab (patch) | |
| tree | 0333c96ae35d6d869ced4a7719174dee4aa5dd76 | |
| parent | 747eafd10703d5a93003e410469771f883df68b5 (diff) | |
| download | mitmproxy-3e0cd6442aa3dd5ecc08af4851e68545121737ab.tar.gz mitmproxy-3e0cd6442aa3dd5ecc08af4851e68545121737ab.tar.bz2 mitmproxy-3e0cd6442aa3dd5ecc08af4851e68545121737ab.zip | |
Add .spec methods for Request and Response objects.
| -rw-r--r-- | libpathod/language.py | 18 | ||||
| -rw-r--r-- | test/test_language.py | 15 |
2 files changed, 26 insertions, 7 deletions
diff --git a/libpathod/language.py b/libpathod/language.py index 2dceefa5..1c010c91 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 c453766c..219b2909 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): |
