Skip to content

Commit

Permalink
Adds echo directive, alt for response body
Browse files Browse the repository at this point in the history
  • Loading branch information
jar-o authored and traptoy committed Jun 4, 2018
1 parent 57b40c1 commit c16da92
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -147,7 +147,7 @@ The _Response_ section designates what will be returned if the _Request_ section
< delay <milliseconds>
< status <number>
< header <line>
< body <data>
< body <data> | echo
.
```

Expand All @@ -166,9 +166,25 @@ The _body_ directive can be a single line or multiple lines, and can contain any
"hm": "简体中文测试"
}
<<<
```

Note that you can also substitute _body_ for _echo_, in which case the _request
body_ the user supplied will be echoed as the _response body_, as long as
request values match. For example the following will simply echo whatever JSON
the user posts, with a `200 OK` status.

```
printf "
> POST
> /test
> header Content-Type: application/json
# response
< header Content-Type: application/json
< echo
.
" | python dumdum.py
```

### Example

Expand Down
2 changes: 2 additions & 0 deletions README.rst
Expand Up @@ -64,3 +64,5 @@ WSGI compliant library, so you can easily serve it from your own code like
""")
srv = make_server('', 5000, dum.server)
srv.serve_forever()

Source and further details can be found at https://github.com/jar-o/dumdum
26 changes: 20 additions & 6 deletions dumdum.py
Expand Up @@ -127,12 +127,16 @@ def status_map(code):
body_resp = response_start + Keyword('body') + \
(Word(' ' + printables + unicodePrintables) ^ \
QuotedString(quoteChar='<<<', escQuote='<<<<<<', multiline=True))
# This echos the request body, used as an alt to '< body ...' above
echo_resp = response_start + Keyword('echo')

# Defines the stanza list
stanza_end = LineStart() + '.' + LineEnd()

# The parser
class DumdumParser(object):
class EchoFlag(object):
pass
def __init__(self, user_input):
self.S = {}
self.stanza = Group(
Expand All @@ -148,8 +152,10 @@ def __init__(self, user_input):
Optional(Suppress(comment))
) + \
ZeroOrMore(
Group(param_S.setParseAction(self.process_param) ^ \
param_R.setParseAction(self.process_param)) + \
Group(
param_S.setParseAction(self.process_param) ^ \
param_R.setParseAction(self.process_param)
) + \
Optional(Suppress(comment))
) + \
ZeroOrMore(
Expand All @@ -172,9 +178,10 @@ def __init__(self, user_input):
) + \
ZeroOrMore(
Group(
body_resp.setParseAction(self.process_resp_body) + \
Optional(Suppress(comment))
)
body_resp.setParseAction(self.process_resp_body) ^ \
echo_resp.setParseAction(self.process_resp_echo)
) + \
Optional(Suppress(comment))
) + \
Suppress(stanza_end.setParseAction(self.save_stanza))

Expand Down Expand Up @@ -248,6 +255,9 @@ def process_resp_body(self, tokens):
b = tokens[2:][0]
self.respobj['body'] = b.strip() # TODO is this wise?

def process_resp_echo(self, tokens):
self.respobj['body'] = DumdumParser.EchoFlag()

def save_stanza(self, tokens):
if self.reqobj:
for verb in self.current_verbs:
Expand Down Expand Up @@ -413,7 +423,11 @@ def flatten(x, name=''):
hdrs.append( (str(h),
str(resp['headers'][h])) )
start_response(status, hdrs)
return [resp['body'].encode('utf-8')]
print type(resp['body'])
if type(resp['body']) is DumdumParser.EchoFlag:
return request_body
else:
return [resp['body'].encode('utf-8')]
# We matched nothing
return bad_req()

Expand Down

0 comments on commit c16da92

Please sign in to comment.