Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify an IP address with Django test client?

I am testing an API with Django test client. The API uses geo blocking so in my test I need to specify an IP address to make sure it works properly. How can I do that?

I am making a request in my test like this:

from django.test.client import Client as HttpClient
.
.
.
client = HttpClient()
response = client.get(uri + query_string)
like image 405
Richard Knop Avatar asked Feb 07 '13 10:02

Richard Knop


2 Answers

The Client.get() method has an extra keyword arguments parameter, which can be used to specify headers.

c.get(/my-url/, REMOTE_ADDR="127.0.0.1") 
like image 86
Alasdair Avatar answered Sep 21 '22 16:09

Alasdair


Pass REMOTE_ADDR in constructor.

client = HttpClient(REMOTE_ADDR='127.0.0.1')

or

client.get('/path/', {'param':'foo'}, **{'HTTP_USER_AGENT':'firefox-22', 'REMOTE_ADDR':'127.0.0.1'})
like image 44
baklarz2048 Avatar answered Sep 21 '22 16:09

baklarz2048