Django has a nice support for unit and functional testing; however, its django.test.client.Client class does not support PUT and DELETE requests, which might be useful if, like me, you’re doing some kind of REST implementation using that framework. There’s an open ticket about it, but for the time being, here’s my wrapper that supports those methods as well as GET and POST:
Hope it helps! Of course you could extend this to support OPTIONS, HEAD or other HTTP methods you could find in the specification.
Update, 2008-03-05: Following Yoan’s comment below, I’ve DRYed the code a bit. Neat.
Update, 2008-03-11: I’ve added HTTP Basic Authentication support to the class (and changed the post title accordingly).
Update, 2008-03-13: Another modification: now the client supports HTTP Digest Authentication (Yay! To use it, make a first call to your server and then pass the response as a parameter to the http_digest_login method), plus support for the OPTIONS verb, plus another method which sends a method with a “HELLO” verb, which of course does not exist… and which will (normally) return a 405 response!
3 Comments
#1. Yoan 03.05.2008
use:
class RESTClient(DjangoClient)
and you’ll spare the first 5 lines of your script, get and post are really redundant as is imho.
Cheers,
#2. adrian 03.05.2008
Good idea!
#3. bruno 03.05.2008
Thanks, I also needed this.
Commenting