What's the best way to simulate a request?

60 views
Skip to first unread message

Andrew

unread,
Jul 7, 2008, 9:46:54 PM7/7/08
to Django users
Here's the situation:

I'm creating an RESTful internal API as an app in our project. Our
'main' app (we'll call it main) is a client of the API app. Since
we're building the API piece by piece as we use it, the emphasis on
all of this is simple simple simple -- with our eye on eventually
opening up pieces of the API to the public.

The general flow looks like:
request => main app handling => API client => API logic.

Because right now both of the apps live in the same project and are
being served from the same boxes (and the API won't be running on a
separate machine for a while), I figured, hey, why not just fake an
HttpRequest from the main app logic and pass it over to API? That way,
we don't need to worry about exposing the internal URLs, we don't need
to worry about authentication right now, it'll be simple.

Right now the API client I've written takes an HTTP method, path, and
params, uses resolve(path, api.urls) to get the view, and then calls
the view using a fake HttpRequest that I populate myself.

The one problem is that I'm finding it much harder to fake the
HttpRequests than I thought -- namely because the RESTful handlers on
the API side are expecting things like request._get_post_and_files()
to be present.

So, my thoughts:
a) Continue using the lightweight API "client" I've written, but
instead of building a HttpRequest, fake a WSGIRequest instead (with
the _minimum_ data needed)
b) Patch the django test client to deal with PUT and DELETE and use
that as our API client
c) Import the API urls.py into the main urls.py and call the API
methods by generating actual HTTP requests

I'm not crazy about c) since it requires more infrastructure work now.

Any thoughts?

Norman Harman

unread,
Jul 8, 2008, 1:26:14 PM7/8/08
to django...@googlegroups.com

Yeah, give more than lip service to "simple simple simple". You say
that then outline how you are reimplementing HTTP because you don't want
to be "exposing the internal URLs" yet.

I suggest spending an hour, a day learning about how your webserver(s)
handle auth and start using it. My $.02US (only about .01 at today's rates)

Baring that, option B is best since the code will at least be useful for
tests. Where as the other options' code become mostly worthless once
you start using real requests.

--
Norman J. Harman Jr.
Senior Web Specialist, Austin American-Statesman
___________________________________________________________________________
You've got fun! Check out Austin360.com for all the entertainment
info you need to live it up in the big city!

Prahlad

unread,
May 17, 2012, 1:36:25 AM5/17/12
to django...@googlegroups.com
To simulate requests I am using following code and works perfect for me:


from django.test.client import Client

c = Client()
response = c.get('/url/you/want/to/request')
Reply all
Reply to author
Forward
0 new messages