Sanic: Sanic test client follows redirects

Created on 25 Sep 2020  ·  7Comments  ·  Source: sanic-org/sanic

Describe the bug
When using the test client to test a route that returns a sanic.response.redirect, the test client follows the actual redirect and then returns the response instead of simply returning the redirect response. This isn't handy to make assertions about the redirect response. It also generates unnecessary network traffic when the redirect points to an actual domain. It makes tests rely on network status, making them change their behaviour if network is down.

Code snippet

from sanic.blueprints import Blueprint
from sanic.response import redirect
from sanic import Sanic
from unittest import TestCase
import unittest

bp = Blueprint('test')

@bp.route('/redir/', methods=['GET'])
async def redir(request):
    return redirect('https://example.com/')

app = Sanic('test')
app.register_blueprint(bp)

class TestTest(TestCase):
    def test_test(self):
        request, response = app.test_client.get('/redir/')
        print(response.status)

unittest.main()
# Shows 200

Expected behavior
The test client should return the actual redirect response, to be able to make assertions about it and avoid unnecessary network interactions when running tests. This means the snippet above should show the redirect status instead of 200.

Environment (please complete the following information):

  • OS: Pop!_OS 20.04 LTS
  • Sanic Version 20.6.3
  • Python Version 3.8.2

Additional context
Add any other context about the problem here.

beginner documentation help wanted

All 7 comments

@ohemelaar

From my point of view, that also seems weird and not logical behavior, the response should return 302 HTTP status.

@ahopkins @sjsadowski if this looks like a bug, I can take a look a bit closer to it.

Note, the sanic test client has been decoupled from main Sanic and moved to a new repository, here:
https://github.com/huge-success/sanic-test
It might be better to raise this Issue on that repo, rather than this one.

That makes sense why it is happening. This is default behavior for httpx.

You should be able to pass allow_redirects=False to disable this. I have not tested it.

I just submitted PR #1935 which mainly deals with the testing module. I thought about adding this small change in, but it is technically a breaking change. And I would only be comfortable making it knowing that it is configurable. Seeing as the test client does pass through **kwargs to httpx, you should be able to control that.

The decoupling is not quite ready and will not be a part of this current release. If there is a strong enough case for changing the default behavior and not relying upon kwargs, then we can talk about doing that in the other repo.

@ashleysommer

It might be better to raise this Issue on that repo, rather than this one.

sanic-test doesn't seem to be installable with pip as of today (I can't find in on PyPI either), so I can't update my test snippet and check if the behaviour is the same there.

@ahopkins I tested by passing allow_redirects=True and it has the expected behaviour. This workaround is fine for me. It could be useful to show it in the testing documentation though.

EDIT: I meant allow_redirects=False

sanic-test doesn't seem to be installable with pip as of today (I can't find in on PyPI either), so I can't update my test snippet and check if the behaviour is the same there.

Correct. Still unreleased. If you want to use it, you would have to install from git.

This workaround is fine for me. It could be useful to show it in the testing documentation though.

Agreed. PRs welcome.

Testing has been removed from Sanic core.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

misakar picture misakar  ·  4Comments

eseglem picture eseglem  ·  4Comments

sirex picture sirex  ·  4Comments

geekpy picture geekpy  ·  4Comments

fiecato picture fiecato  ·  3Comments