2021-01-23 09:30:53 +01:00
|
|
|
import json
|
|
|
|
import decimal
|
2021-03-12 19:01:16 +01:00
|
|
|
import datetime
|
2021-01-23 09:30:53 +01:00
|
|
|
|
2021-10-07 13:58:03 +02:00
|
|
|
from starlette.responses import Response
|
|
|
|
from halfapi.lib.responses import *
|
2021-01-23 09:30:53 +01:00
|
|
|
|
|
|
|
def test_orjson():
|
|
|
|
test_obj = {
|
|
|
|
"ok": "ko",
|
|
|
|
"dec": decimal.Decimal(42),
|
2021-03-12 19:01:16 +01:00
|
|
|
"set": {0, 4, 2},
|
|
|
|
"date": datetime.date(1,1,1),
|
|
|
|
"datetime": datetime.datetime(1,1,1),
|
2021-01-23 09:30:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
resp = ORJSONResponse(test_obj)
|
|
|
|
body = resp.body.decode()
|
|
|
|
test_obj_dec = json.loads(body)
|
|
|
|
print(test_obj_dec)
|
|
|
|
assert 'ok' in test_obj_dec.keys()
|
|
|
|
assert isinstance(test_obj_dec['ok'], str)
|
|
|
|
assert isinstance(test_obj_dec['dec'], str)
|
2021-01-25 10:44:07 +01:00
|
|
|
assert isinstance(test_obj_dec['set'], list)
|
2021-03-12 19:01:16 +01:00
|
|
|
assert isinstance(test_obj_dec['date'], str)
|
|
|
|
assert test_obj_dec['date'] == '0001-01-01'
|
|
|
|
assert test_obj_dec['datetime'] == '0001-01-01T00:00:00'
|
2021-10-07 13:58:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_responses():
|
|
|
|
resp = HJSONResponse('')
|
|
|
|
assert isinstance(resp, Response)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
|
|
resp = ORJSONResponse('')
|
|
|
|
assert isinstance(resp, Response)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
|
|
resp = PlainTextResponse()
|
|
|
|
assert isinstance(resp, Response)
|
|
|
|
assert resp.status_code == 200
|
|
|
|
|
|
|
|
def test_errors():
|
|
|
|
resp = ServiceUnavailableResponse()
|
|
|
|
assert isinstance(resp, Response)
|
|
|
|
assert resp.status_code == 503
|
|
|
|
|
|
|
|
resp = UnauthorizedResponse()
|
|
|
|
assert isinstance(resp, Response)
|
|
|
|
assert resp.status_code == 401
|
|
|
|
|
|
|
|
resp = InternalServerErrorResponse()
|
|
|
|
assert isinstance(resp, Response)
|
|
|
|
assert resp.status_code == 500
|
|
|
|
|
|
|
|
resp = NotFoundResponse()
|
|
|
|
assert isinstance(resp, Response)
|
|
|
|
assert resp.status_code == 404
|
|
|
|
|
|
|
|
resp = NotImplementedResponse()
|
|
|
|
assert isinstance(resp, Response)
|
|
|
|
assert resp.status_code == 501
|