freechat/json-api/client-tests/request.py
shockrah 38ff0edd39 ! Massive test suite overhaul see details below !
Problem: the old test suite was extremely inflexible
This meant that making new tests took way too much time.

+ This new rework makes the new client's backend much thinner and less "magical"
With less magic going on we can pass way more data more easily to the actual
http-request engine making the convenience wrapper over top it much more flexible

Translating old tests to the new engine might take a while but for now the old
client codebase is completely deprecated and will no longer be used+updated
2021-04-21 17:22:16 -07:00

60 lines
1.7 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):
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
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('\tHeaders ', self.headers)
if len(self.qs) != 0:
print('\tQuery Dictionary ', self.qs)
if self.body is not None:
print('\tBody ', str(self.body))
else:
print(f'{GREEN}Pass{NC} {self.method} {self.url}')