|
| 1 | +import pytest |
| 2 | +import responses |
| 3 | +from pytest import raises |
| 4 | +from requests.exceptions import HTTPError |
| 5 | + |
| 6 | +from epo_ops.api import Client |
| 7 | +from epo_ops.models import Docdb |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def ops_backend_413(): |
| 12 | + """ |
| 13 | + Emulate an OPS backend returning 413 on the fulltext endpoint for an |
| 14 | + ambiguous input. The real upstream returns 413 for e.g. EP.0536425/fulltext. |
| 15 | + """ |
| 16 | + token = responses.Response( |
| 17 | + responses.POST, |
| 18 | + url="https://ops.epo.org/3.2/auth/accesstoken", |
| 19 | + status=200, |
| 20 | + json={"access_token": "foo", "expires_in": 42}, |
| 21 | + ) |
| 22 | + fulltext_413 = responses.Response( |
| 23 | + responses.POST, |
| 24 | + url="https://ops.epo.org/3.2/rest-services/published-data/publication/docdb/fulltext", |
| 25 | + status=413, |
| 26 | + headers={"Content-Type": "application/xml"}, |
| 27 | + body=( |
| 28 | + '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' |
| 29 | + ' <fault xmlns="http://ops.epo.org">\n' |
| 30 | + " <code>CLIENT.AmbiguousRequest</code>\n" |
| 31 | + " <message>The request was ambiguous</message>\n" |
| 32 | + " <details>\n" |
| 33 | + " <cause>Ambiguous input: publication/docdb/EP.0536425</cause>\n" |
| 34 | + " <resolution>publication/docdb/EP.0536425.A1</resolution>\n" |
| 35 | + " <resolution>publication/docdb/EP.0536425.A4</resolution>\n" |
| 36 | + " <resolution>publication/docdb/EP.0536425.B1</resolution>\n" |
| 37 | + " <resolution>publication/docdb/EP.0536425.B2</resolution>\n" |
| 38 | + " </details>\n" |
| 39 | + " </fault>" |
| 40 | + ), |
| 41 | + ) |
| 42 | + for response in [token, fulltext_413]: |
| 43 | + responses.add(response) |
| 44 | + |
| 45 | + |
| 46 | +def _issue_fulltext_request(client): |
| 47 | + return client.published_data( |
| 48 | + "publication", |
| 49 | + Docdb("0536425", "EP", "B1"), |
| 50 | + endpoint="fulltext", |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@responses.activate |
| 55 | +def test_413_raises_by_default(ops_backend_413): |
| 56 | + client = Client("key", "secret", middlewares=[]) |
| 57 | + with raises(HTTPError): |
| 58 | + _issue_fulltext_request(client) |
| 59 | + |
| 60 | + |
| 61 | +@responses.activate |
| 62 | +def test_413_returned_when_raise_for_status_disabled(ops_backend_413): |
| 63 | + client = Client("key", "secret", middlewares=[], raise_for_status=False) |
| 64 | + response = _issue_fulltext_request(client) |
| 65 | + assert response.status_code == 413 |
| 66 | + assert "CLIENT.AmbiguousRequest" in response.text |
| 67 | + assert "publication/docdb/EP.0536425.B1" in response.text |
0 commit comments