On Sat, Mar 14, 2009 at 5:21 AM, Chris <chriss...@gmail.com> wrote:
>
> I'm trying to test an email sending feature, and the email text
> requires the current domain, which I'm getting from request.META
> ['HTTP_HOST']. On a normal system, this works fine, but when I access
> it using the test framework and django.test.Client, I get a KeyError
> exception for 'HTTP_HOST'.

The test client doesn't currently provide the HTTP_HOST header by
default. It does provide the SERVER_NAME header, because that header
is required as part of the WSGI spec:

http://www.python.org/dev/peps/pep-0333/

> I tried instantiating the Client with and without the HTTP_HOST
> argument, but both generate the error. Is the standard response meta
> data shown at 
> http://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpRequest.META
> not supported for the test client? If it is supported, how can I
> ensure the response contains this meta data?

If you read the docs on HttpRequest more closely, you will see that
they note that the headers that are available depend on the client and
the server. HTTP_HOST is not guaranteed to be available.

However, if you want to inject HTTP_HOST into the requests made by the
test client, you should be able to do so - either using arguments to
the client at time of construction:

>>> c = Client(HTTP_HOST='example.com')

or by manipulating the defaults of an existing test client:

>>> client.defaults = {'HTTP_HOST':'example.com'}

or by providing an 'extra' argument to an individual request:

>>> client.get('/foo/bar/',extra={'HTTP_HOST':'example.com'}

If any of these approaches aren't working for you, you will need to
tell us the error you are seeing before we can help.

Yours,
Russ Magee %-)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to