Skip to content

Commit 98a2982

Browse files
committed
fix bugs on specifying on-behalf-of and packaging headers incorrectly; also add 4 further tests on section 6.7.1
1 parent 83d10b5 commit 98a2982

2 files changed

Lines changed: 89 additions & 6 deletions

File tree

sword2/connection.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ def _make_request(self,
461461
headers = {}
462462
headers['In-Progress'] = str(in_progress).lower()
463463
if on_behalf_of:
464-
headers['On-Behalf-Of'] = self.on_behalf_of
464+
headers['On-Behalf-Of'] = on_behalf_of
465465
elif self.on_behalf_of:
466466
headers['On-Behalf-Of'] = self.on_behalf_of
467467

@@ -525,6 +525,9 @@ def _make_request(self,
525525

526526
elif metadata_entry and filename and payload:
527527
# Multipart resource creation
528+
my_headers = {"Content-MD5" : str(md5sum)}
529+
if packaging is not None:
530+
my_headers['Packaging'] = str(packaging)
528531
multicontent_type, payload_data = create_multipart_related([{'key':'atom',
529532
'type':'application/atom+xml; charset="utf-8"',
530533
'data':str(metadata_entry), # etree default is utf-8
@@ -533,9 +536,7 @@ def _make_request(self,
533536
'type':str(mimetype),
534537
'filename':filename,
535538
'data':payload,
536-
'headers':{'Content-MD5':str(md5sum),
537-
'Packaging':str(packaging),
538-
}
539+
'headers':my_headers
539540
}
540541
])
541542

@@ -566,7 +567,8 @@ def _make_request(self,
566567
headers['Content-MD5'] = str(md5sum)
567568
headers['Content-Length'] = str(f_size)
568569
headers['Content-Disposition'] = "attachment; filename=%s" % filename # TODO: ensure filename is ASCII
569-
headers['Packaging'] = str(packaging)
570+
if packaging is not None:
571+
headers['Packaging'] = str(packaging)
570572

571573
resp, content = self.h.request(target_iri, method, headers=headers, body = payload)
572574
_, took_time = self._t.time_since_start(request_type)
@@ -944,6 +946,7 @@ def add_file_to_resource(self,
944946
filename, # According to spec, "The client MUST supply a Content-Disposition header with a filename parameter
945947
# (note that this requires the filename be expressed in ASCII)."
946948
mimetype=None,
949+
packaging=None,
947950

948951

949952
on_behalf_of=None,
@@ -991,6 +994,7 @@ def add_file_to_resource(self,
991994
return self._make_request(target_iri = edit_media_iri,
992995
payload=payload,
993996
mimetype=mimetype,
997+
packaging=packaging,
994998
filename=filename,
995999
on_behalf_of=on_behalf_of,
9961000
in_progress=in_progress,
@@ -1161,7 +1165,8 @@ def delete(self,
11611165
return self._make_request(target_iri = resource_iri,
11621166
on_behalf_of=on_behalf_of,
11631167
method="DELETE",
1164-
request_type='IRI DELETE')
1168+
request_type='IRI DELETE',
1169+
in_progress=False)
11651170

11661171
def delete_content_of_resource(self, edit_media_iri = None,
11671172
on_behalf_of = None,

tests/spec/test_spec.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,10 +495,88 @@ def test_22_delete_content(self):
495495
assert new_receipt.code == 204
496496
assert new_receipt.dom is None
497497

498+
def test_23_basic_add_content_to_resource_single_file(self):
499+
conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW)
500+
conn.get_service_document()
501+
col = conn.sd.workspaces[0][1][0]
502+
with open(PACKAGE) as pkg:
503+
receipt = conn.create(col_iri = col.href,
504+
payload=pkg,
505+
mimetype=PACKAGE_MIME,
506+
filename="example.zip",
507+
packaging = 'http://purl.org/net/sword/package/SimpleZip')
508+
receipt = conn.get_deposit_receipt(receipt.location)
498509

510+
with open(PACKAGE) as pkg:
511+
new_receipt = conn.add_file_to_resource(receipt.edit_media, pkg, "addition.zip", mimetype=PACKAGE_MIME)
512+
513+
assert new_receipt.code >= 200 and new_receipt.code < 400
514+
assert new_receipt.location is not None
515+
assert new_receipt.location != receipt.edit_media
499516

517+
def test_24_advanced_add_content_to_resource_single_file(self):
518+
conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO)
519+
conn.get_service_document()
520+
col = conn.sd.workspaces[0][1][0]
521+
with open(PACKAGE) as pkg:
522+
receipt = conn.create(col_iri = col.href,
523+
payload=pkg,
524+
mimetype=PACKAGE_MIME,
525+
filename="example.zip",
526+
packaging = 'http://purl.org/net/sword/package/SimpleZip')
527+
receipt = conn.get_deposit_receipt(receipt.location)
528+
529+
with open(PACKAGE) as pkg:
530+
new_receipt = conn.add_file_to_resource(receipt.edit_media, pkg, "addition.zip",
531+
mimetype=PACKAGE_MIME,
532+
metadata_relevant=True)
533+
534+
assert new_receipt.code >= 200 and new_receipt.code < 400
535+
assert new_receipt.location is not None
536+
assert new_receipt.location != receipt.edit_media
500537

538+
def test_25_basic_add_content_to_resource_package(self):
539+
conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW)
540+
conn.get_service_document()
541+
col = conn.sd.workspaces[0][1][0]
542+
with open(PACKAGE) as pkg:
543+
receipt = conn.create(col_iri = col.href,
544+
payload=pkg,
545+
mimetype=PACKAGE_MIME,
546+
filename="example.zip",
547+
packaging = 'http://purl.org/net/sword/package/SimpleZip')
548+
receipt = conn.get_deposit_receipt(receipt.location)
549+
550+
with open(PACKAGE) as pkg:
551+
new_receipt = conn.add_file_to_resource(receipt.edit_media, pkg, "addition.zip",
552+
mimetype=PACKAGE_MIME,
553+
packaging="http://purl.org/net/sword/package/SimpleZip")
554+
555+
assert new_receipt.code >= 200 and new_receipt.code < 400
556+
assert new_receipt.location is not None
557+
assert new_receipt.location == receipt.edit_media
501558

559+
def test_26_advanced_add_content_to_resource_package(self):
560+
conn = Connection(SSS_URL, user_name=SSS_UN, user_pass=SSS_PW, on_behalf_of=SSS_OBO)
561+
conn.get_service_document()
562+
col = conn.sd.workspaces[0][1][0]
563+
with open(PACKAGE) as pkg:
564+
receipt = conn.create(col_iri = col.href,
565+
payload=pkg,
566+
mimetype=PACKAGE_MIME,
567+
filename="example.zip",
568+
packaging = 'http://purl.org/net/sword/package/SimpleZip')
569+
receipt = conn.get_deposit_receipt(receipt.location)
570+
571+
with open(PACKAGE) as pkg:
572+
new_receipt = conn.add_file_to_resource(receipt.edit_media, pkg, "addition.zip",
573+
mimetype=PACKAGE_MIME,
574+
packaging="http://purl.org/net/sword/package/SimpleZip",
575+
metadata_relevant=True)
576+
577+
assert new_receipt.code >= 200 and new_receipt.code < 400
578+
assert new_receipt.location is not None
579+
assert new_receipt.location == receipt.edit_media
502580

503581

504582

0 commit comments

Comments
 (0)