def test_success(self): """ Status codes indicating success pass the response through without any exceptions. """ def _response_for(method, url, params, headers, data): return 200, {}, b'hello world' resource = StringStubbingResource(_response_for) treq = StubTreq(resource) request = documint_request_factory(treq.request) self.assertThat( request(b'GET', b'http://example.com/success'), succeeded( AfterPreprocessing( treq.content, succeeded(Equals(b'hello world')))))
def test_malformed_error(self): """ Documint errors that do not have a JSON content type raise `MalformedDocumintError`. """ def _response_for(method, url, params, headers, data): return 400, {}, b'an error' resource = StringStubbingResource(_response_for) treq = StubTreq(resource) request = documint_request_factory(treq.request) self.assertThat( request(b'GET', b'http://example.com/malformed_error'), failed( AfterPreprocessing( lambda f: f.value, MatchesAll( IsInstance(MalformedDocumintError), MatchesStructure(data=Equals(b'an error'))))))
def test_not_json_error(self): """ Documint errors that have a JSON content type but do not contain valid JSON raise `MalformedDocumintError`. """ def _response_for(method, url, params, headers, data): return (400, {b'Content-Type': b'application/json'}, b'hello world') resource = StringStubbingResource(_response_for) treq = StubTreq(resource) request = documint_request_factory(treq.request) self.assertThat( request(b'GET', b'http://example.com/not_json_error'), failed( AfterPreprocessing( lambda f: f.value, MatchesAll( IsInstance(MalformedDocumintError), MatchesStructure(data=Equals(b'hello world'))))))
def test_error(self): """ Documint errors are parsed into a structured exception. """ def _response_for(method, url, params, headers, data): return (400, {b'Content-Type': b'application/json'}, json.dumps({u'causes': [ {u'type': u'foo', u'reason': 42, u'description': u'nope'}, {u'type': u'bar', u'reason': 42, u'description': None}, {u'type': u'baz', u'reason': None, u'description': None}]})) resource = StringStubbingResource(_response_for) treq = StubTreq(resource) request = documint_request_factory(treq.request) def cause(t, r=None, d=None): return MatchesStructure(type=Equals(t), reason=Equals(r), description=Equals(d)) self.assertThat( request(b'GET', b'http://example.com/error'), failed( AfterPreprocessing( lambda f: f.value, MatchesAll( IsInstance(DocumintError), MatchesStructure( causes=MatchesListwise([ cause(u'foo', 42, u'nope'), cause(u'bar', 42), cause(u'baz')]))))))