Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django test always returning 301

I know there are few same posts with this problem, but they doesn't helped for me. I'm always got a 301 status in tests:

self.client.get('/')

and this:

self.client.get('/admin/')

return:

AssertionError: 301 != 200

All urls will returning 301 status... Only way that help is: self.client.get('/', follow=True)

Anybody knows where is problem?

like image 709
Arti Avatar asked Jan 19 '14 09:01

Arti


4 Answers

301 is status for redirection, whitch means your get request first have response that is the 301. Http headers contains the url to redirect to...

If you want your request to follow, you have pass in follow=True, which indicates the method to automatically trigger another request to the redirect url. There can be many redirections.

It's a common error in assertion tests.

like image 142
biodigitals Avatar answered Nov 13 '22 09:11

biodigitals


Open your browser to see if the tailing backslash has caused this issue.

like image 39
Dragonfly Avatar answered Nov 13 '22 09:11

Dragonfly


I had the same error as you described.

My django code is:

response = self.client.get('**/admin**')<br>
self.assertEqual(response.status_code, 200)<br>
AssertionError: 301 != 200

This is my solution:
Option 1

self.client.get('**/admin/**')<br>

Option 2

self.client.get('**/admin**', follow=True) 
like image 42
Signal Fish Avatar answered Nov 13 '22 08:11

Signal Fish


Is the root URL protected by login? That's certainly the case for the admin URL, so it will redirect to the login page unless you have already logged in. If you have protected the root view with @login_required that would explain what you see.

like image 1
Daniel Roseman Avatar answered Nov 13 '22 09:11

Daniel Roseman