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
|
|
|
|
|
|
|
|
def test_init():
|
|
|
|
with patch('starlette.middleware.base.BaseHTTPMiddleware.__init__') as init:
|
|
|
|
mw = DomainMiddleware('app', 'domain')
|
|
|
|
init.assert_called_once_with('app')
|
|
|
|
assert mw.domain == 'domain'
|
|
|
|
assert mw.request == None
|
|
|
|
|
|
|
|
def test_call(application_debug):
|
|
|
|
c = TestClient(application_debug)
|
2023-01-14 11:10:13 +01:00
|
|
|
r = c.request('get', '/abc/alphabet')
|
2021-12-13 12:42:12 +01:00
|
|
|
assert r.status_code == 200
|
|
|
|
assert r.headers['x-domain'] == 'dummy_domain'
|
|
|
|
assert r.headers['x-acl'] == 'public'
|
|
|
|
|
2023-01-14 11:10:13 +01:00
|
|
|
r = c.request('get', '/arguments')
|
2021-12-13 12:42:12 +01:00
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.headers['x-domain'] == 'dummy_domain'
|
|
|
|
assert r.headers['x-acl'] == 'public'
|
2023-02-21 19:30:59 +01:00
|
|
|
assert 'foo' in r.headers['x-args-required'].split(',')
|
|
|
|
assert 'bar' in r.headers['x-args-required'].split(',')
|
2021-12-13 12:42:12 +01:00
|
|
|
assert r.headers['x-args-optional'] == 'x'
|
|
|
|
|
|
|
|
c = TestClient(application_debug)
|
2023-01-14 11:10:13 +01:00
|
|
|
r = c.request('post', '/arguments')
|
2021-12-13 12:42:12 +01:00
|
|
|
assert r.status_code == 400
|
|
|
|
assert r.headers['x-domain'] == 'dummy_domain'
|
|
|
|
assert r.headers['x-acl'] == 'public'
|
2023-02-21 19:30:59 +01:00
|
|
|
assert 'foo' in r.headers['x-args-required'].split(',')
|
|
|
|
assert 'baz' in r.headers['x-args-required'].split(',')
|
|
|
|
assert 'truebidoo' in r.headers['x-args-optional'].split(',')
|
|
|
|
assert 'z' in r.headers['x-args-optional'].split(',')
|
2021-12-13 12:42:12 +01:00
|
|
|
|
|
|
|
|