Set referer header in test client?

804 views
Skip to first unread message

Tim Sawyer

unread,
Sep 29, 2008, 5:20:40 PM9/29/08
to Django users
Hi,

I'm having a problem testing with:

c = Client()
response = c.post('/guestbook/', {
'name' : 'name',
'email' : 'email',
'realurl' : 'url',
})

in that my view checks the referer header available from
request.META['HTTP_REFERER'] to make sure the form was submitted from the
correct url.

Can I simulate this header with the test client?

Thanks,

Tim.

Russell Keith-Magee

unread,
Sep 29, 2008, 9:21:31 PM9/29/08
to django...@googlegroups.com

Yes.

The full method signature for client.post() is:
def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):

'extra' is an argument that allows you to specify any additional
header data that you want to send with the request. So, if you want to
set the HTTP REFERRER in your request, make a call like:

>>> response = c.post('/guestbook/', {...your data...}, HTTP_REFERRER='foo')

Yours
Russ Magee %-)

Tim Sawyer

unread,
Sep 30, 2008, 3:27:50 PM9/30/08
to django...@googlegroups.com
On Tuesday 30 Sep 2008, Russell Keith-Magee wrote:
> > Can I simulate this header with the test client?
>
> Yes.
>
> The full method signature for client.post() is:
> def post(self, path, data={}, content_type=MULTIPART_CONTENT, **extra):
>
> 'extra' is an argument that allows you to specify any additional
> header data that you want to send with the request. So, if you want to
>
> set the HTTP REFERRER in your request, make a call like:
> >>> response = c.post('/guestbook/', {...your data...},
> >>> HTTP_REFERRER='foo')
>
> Yours
> Russ Magee %-)

Excellent, thanks!

Tim.


Reply all
Reply to author
Forward
0 new messages