Skip to content

Commit

Permalink
Enable multi-method URL declarations (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffjukes authored and mattbennett committed Jan 19, 2018
1 parent a0c61ad commit b33a361
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/built_in_extensions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ HTTP

The HTTP entrypoint is built on top of `werkzeug <http://werkzeug.pocoo.org/>`_, and supports all the standard HTTP methods (GET/POST/DELETE/PUT etc)

The HTTP entrypoint can specify multiple HTTP methods for a single URL as a comma-separated list. See example below.

Service methods must return one of:

- a string, which becomes the response body
Expand Down
4 changes: 4 additions & 0 deletions docs/examples/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ def get_method(self, request, value):
@http('POST', '/post')
def do_post(self, request):
return u"received: {}".format(request.get_data(as_text=True))

@http('GET,PUT,POST,DELETE', '/multi')
def do_multi(self, request):
return request.method
16 changes: 16 additions & 0 deletions docs/examples/test/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ def test_http(self, container_factory, web_config, web_session):
assert res.status_code == 200
assert res.text == u'received: 你好'

res = web_session.get("/multi")
assert res.status_code == 200
assert res.text == 'GET'

res = web_session.put("/multi")
assert res.status_code == 200
assert res.text == 'PUT'

res = web_session.post("/multi")
assert res.status_code == 200
assert res.text == 'POST'

res = web_session.delete("/multi")
assert res.status_code == 200
assert res.text == 'DELETE'

def test_advanced(self, container_factory, web_config, web_session):

from examples.advanced_http import Service
Expand Down
2 changes: 1 addition & 1 deletion nameko/web/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(self, method, url, **kwargs):
super(HttpRequestHandler, self).__init__(**kwargs)

def get_url_rule(self):
return Rule(self.url, methods=[self.method])
return Rule(self.url, methods=self.method.split(','))

def setup(self):
self.server.register_provider(self)
Expand Down
20 changes: 19 additions & 1 deletion test/web/test_http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ def do_get(self, request, bar):
def do_post(self, request):
data = json.loads(request.get_data(as_text=True))
value = data['value']

return value

@http('GET,PUT,POST', '/multi')
def do_multi(self, request):
return request.method

@http('GET', '/custom')
def do_custom(self, request):
return Response('response')
Expand Down Expand Up @@ -73,6 +76,21 @@ def test_post(web_session):
assert rv.text == "foo"


def test_multi_get(web_session):
rv = web_session.get('/multi')
assert rv.text == "GET"


def test_multi_put(web_session):
rv = web_session.put('/multi')
assert rv.text == "PUT"


def test_multi_post(web_session):
rv = web_session.post('/multi')
assert rv.text == "POST"


def test_custom_response(web_session):
rv = web_session.get('/custom')
assert rv.text == 'response'
Expand Down

0 comments on commit b33a361

Please sign in to comment.