[Webtest] RESTful testing with Webtest (incl. PUT, DELETE, HEAD, OPTIONS)

Dennis Knochenwefel Dennis Knochenwefel <dennis.knochenwefel@28msec.com>
Mon, 20 Feb 2012 14:13:42 +0100


This is a multi-part message in MIME format.
--------------050904030204050106070809
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Hi all,

I've seen some demand for testing RESTful services (for example useful 
for testing AtomPub servers) with webtest. Currently, this cannot be 
done using webtest because "invoke" does not allow to change the content 
type for POST requests; Plus, HTTP method PUT cannot be used to send 
content.

Marc said earlier a patch would be welcome 
(http://lists.canoo.com/pipermail/webtest/2008q1/010021.html):
 > I don't think that you will be able to send a body currently with PUT 
for instance. Any patch is welcome.

So, I've attached a patch that solves this problem and makes webtest 
usable for any RESTful service (a door opener for a huge comunity). The 
patch should be 100% backwards compatible with hitherto functionality. 
It adds an additional attribute "contentType" which can be used to 
overwrite the content-type HTTP header. Also, I've enabled the PUT 
method to send body data within this patch.

I hope this is helpful for others as well and would be happy if it makes 
it into the trunk. Thank you for all your fantastic work.

cheers,

Dennis

-- 
Dennis Knochenwefel
Software Architect

28msec Inc.
http://www.28msec.com
http://twitter.com/28msec


--------------050904030204050106070809
Content-Type: text/plain;
 name="restful.patch"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="restful.patch"

SW5kZXg6IHNyYy9tYWluL2phdmEvY29tL2Nhbm9vL3dlYnRlc3Qvc3RlcHMvcmVxdWVzdC9J
bnZva2VQYWdlLmphdmENCj09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0NCi0tLSBzcmMvbWFpbi9qYXZhL2NvbS9j
YW5vby93ZWJ0ZXN0L3N0ZXBzL3JlcXVlc3QvSW52b2tlUGFnZS5qYXZhCShSZXZpc2lvbiAx
MTc0NzYpDQorKysgc3JjL21haW4vamF2YS9jb20vY2Fub28vd2VidGVzdC9zdGVwcy9yZXF1
ZXN0L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YWxzKGdldE1ldGhvZCgpKSAmJiBnZXRDb250ZW50KCkg
PT0gbnVsbCAmJiBnZXRDb250ZW50RmlsZSgpID09IG51bGwsCi0gICAgICAgICAgICAgICAg
Ik9uZSBvZiAnY29udGVudCcgb3IgJ2NvbnRlbnRGaWxlJyBtdXN0IGJlIHNldCBmb3IgUE9T
VC4iKTsKKyAgICAJc3VwZXIudmVyaWZ5UGFyYW1ldGVycygpOworICAgIAludWxsUGFyYW1D
aGVjayhnZXRVcmwoKSwgInVybCIpOworICAgICAgICBwYXJhbUNoZWNrKGdldENvbnRlbnQo
KSAhPSBudWxsICYmIGdldENvbnRlbnRGaWxlKCkgIT0gbnVsbCwgCisgICAgICAgIAkJIk9u
bHkgb25lIG9mICdjb250ZW50JyBhbmQgJ2NvbnRlbnRGaWxlJyBtdXN0IGJlIHNldC4iKTsK
KyAgICAJCisgICAgICAgIFN0cmluZyBtZXRob2Q9Z2V0TWV0aG9kKCkudG9VcHBlckNhc2Uo
KTsKKyAgICAgICAgcGFyYW1DaGVjayghIlBPU1QiLmVxdWFscyhtZXRob2QpIAorICAgICAg
ICAJCSYmICEiR0VUIi5lcXVhbHMobWV0aG9kKQorICAgICAgICAJCSYmICEiUFVUIi5lcXVh
bHMobWV0aG9kKQorICAgICAgICAJCSYmICEiREVMRVRFIi5lcXVhbHMobWV0aG9kKQorICAg
ICAgICAJCSYmICEiSEVBRCIuZXF1YWxzKG1ldGhvZCkKKyAgICAgICAgCQkmJiAhIk9QVElP
TlMiLmVxdWFscyhtZXRob2QpLAorICAgICAgICAgICAgICAgICInbWV0aG9kJyBtdXN0IGJl
IG9uZSBvZiBHRVQsIFBPU1QsIFBVVCwgREVMRVRFLCBIRUFELCBvciBPUFRJT05TLiIpOwor
ICAgICAgICBwYXJhbUNoZWNrKCJQT1NUIi5lcXVhbHMobWV0aG9kKSAmJiBnZXRDb250ZW50
KCkgPT0gbnVsbCAmJiBnZXRDb250ZW50RmlsZSgpID09IG51bGwsCisgICAgICAgIC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AgICAgICAgaGVhZGVycy5wdXQoIkNvbnRlbnQtdHlwZSIsICJhcHBsaWNhdGlvbi94LXd3
dy1mb3JtLXVybGVuY29kZWQiKTsKICAgICAgICAgfQotICAgICAgICBzZXR0aW5ncy5zZXRB
ZGRpdGlvbmFsSGVhZGVycyhoZWFkZXJzKTsKKyAgICAgICAgCisgICAgICAgIHJlcXVlc3Qu
c2V0QWRkaXRpb25hbEhlYWRlcnMoaGVhZGVycyk7CiAgICAgICAgIGZpbmFsIFN0cmluZyBj
b250ZW50OwogICAgICAgICBpZiAoZ2V0Q29udGVudCgpICE9IG51bGwpIHsKICAgICAgICAg
ICAgIGNvbnRlbnQgPSBnZXRDb250ZW50KCk7CkBAIC0xODQsOSArMjIwLDkgQEANCiAgICAg
ICAgIGVsc2UgewogICAgICAgICAgICAgY29udGVudCA9IEZpbGVVdGlsLnJlYWRGaWxlVG9T
dHJpbmcoZ2V0Q29udGVudEZpbGUoKSwgdGhpcyk7CiAgICAgICAgIH0KLSAgICAgICAgc2V0
dGluZ3Muc2V0UmVxdWVzdEJvZHkoY29udGVudCk7Ci0gICAgICAgIHNldHRpbmdzLnNldENo
YXJzZXQoY2hhcnNldCk7Ci0gICAgICAgIHJldHVybiBnZXRSZXNwb25zZShzZXR0aW5ncyk7
CisgICAgICAgIHJlcXVlc3Quc2V0UmVxdWVzdEJvZHkoY29udGVudCk7CisgICAgICAgIHJl
cXVlc3Quc2V0Q2hhcnNldChjaGFyc2V0KTsKKyAgICAgICAgcmV0dXJuIGdldFJlc3BvbnNl
KHJlcXVlc3QpOwogICAgIH0KIAogICAgIHByb3RlY3RlZCBTdHJpbmcgZ2V0TG9nTWVzc2Fn
ZUZvclRhcmdldCgpIHsK
--------------050904030204050106070809--