librelist archives

« back to archive

request.host in tests

request.host in tests

From:
Darren Worrall
Date:
2012-01-31 @ 11:16
Hi,

We havent needed to use flask's full subdomain support, but we do pull
some information out of the http host in a before_request decorated
function. So in my tests, I can do something like this:

with app.test_request_context('/'):
    flask.request.host = 'test.me.local'
    app.preprocess_request()
    self.assertEqual(
        'test',
        g.subdomain,
    )

To test that we identify the subdomain of the request (if the app is
hosted on me.local), and it works how we want in our production
environment, even though the test is a bit hacky :)

I'm trying to actually test a request with the hostname set now, but
cant seem to get it right. If I do
test_client().get('http://test.me.local/foo'), I get a 404, because
the subdomain support isnt switched on I guess. If I do something
like:

with app.test_client() as c:
    with app.test_request_context('/'):
        flask.request.host = 'test.me.local'
        c.get('/foo')

The http host seems to be None when the request preprocessors fire.

If I create a new instance of the application with SERVER_NAME set to
'me.local' in the config,
test_client().get('http://test.me.local/foo') is still a 404, as is
test_client().get('/foo', 'http://test.me.local')

I fear I'm missing something obvious, any ideas?

Thanks,

Darren

Re: [flask] request.host in tests

From:
Simon Sapin
Date:
2012-01-31 @ 11:59
Le 31/01/2012 12:16, Darren Worrall a écrit :
> If I do something
> like:
>
> with app.test_client() as c:
>      with app.test_request_context('/'):
>          flask.request.host = 'test.me.local'
>          c.get('/foo')

Hi,

Client.get creates a new request context. It also takes the same 
arguments as test_request_context(), as Armin suggested:

c.get('/foo', base_url='http://example.com/')

If you only use the test client, you don’t even need test_request_context.

Regards,
-- 
Simon Sapin

Re: [flask] request.host in tests

From:
Darren Worrall
Date:
2012-01-31 @ 12:49
On 31 January 2012 11:59, Simon Sapin <simon.sapin@exyr.org> wrote:
> Le 31/01/2012 12:16, Darren Worrall a écrit :
>> If I do something
>> like:
>>
>> with app.test_client() as c:
>>      with app.test_request_context('/'):
>>          flask.request.host = 'test.me.local'
>>          c.get('/foo')
>
> Hi,
>
> Client.get creates a new request context. It also takes the same
> arguments as test_request_context(), as Armin suggested:
>
> c.get('/foo', base_url='http://example.com/')
>
> If you only use the test client, you don’t even need test_request_context.
>
> Regards,
> --
> Simon Sapin
Cheers Simon, just what I wanted, thanks for the help.

Re: [flask] request.host in tests

From:
Armin Ronacher
Date:
2012-01-31 @ 11:37
On 2012-01-31 12:16 PM, Darren Worrall wrote:
> Hi,
>
> We havent needed to use flask's full subdomain support, but we do pull
> some information out of the http host in a before_request decorated
> function. So in my tests, I can do something like this:
The correct way to do that would be to specify it when opening the 
context.  The request object should be seen immutable.

with app.test_request_context('/', base_url='http://example.com/'):
     ...

Re: [flask] request.host in tests

From:
Darren Worrall
Date:
2012-01-31 @ 12:49
On 31 January 2012 11:37, Armin Ronacher <armin.ronacher@active-4.com> wrote:
> On 2012-01-31 12:16 PM, Darren Worrall wrote:
>> Hi,
>>
>> We havent needed to use flask's full subdomain support, but we do pull
>> some information out of the http host in a before_request decorated
>> function. So in my tests, I can do something like this:
> The correct way to do that would be to specify it when opening the
> context.  The request object should be seen immutable.
>
> with app.test_request_context('/', base_url='http://example.com/'):
>     ...
Thanks Armin, clears up some of the hackiness.