blob: 0a552e42aa6642c74fb7f16646edc959cbddf9ef [file] [log] [blame]
Zack Williams41513bf2018-07-07 20:08:35 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
Zsolt Haraszti66862032016-11-28 14:28:39 -080014from unittest import TestCase
15from requests import get, post, put, patch, delete
ubuntuc5c83d72017-07-01 17:57:19 -070016import urllib3
17
18# This is to suppress the insecure request warning when using
19# self signed ssl certificates
20urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
Zsolt Haraszti66862032016-11-28 14:28:39 -080021
Stephane Barbariecd51f992017-09-07 16:37:02 -040022# For a list of valid GRPC status codes, refer to the following link
23# https://github.com/grpc/grpc-go/blob/master/codes/codes.go
Zsolt Haraszti66862032016-11-28 14:28:39 -080024
25class RestBase(TestCase):
26
ubuntuc5c83d72017-07-01 17:57:19 -070027 base_url = 'https://localhost:8881'
Zsolt Haraszti66862032016-11-28 14:28:39 -080028
29 def url(self, path):
30 while path.startswith('/'):
31 path = path[1:]
32 return self.base_url + '/' + path
33
34 def verify_content_type_and_return(self, response, expected_content_type):
Stephane Barbariecd51f992017-09-07 16:37:02 -040035 if 'grpc-status' in response.headers and int(response.headers['grpc-status']) != 0:
36 return None
37
Zsolt Haraszti66862032016-11-28 14:28:39 -080038 if 200 <= response.status_code < 300:
39 self.assertEqual(
40 response.headers['Content-Type'],
41 expected_content_type,
42 msg='Content-Type %s != %s; msg:%s' % (
43 response.headers['Content-Type'],
44 expected_content_type,
45 response.content))
46 if expected_content_type == 'application/json':
47 return response.json()
48 else:
49 return response.content
50
Stephane Barbariecd51f992017-09-07 16:37:02 -040051 def get(self, path, expected_http_code=200, grpc_status=0,
ubuntuc5c83d72017-07-01 17:57:19 -070052 expected_content_type='application/json', headers=None, verify=False):
53 r = get(self.url(path), headers=headers, verify=verify)
Stephane Barbariecd51f992017-09-07 16:37:02 -040054 if 'grpc-status' in r.headers:
55 self.assertEqual(int(r.headers['grpc-status']), grpc_status,
56 msg='GRPC Status %d!=%d; msg:%s' % (
57 int(r.headers['grpc-status']),
58 grpc_status,
59 r.headers['grpc-message']))
60 self.assertEqual(r.status_code, expected_http_code,
Zsolt Haraszti66862032016-11-28 14:28:39 -080061 msg='Code %d!=%d; msg:%s' % (
Stephane Barbariecd51f992017-09-07 16:37:02 -040062 r.status_code, expected_http_code, r.content))
Zsolt Haraszti66862032016-11-28 14:28:39 -080063 return self.verify_content_type_and_return(r, expected_content_type)
64
Stephane Barbariecd51f992017-09-07 16:37:02 -040065 def post(self, path, json_dict=None, expected_http_code=201, grpc_status=0, verify=False):
ubuntuc5c83d72017-07-01 17:57:19 -070066 r = post(self.url(path), json=json_dict, verify=verify)
Stephane Barbariecd51f992017-09-07 16:37:02 -040067 if 'grpc-status' in r.headers:
68 self.assertEqual(int(r.headers['grpc-status']), grpc_status,
69 msg='GRPC Status %d!=%d; msg:%s' % (
70 int(r.headers['grpc-status']),
71 grpc_status,
72 r.headers['grpc-message']))
73 self.assertEqual(r.status_code, expected_http_code,
Zsolt Haraszti66862032016-11-28 14:28:39 -080074 msg='Code %d!=%d; msg:%s' % (
Stephane Barbariecd51f992017-09-07 16:37:02 -040075 r.status_code, expected_http_code, r.content))
Zsolt Haraszti66862032016-11-28 14:28:39 -080076 return self.verify_content_type_and_return(r, 'application/json')
77
Stephane Barbariecd51f992017-09-07 16:37:02 -040078 def put(self, path, json_dict, expected_http_code=200, grpc_status=0, verify=False):
ubuntuc5c83d72017-07-01 17:57:19 -070079 r = put(self.url(path), json=json_dict, verify=verify)
Stephane Barbariecd51f992017-09-07 16:37:02 -040080 if 'grpc-status' in r.headers:
81 self.assertEqual(int(r.headers['grpc-status']), grpc_status,
82 msg='GRPC Status %d!=%d; msg:%s' % (
83 int(r.headers['grpc-status']),
84 grpc_status,
85 r.headers['grpc-message']))
86 self.assertEqual(r.status_code, expected_http_code,
Zsolt Haraszti66862032016-11-28 14:28:39 -080087 msg='Code %d!=%d; msg:%s' % (
Stephane Barbariecd51f992017-09-07 16:37:02 -040088 r.status_code, expected_http_code, r.content))
Zsolt Haraszti66862032016-11-28 14:28:39 -080089 return self.verify_content_type_and_return(r, 'application/json')
90
Stephane Barbariecd51f992017-09-07 16:37:02 -040091 def delete(self, path, expected_http_code=209, grpc_status=0, verify=False):
ubuntuc5c83d72017-07-01 17:57:19 -070092 r = delete(self.url(path), verify=verify)
Stephane Barbariecd51f992017-09-07 16:37:02 -040093 if 'grpc-status' in r.headers:
94 self.assertEqual(int(r.headers['grpc-status']), grpc_status,
95 msg='GRPC Status %d!=%d; msg:%s' % (
96 int(r.headers['grpc-status']),
97 grpc_status,
98 r.headers['grpc-message']))
99 self.assertEqual(r.status_code, expected_http_code,
Zsolt Haraszti66862032016-11-28 14:28:39 -0800100 msg='Code %d!=%d; msg:%s' % (
Stephane Barbariecd51f992017-09-07 16:37:02 -0400101 r.status_code, expected_http_code, r.content))