mgr/dashboard_v2: Very simple ping example

Signed-off-by: Ricardo Dias <rdias@suse.com>
This commit is contained in:
Ricardo Dias 2018-01-25 11:44:01 +00:00
parent 1333d3d63e
commit ec57819386
No known key found for this signature in database
GPG Key ID: 74390C579BD37B68
2 changed files with 64 additions and 4 deletions

View File

@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import cherrypy
from ..restresource import RESTResource
from ..tools import ApiController, AuthRequired
@ApiController('ping')
@AuthRequired()
class Ping(object):
@cherrypy.expose
def default(self, *args):
return "pong"
@ApiController('echo1')
class EchoArgs(RESTResource):
@RESTResource.args_from_json
def create(self, msg):
return {'echo': msg}
@ApiController('echo2')
class Echo(RESTResource):
def create(self, data):
return {'echo': data['msg']}

View File

@ -2,18 +2,50 @@
from __future__ import absolute_import
import json
from cherrypy.test import helper
from ..controllers.auth import Auth
from ..module import Module, cherrypy
from ..tools import load_controller
class SimpleCPTest(helper.CPWebCase):
@staticmethod
def setup_server():
cherrypy.tools.autenticate = cherrypy.Tool('before_handler',
Auth.check_auth)
module = Module('attic', None, None)
cherrypy.tree.mount(Module.HelloWorld(module), "/api/hello")
Ping = load_controller(module, 'Ping')
Echo = load_controller(module, 'Echo')
EchoArgs = load_controller(module, 'EchoArgs')
cherrypy.tree.mount(Ping(), "/api/ping")
cherrypy.tree.mount(Echo(), "/api/echo2")
cherrypy.tree.mount(EchoArgs(), "/api/echo1")
def _request(self, url, method, data=None):
if not data:
b = None
h = None
else:
b = json.dumps(data)
h = [('Content-Type', 'application/json'),
('Content-Length', str(len(b)))]
self.getPage(url, method=method, body=b, headers=h)
def _post(self, url, data=None):
self._request(url, 'POST', data)
def test_ping(self):
self.getPage("/api/hello/ping")
self.assertStatus('200 OK')
self.assertBody('"pong"')
self.getPage("/api/ping")
self.assertStatus('401 Unauthorized')
def test_echo(self):
self._post("/api/echo2", {'msg': 'Hello World'})
self.assertStatus('201 Created')
def test_echo_args(self):
self._post("/api/echo1", {'msg': 'Hello World'})
self.assertStatus('201 Created')