Skip to content

Commit a89355f

Browse files
committed
bugfix: add missing return statements to error handling; workaround: work around unidentified problem in get_resource which erroneously remebers old headers from previous requests; add 4 new tests covering atom entry and zip file retrieval
1 parent 0ee5d2b commit a89355f

2 files changed

Lines changed: 101 additions & 10 deletions

File tree

sword2/connection.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,19 @@ def _handle_error_response(self, resp, content):
278278
"""
279279
if resp['status'] == "401":
280280
conn_l.error("You are unauthorised (401) to access this document on the server. Check your username/password credentials and your 'On Behalf Of'")
281-
self._return_error_or_exception(NotAuthorised, resp, content)
281+
return self._return_error_or_exception(NotAuthorised, resp, content)
282282
elif resp['status'] == "403":
283283
conn_l.error("You are Forbidden (401) to POST to '%s'. Check your username/password credentials and your 'On Behalf Of'")
284-
self._return_error_or_exception(Forbidden, resp, content)
284+
return self._return_error_or_exception(Forbidden, resp, content)
285285
elif resp['status'] == "408":
286286
conn_l.error("Request Timeout (408) - error uploading.")
287-
self._return_error_or_exception(RequestTimeOut, resp, content)
287+
return self._return_error_or_exception(RequestTimeOut, resp, content)
288288
elif int(resp['status']) > 499:
289289
conn_l.error("Server error occured. Response headers from the server:\n%s" % resp)
290-
self._return_error_or_exception(ServerError, resp, content)
290+
return self._return_error_or_exception(ServerError, resp, content)
291291
else:
292292
conn_l.error("Unknown error occured. Response headers from the server:\n%s" % resp)
293-
self._return_error_or_exception(HTTPResponseError, resp, content)
293+
return self._return_error_or_exception(HTTPResponseError, resp, content)
294294

295295
def _cache_deposit_receipt(self, d):
296296
"""Method for storing the deposit receipts, and also for providing lookup dictionaries that
@@ -1577,9 +1577,13 @@ def get_deposit_receipt(self, edit_iri):
15771577
15781578
FIXME: this explicitly requests the receipt from the server, but there is a
15791579
cache of deposit receipts - how should we access this?
1580+
1581+
FIXME: there's also something funny going on with get_resource remembering
1582+
old headers, but not quite sure where that's coming from. Have to pass in
1583+
packaging and headers explicitly to overcome
15801584
"""
15811585
conn_l.debug("Trying to GET the ATOM Entry Document at %s." % edit_iri)
1582-
response = self.get_resource(edit_iri)
1586+
response = self.get_resource(edit_iri, packaging=None, headers={})
15831587
if response.code == 200:
15841588
conn_l.debug("Attempting to parse the response as a Deposit Receipt")
15851589
d = Deposit_Receipt(xml_deposit_receipt = response.content)
@@ -1673,7 +1677,7 @@ def get_resource(self, content_iri = None,
16731677
conn_l.error("Desired packaging format '%' not available from the server, according to the deposit receipt. Change the client parameter 'honour_receipts' to False to avoid this check.")
16741678
return self._return_error_or_exception(PackagingFormatNotAvailable, {}, "")
16751679
if on_behalf_of:
1676-
headers['On-Behalf-Of'] = self.on_behalf_of
1680+
headers['On-Behalf-Of'] = on_behalf_of
16771681
elif self.on_behalf_of:
16781682
headers['On-Behalf-Of'] = self.on_behalf_of
16791683
if packaging:
@@ -1684,6 +1688,7 @@ def get_resource(self, content_iri = None,
16841688
conn_l.info("IRI GET resource '%s' with Accept-Packaging:%s" % (content_iri, packaging))
16851689
else:
16861690
conn_l.info("IRI GET resource '%s'" % content_iri)
1691+
conn_l.info("Using headers: " + str(headers))
16871692
resp, content = self.h.request(content_iri, "GET", headers=headers)
16881693
_, took_time = self._t.time_since_start("IRI GET resource")
16891694
if self.history:

tests/spec/test_spec.py

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from . import TestController
2-
from sword2 import Connection, Entry
2+
from sword2 import Connection, Entry, Error_Document
33

44
PACKAGE = "tests/spec/example.zip"
55
PACKAGE_MIME = "application/zip"
@@ -193,8 +193,94 @@ def test_10_advanced_retrieve_deposit_receipt(self):
193193
assert new_receipt.parsed == True
194194
assert new_receipt.valid == True
195195

196-
197-
196+
def test_11_basic_retrieve_content_cont_iri(self):
197+
conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW)
198+
conn.get_service_document()
199+
col = conn.sd.workspaces[0][1][0]
200+
with open(PACKAGE) as pkg:
201+
receipt = conn.create(col_iri = col.href,
202+
payload=pkg,
203+
mimetype=PACKAGE_MIME,
204+
filename="example.zip",
205+
packaging='http://purl.org/net/sword/package/SimpleZip')
206+
# ensure that we have a receipt (the server may not give us one
207+
# by default)
208+
receipt = conn.get_deposit_receipt(receipt.location)
209+
210+
# we're going to work with the cont_iri
211+
assert receipt.cont_iri is not None
212+
213+
resource = conn.get_resource(content_iri=receipt.cont_iri)
214+
215+
assert resource.code == 200
216+
assert resource.content is not None
217+
218+
def test_12_basic_retrieve_content_em_iri(self):
219+
conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW)
220+
conn.get_service_document()
221+
col = conn.sd.workspaces[0][1][0]
222+
with open(PACKAGE) as pkg:
223+
receipt = conn.create(col_iri = col.href,
224+
payload=pkg,
225+
mimetype=PACKAGE_MIME,
226+
filename="example.zip",
227+
packaging='http://purl.org/net/sword/package/SimpleZip')
228+
# ensure that we have a receipt (the server may not give us one
229+
# by default)
230+
receipt = conn.get_deposit_receipt(receipt.location)
231+
232+
# we're going to work with the cont_iri
233+
assert receipt.edit_media is not None
234+
235+
resource = conn.get_resource(content_iri=receipt.edit_media)
236+
237+
assert resource.code == 200
238+
assert resource.content is not None
239+
240+
def test_13_advanced_retrieve_content_em_iri(self):
241+
conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW)
242+
conn.get_service_document()
243+
col = conn.sd.workspaces[0][1][0]
244+
with open(PACKAGE) as pkg:
245+
receipt = conn.create(col_iri = col.href,
246+
payload=pkg,
247+
mimetype=PACKAGE_MIME,
248+
filename="example.zip",
249+
packaging='http://purl.org/net/sword/package/SimpleZip')
250+
# ensure that we have a receipt (the server may not give us one
251+
# by default)
252+
receipt = conn.get_deposit_receipt(receipt.location)
253+
254+
packaging = 'http://purl.org/net/sword/package/SimpleZip'
255+
if receipt.packaging is not None and len(receipt.packaging) > 0:
256+
packaging = receipt.packaging[0]
257+
258+
resource = conn.get_resource(content_iri=receipt.edit_media, packaging=packaging, on_behalf_of=SSS_OBO)
259+
260+
assert resource.code == 200
261+
assert resource.content is not None
262+
263+
def test_14_error_retrieve_content_em_iri(self):
264+
conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW,
265+
error_response_raises_exceptions=False)
266+
conn.get_service_document()
267+
col = conn.sd.workspaces[0][1][0]
268+
with open(PACKAGE) as pkg:
269+
receipt = conn.create(col_iri = col.href,
270+
payload=pkg,
271+
mimetype=PACKAGE_MIME,
272+
filename="example.zip",
273+
packaging='http://purl.org/net/sword/package/SimpleZip')
274+
# ensure that we have a receipt (the server may not give us one
275+
# by default)
276+
receipt = conn.get_deposit_receipt(receipt.location)
277+
278+
error = 'http://purl.org/net/sword/package/IJustMadeThisUp'
279+
response = conn.get_resource(content_iri=receipt.edit_media, packaging=error)
280+
281+
assert response.code == 406
282+
assert isinstance(response, Error_Document)
283+
198284

199285

200286

0 commit comments

Comments
 (0)