2021-12-13 12:42:12 +01:00
|
|
|
from starlette.testclient import TestClient
|
|
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
|
|
from unittest.mock import patch
|
|
|
|
from halfapi.lib.domain_middleware import DomainMiddleware
|
2022-08-08 20:19:25 +02:00
|
|
|
from halfapi import __version__
|
2021-12-13 12:42:12 +01:00
|
|
|
|
|
|
|
def test_init():
|
|
|
|
with patch('starlette.middleware.base.BaseHTTPMiddleware.__init__') as init:
|
2022-08-08 20:19:25 +02:00
|
|
|
mw = DomainMiddleware('app', {
|
|
|
|
'name': 'test',
|
|
|
|
'id': 'randomid',
|
|
|
|
'version': '0.0.0',
|
|
|
|
'halfapi_version': __version__,
|
|
|
|
'config': {}
|
|
|
|
})
|
2021-12-13 12:42:12 +01:00
|
|
|
init.assert_called_once_with('app')
|
2022-08-08 20:19:25 +02:00
|
|
|
assert isinstance(mw.domain, dict)
|
|
|
|
assert isinstance(mw.name, str)
|
|
|
|
assert mw.name == 'test'
|
|
|
|
assert isinstance(mw.config, dict)
|
|
|
|
assert len(mw.config) == 0
|
2021-12-13 12:42:12 +01:00
|
|
|
assert mw.request == None
|
|
|
|
|
|
|
|
def test_call(application_debug):
|
|
|
|
c = TestClient(application_debug)
|
|
|
|
r = c.get('/abc/alphabet')
|
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.headers['x-domain'] == 'dummy_domain'
|
|
|
|
assert r.headers['x-acl'] == 'public'
|
|
|
|
|
|
|
|
r = c.get('/arguments')
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.headers['x-domain'] == 'dummy_domain'
|
|
|
|
assert r.headers['x-acl'] == 'public'
|
|
|
|
assert 'foo' in r.headers['x-args-required']
|
|
|
|
assert 'bar' in r.headers['x-args-required']
|
|
|
|
assert r.headers['x-args-optional'] == 'x'
|
|
|
|
|
|
|
|
c = TestClient(application_debug)
|
|
|
|
r = c.post('/arguments')
|
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.headers['x-domain'] == 'dummy_domain'
|
|
|
|
assert r.headers['x-acl'] == 'public'
|
|
|
|
assert 'foo' in r.headers['x-args-required']
|
|
|
|
assert 'baz' in r.headers['x-args-required']
|
|
|
|
assert r.headers['x-args-optional'] == 'truebidoo'
|
|
|
|
|
|
|
|
|