mirror of
https://github.com/ceph/ceph
synced 2025-01-01 00:22:25 +00:00
mgr/dashboard_v2: Very simple ping example
Signed-off-by: Ricardo Dias <rdias@suse.com>
This commit is contained in:
parent
1333d3d63e
commit
ec57819386
28
src/pybind/mgr/dashboard_v2/controllers/ping.py
Normal file
28
src/pybind/mgr/dashboard_v2/controllers/ping.py
Normal 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']}
|
@ -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')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user