1

when run develop server (py manage.py runserver), and user browser with url "localhost:8000//index.html", it is giving 404.

but when run test, client.get('//index.html') is 200, and content is same as request to ('/').

when run develop server, using py manage.py shell, then from django.test import Client, directly run is same result.

so is it an issue of Client.get() function? or I am using it wrongly?

 def test_invalid_url2(self): response=self.client.get('//index.html') self.assertEqual(response.status_code, 404) 
1

1 Answer 1

2

A URL with multiple slashes is perfectly valid. (See RFC 2396 for all the gory technical details.) However, they are not treated equally by all software. You are seeing a difference here between the browser behavior and the Django test client.

At the end of the day, you need to ask yourself, what are you really testing here? Do you really care how double slashes are handled? This seems like a detail that is best left to django to parse as it will and leave the django developers to testing that behavior.

Your test should only test the behavior of your own app. For my own apps, I test that routes return a 200 when they are accessible. Or if I explicitly deny access using Django's permissions, then I'll will write a test that a 403 or 404 is returned as appropriate.

Also, I never use explicit paths in my tests. Instead, I use reverse() to look up the path for a given name.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks. i have '' and '/', that should landing to a default homepage. then i tried to test if add anything should be 404, but mistakingly typed '//index.html' instead of '/index.html'. the later one which is '/index.html' is giving back 404 as expected. but the previous one which i typed wrong at beginning is giving 200 with the default homepage. as it confuses me so put a question here. if it's not appropriate sorry about that. thanks.
@BonLee For the test that returns 200, what is response.url?
the returned Response from Client object usring get, does not have .url attribute. but it have returned content from server. the content is the one matching to '' which defined in urls.py and should be something like "localhost:8000"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.