Reputation: 2257
I am attempting to write a test case for an HTTP get operation for my Django site. Here is test.py in that app: (I have changed some names and paths in this post as required by my security group, but the output is otherwise accurate)
from django.test import TestCase
class DashboardTestCase(TestCase):
def test_index(self):
resp = self.client.get('http://myhost.mydomain.com/apiv1/masterservers/apollo/')
self.assertEqual(resp.status_code,200)
When I do a curl on the command line (using either localhost:///apiv1/masterservers/apollo/
or the full URL) It works just fine, and I receive the expected JSON (which is currently just serializing hostname while I'm developing it):
$ curl localhost:///apiv1/masterservers/apollo/
{"servername":"apollo"}
$ curl http://myhost.mydomain.com/apiv1/masterservers/apollo/
{"servername":"apollo"}
The issue that I have is when I run ./manage.py test
, I get the following:
Traceback (most recent call last):
File "/var/www/django-apps/project/dashboard/tests.py", line 8, in test_index
self.assertEqual(resp.status_code,200)
AssertionError: 404 != 200
----------------------------------------------------------------------
Ran 1 test in 0.368s
Another thing to note is that when I remove the hostname (apollo) in this case, and run the tests with just /apiv1/masterservers/
the test works just fine, and the http return code is matched at 200.
Another thing, I installed httpie to use instead of curl because it shows the HTTP header information, and this is the result I got from the command line, same URL:
$ http http://myhost.mydomain.com/apiv1/masterservers/apollo/
HTTP/1.1 200 OK
Allow: GET, PUT, PATCH, DELETE, HEAD, OPTIONS
Connection: Keep-Alive
Content-Type: application/json
Date: Wed, 09 Mar 2016 19:05:16 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.6 (CentOS) PHP/5.4.16 mod_wsgi/3.4 Python/2.7.5
Transfer-Encoding: chunked
Vary: Accept,Cookie
{
"servername": "apollo"
}
Anyone have any reason why this test would be failing with a return code of 404, when the return code is 200 outside of the test?
Upvotes: 0
Views: 315
Reputation: 2257
Just as I went to submit this question, I recalled reading about the test database, and how it's created empty, so that "apollo" didn't exist. Once I created it with the following line, the test passed:
MasterServers.objects.create(servername="apollo")
Upvotes: 1