base.PathodSpec -> http.PathodResponse

This commit is contained in:
Aldo Cortesi 2015-05-03 08:56:19 +12:00
parent a46e17459d
commit bf71a9a2a0
4 changed files with 63 additions and 62 deletions

View File

@ -259,39 +259,6 @@ class KeyValue(_Component):
)
class PathodSpec(Token):
def __init__(self, value):
self.value = value
try:
import http
self.parsed = http.Response(
http.Response.expr().parseString(
value.val,
parseAll=True
)
)
except pp.ParseException, v:
raise exceptions.ParseException(v.msg, v.line, v.col)
@classmethod
def expr(klass):
e = pp.Literal("s").suppress()
e = e + ValueLiteral.expr()
return e.setParseAction(lambda x: klass(*x))
def values(self, settings):
return [
self.value.get_generator(settings),
]
def spec(self):
return "s%s"%(self.value.spec())
def freeze(self, settings):
f = self.parsed.freeze(settings).spec()
return PathodSpec(ValueLiteral(f.encode("string_escape")))
class CaselessLiteral(_Component):
"""
A caseless token that can take only one value.

View File

@ -88,6 +88,38 @@ class ShortcutUserAgent(_HeaderMixin, base.OptionsOrValue):
)
class PathodResponse(base.Token):
def __init__(self, value):
self.value = value
try:
self.parsed = Response(
Response.expr().parseString(
value.val,
parseAll=True
)
)
except pp.ParseException, v:
raise exceptions.ParseException(v.msg, v.line, v.col)
@classmethod
def expr(klass):
e = pp.Literal("s").suppress()
e = e + base.ValueLiteral.expr()
return e.setParseAction(lambda x: klass(*x))
def values(self, settings):
return [
self.value.get_generator(settings),
]
def spec(self):
return "s%s"%(self.value.spec())
def freeze(self, settings):
f = self.parsed.freeze(settings).spec()
return PathodResponse(base.ValueLiteral(f.encode("string_escape")))
def get_header(val, headers):
"""
Header keys may be Values, so we have to "generate" them as we try the
@ -248,7 +280,7 @@ class Request(_HTTPMessage):
ShortcutContentType,
ShortcutUserAgent,
Raw,
base.PathodSpec,
PathodResponse,
actions.PauseAt,
actions.DisconnectAt,
@ -270,7 +302,7 @@ class Request(_HTTPMessage):
@property
def pathodspec(self):
return self.tok(base.PathodSpec)
return self.tok(PathodResponse)
def preamble(self, settings):
v = self.method.values(settings)

View File

@ -182,32 +182,6 @@ class TestMisc:
s = v.spec()
assert s == v.expr().parseString(s)[0].spec()
def test_pathodspec(self):
e = base.PathodSpec.expr()
v = e.parseString("s'200'")[0]
assert v.value.val == "200"
tutils.raises(
language.ParseException,
e.parseString,
"s'foo'"
)
v = e.parseString('s"200:b@1"')[0]
assert "@1" in v.spec()
f = v.freeze({})
assert "@1" not in f.spec()
def test_pathodspec_freeze(self):
e = base.PathodSpec(
base.ValueLiteral(
"200:b'foo':i10,'\\''".encode(
"string_escape"
)
)
)
assert e.freeze({})
assert e.values({})
def test_prevalue(self):
class TT(base.PreValue):
preamble = "m"

View File

@ -77,7 +77,7 @@ class TestRequest:
r = language.parse_requests(l)
assert len(r) == 1
assert len(r[0].tokens) == 3
assert isinstance(r[0].tokens[2], base.PathodSpec)
assert isinstance(r[0].tokens[2], http.PathodResponse)
assert r[0].values({})
def test_render(self):
@ -320,3 +320,31 @@ def test_user_agent():
v2 = v.freeze({})
v3 = v2.freeze({})
assert v2.value.val == v3.value.val
def test_pathodspec():
e = http.PathodResponse.expr()
v = e.parseString("s'200'")[0]
assert v.value.val == "200"
tutils.raises(
language.ParseException,
e.parseString,
"s'foo'"
)
v = e.parseString('s"200:b@1"')[0]
assert "@1" in v.spec()
f = v.freeze({})
assert "@1" not in f.spec()
def test_pathodspec_freeze():
e = http.PathodResponse(
base.ValueLiteral(
"200:b'foo':i10,'\\''".encode(
"string_escape"
)
)
)
assert e.freeze({})
assert e.values({})