freechat/json-api/client-tests/request.py
shockrah e2e68c51ba - Removing old client test code in favor of new structure
+ Moving tests to the new client
- Removing web/ module
! Currently all tests are passing 17/17 but the real trickery comes with doing this
on CI which should will likely take some magic somewhere
Or we'll just extend the freechat docker image to finally have all the required
dependancies and just test on that with diesel and what not
2021-04-21 21:38:25 -07:00

78 lines
2.5 KiB
Python

from requests import Session
import requests
NC = '\033[0m'
RED = '\033[1;31m'
GREEN = '\033[1;32m'
class Request:
def __init__(self, url: str, method: str, qs: dict, headers: dict, hope: int, body=None, verbose=False):
self.method = method
self.url = url
# query string parameters are appended to the url which is why we do this
self.qs = qs
self.headers = headers
self.body = body
self.response = None
self.hope = hope
# This flag lets us control how much output we want to see
self.verbose = verbose
@property
def passing(self):
if self.response is None:
return False
else:
return self.response.status_code == self.hope
def fire(self) -> requests.Response:
try:
if self.method == 'get':
self.response = requests.get(self.url, headers=self.headers, params=self.qs)
elif self.method == 'post':
self.response = requests.post(self.url, headers=self.headers, params=self.qs, data=self.body)
elif self.method == 'delete':
self.response = requests.delete(self.url, headers=self.headers, params=self.qs)
return self.response
except:
return None
def show_response(self):
if self.response is None:
print('Response := None')
return
real_code = self.response.status_code
if self.hope != real_code:
abstract = RED + 'Fail ' + NC + 'Expected ' + str(self.hope) + ' Got ' + str(real_code)
print(abstract)
print('\t', self.method, ' ', self.url)
if len(self.headers) != 0:
print('\tRequest-Headers ', self.headers)
if len(self.qs) != 0:
print('\tQuery-Dictionary ', self.qs)
if self.body is not None:
print('\tRequest-Body ', str(self.body))
if self.verbose:
print(f'\tResponse-Status {self.response.status_code}')
print(f'\tResponse-Headers {self.response.headers}')
print(f'\tResponse-Text {self.response.text}')
else:
print(f'{GREEN}Pass{NC} {self.method} {self.url}')
if self.verbose:
print(f'\tResponse-Status {self.response.status_code}')
print(f'\tResponse-Headers {self.response.headers}')
print(f'\tResponse-Text {self.response.text}')