Zack Williams | 41513bf | 2018-07-07 20:08:35 -0700 | [diff] [blame] | 1 | # 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 Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 14 | from unittest import TestCase |
| 15 | from requests import get, post, put, patch, delete |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 16 | import urllib3 |
| 17 | |
| 18 | # This is to suppress the insecure request warning when using |
| 19 | # self signed ssl certificates |
| 20 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 21 | |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 22 | # 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 Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 24 | |
| 25 | class RestBase(TestCase): |
| 26 | |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 27 | base_url = 'https://localhost:8881' |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 28 | |
| 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 Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 35 | if 'grpc-status' in response.headers and int(response.headers['grpc-status']) != 0: |
| 36 | return None |
| 37 | |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 38 | 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 Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 51 | def get(self, path, expected_http_code=200, grpc_status=0, |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 52 | expected_content_type='application/json', headers=None, verify=False): |
| 53 | r = get(self.url(path), headers=headers, verify=verify) |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 54 | 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 Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 61 | msg='Code %d!=%d; msg:%s' % ( |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 62 | r.status_code, expected_http_code, r.content)) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 63 | return self.verify_content_type_and_return(r, expected_content_type) |
| 64 | |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 65 | def post(self, path, json_dict=None, expected_http_code=201, grpc_status=0, verify=False): |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 66 | r = post(self.url(path), json=json_dict, verify=verify) |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 67 | 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 Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 74 | msg='Code %d!=%d; msg:%s' % ( |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 75 | r.status_code, expected_http_code, r.content)) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 76 | return self.verify_content_type_and_return(r, 'application/json') |
| 77 | |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 78 | def put(self, path, json_dict, expected_http_code=200, grpc_status=0, verify=False): |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 79 | r = put(self.url(path), json=json_dict, verify=verify) |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 80 | 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 Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 87 | msg='Code %d!=%d; msg:%s' % ( |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 88 | r.status_code, expected_http_code, r.content)) |
Zsolt Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 89 | return self.verify_content_type_and_return(r, 'application/json') |
| 90 | |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 91 | def delete(self, path, expected_http_code=209, grpc_status=0, verify=False): |
ubuntu | c5c83d7 | 2017-07-01 17:57:19 -0700 | [diff] [blame] | 92 | r = delete(self.url(path), verify=verify) |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 93 | 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 Haraszti | 6686203 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 100 | msg='Code %d!=%d; msg:%s' % ( |
Stephane Barbarie | cd51f99 | 2017-09-07 16:37:02 -0400 | [diff] [blame] | 101 | r.status_code, expected_http_code, r.content)) |