1
|
|
|
"""APIServer tests.""" |
2
|
|
|
|
3
|
|
|
import unittest |
4
|
|
|
|
5
|
|
|
from kytos.core.api_server import APIServer |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
class TestAPIServer(unittest.TestCase): |
9
|
|
|
"""Test the class APIServer.""" |
10
|
|
|
|
11
|
|
|
def setUp(self): |
12
|
|
|
"""Instantiate a APIServer.""" |
13
|
|
|
self.api_server = APIServer('CustomName', False) |
14
|
|
|
|
15
|
|
|
def test_register_rest_endpoint(self): |
16
|
|
|
"""Test whether register_rest_endpoint is registering an endpoint.""" |
17
|
|
|
self.api_server.register_rest_endpoint('/custom_method/', |
18
|
|
|
self.__custom_endpoint, |
19
|
|
|
methods=['GET']) |
20
|
|
|
|
21
|
|
|
expected_endpoint = '/kytos/custom_method/' |
22
|
|
|
actual_endpoints = self.api_server.rest_endpoints |
23
|
|
|
self.assertIn(expected_endpoint, actual_endpoints) |
24
|
|
|
|
25
|
|
|
def test_register_api_server_routes(self): |
26
|
|
|
"""Server routes should include status and shutdown endpoints.""" |
27
|
|
|
self.api_server.register_api_server_routes() |
28
|
|
|
|
29
|
|
|
expecteds = ['/kytos/status/', '/kytos/shutdown/', |
30
|
|
|
'/static/<path:filename>'] |
31
|
|
|
|
32
|
|
|
actual_endpoints = self.api_server.rest_endpoints |
33
|
|
|
|
34
|
|
|
self.assertListEqual(sorted(expecteds), sorted(actual_endpoints)) |
35
|
|
|
|
36
|
|
|
def test_rest_endpoints(self): |
37
|
|
|
"""Test whether rest_endpoint returns all registered endpoints.""" |
38
|
|
|
endpoints = ['/custom/', '/custom_2/', '/custom_3/'] |
39
|
|
|
|
40
|
|
|
for endpoint in endpoints: |
41
|
|
|
self.api_server.register_rest_endpoint(endpoint, |
42
|
|
|
self.__custom_endpoint, |
43
|
|
|
methods=['GET']) |
44
|
|
|
|
45
|
|
|
expected_endpoints = ['/kytos{}'.format(e) for e in endpoints] |
46
|
|
|
expected_endpoints.append('/static/<path:filename>') |
47
|
|
|
|
48
|
|
|
actual_endpoints = self.api_server.rest_endpoints |
49
|
|
|
self.assertListEqual(sorted(expected_endpoints), |
50
|
|
|
sorted(actual_endpoints)) |
51
|
|
|
|
52
|
|
|
@staticmethod |
53
|
|
|
def __custom_endpoint(): |
54
|
|
|
"""Custom method used by APIServer.""" |
55
|
|
|
return "Custom Endpoint" |
56
|
|
|
|