X-Git-Url: https://git.openstreetmap.org./nominatim.git/blobdiff_plain/371a780ef427d1b905a729da5f289b5e1099c152..4315debff588a5aa67840eda07bcc85d265f923b:/test/python/api/test_server_glue_v1.py?ds=sidebyside diff --git a/test/python/api/test_server_glue_v1.py b/test/python/api/test_server_glue_v1.py index 538d91f1..5a7430f4 100644 --- a/test/python/api/test_server_glue_v1.py +++ b/test/python/api/test_server_glue_v1.py @@ -7,56 +7,18 @@ """ Tests for the Python web frameworks adaptor, v1 API. """ -from collections import namedtuple import json import xml.etree.ElementTree as ET from pathlib import Path import pytest -from nominatim.config import Configuration +from fake_adaptor import FakeAdaptor, FakeError, FakeResponse + import nominatim.api.v1.server_glue as glue import nominatim.api as napi import nominatim.api.logging as loglib -class FakeError(BaseException): - - def __init__(self, msg, status): - self.msg = msg - self.status = status - - def __str__(self): - return f'{self.status} -- {self.msg}' - -FakeResponse = namedtuple('FakeResponse', ['status', 'output', 'content_type']) - -class FakeAdaptor(glue.ASGIAdaptor): - - def __init__(self, params=None, headers=None, config=None): - self.params = params or {} - self.headers = headers or {} - self._config = config or Configuration(None) - - - def get(self, name, default=None): - return self.params.get(name, default) - - - def get_header(self, name, default=None): - return self.headers.get(name, default) - - - def error(self, msg, status=400): - return FakeError(msg, status) - - - def create_response(self, status, output): - return FakeResponse(status, output, self.content_type) - - - def config(self): - return self._config - # ASGIAdaptor.get_int/bool() @@ -105,7 +67,7 @@ def test_adaptor_parse_format_use_configured(): adaptor = FakeAdaptor(params={'format': 'json'}) assert adaptor.parse_format(napi.StatusResult, 'text') == 'json' - assert adaptor.content_type == 'application/json' + assert adaptor.content_type == 'application/json; charset=utf-8' def test_adaptor_parse_format_invalid_value(): @@ -123,7 +85,7 @@ def test_accepted_languages_from_param(): def test_accepted_languages_from_header(): - a = FakeAdaptor(headers={'http_accept_language': 'de'}) + a = FakeAdaptor(headers={'accept-language': 'de'}) assert a.get_accepted_languages() == 'de' @@ -135,13 +97,13 @@ def test_accepted_languages_from_default(monkeypatch): def test_accepted_languages_param_over_header(): a = FakeAdaptor(params={'accept-language': 'de'}, - headers={'http_accept_language': 'en'}) + headers={'accept-language': 'en'}) assert a.get_accepted_languages() == 'de' def test_accepted_languages_header_over_default(monkeypatch): monkeypatch.setenv('NOMINATIM_DEFAULT_LANGUAGE', 'en') - a = FakeAdaptor(headers={'http_accept_language': 'de'}) + a = FakeAdaptor(headers={'accept-language': 'de'}) assert a.get_accepted_languages() == 'de' @@ -170,7 +132,7 @@ class TestAdaptorRaiseError: def test_json(self): - self.adaptor.content_type = 'application/json' + self.adaptor.content_type = 'application/json; charset=utf-8' err = self.run_raise_error('TEST', 501) @@ -197,14 +159,14 @@ def test_raise_error_during_debug(): loglib.log().section('Ongoing') with pytest.raises(FakeError) as excinfo: - a.raise_error('bad state') + a.raise_error('badstate') content = ET.fromstring(excinfo.value.msg) assert content.tag == 'html' assert '>Ongoing<' in excinfo.value.msg - assert 'bad state' in excinfo.value.msg + assert 'badstate' in excinfo.value.msg # ASGIAdaptor.build_response @@ -227,7 +189,7 @@ def test_build_response_with_status(): assert isinstance(resp, FakeResponse) assert resp.status == 404 assert resp.output == 'stuff\nmore stuff' - assert resp.content_type == 'application/json' + assert resp.content_type == 'application/json; charset=utf-8' def test_build_response_jsonp_with_json(): @@ -239,7 +201,7 @@ def test_build_response_jsonp_with_json(): assert isinstance(resp, FakeResponse) assert resp.status == 200 assert resp.output == 'test.func({})' - assert resp.content_type == 'application/javascript' + assert resp.content_type == 'application/javascript; charset=utf-8' def test_build_response_jsonp_without_json(): @@ -308,7 +270,7 @@ class TestStatusEndpoint: assert isinstance(resp, FakeResponse) assert resp.status == 200 - assert resp.content_type == 'application/json' + assert resp.content_type == 'application/json; charset=utf-8' @pytest.mark.asyncio @@ -546,9 +508,8 @@ class TestSearchEndPointSearch: a.params['q'] = 'something' a.params['city'] = 'ignored' - res = await glue.search_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) - - assert len(json.loads(res.output)) == 1 + with pytest.raises(FakeError, match='^400 -- .*cannot be used together'): + res = await glue.search_endpoint(napi.NominatimAPIAsync(Path('/invalid')), a) @pytest.mark.asyncio