Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Test Client: How to remove http headers

I am trying to write a unit test for a piece of code that checks if a header is missing. How can I omit the http_referer header (or any header for that matter) from Django's test client?

def testCase(self):
    response = self.client.get('/some_url/')
    self.assertNotEqual(500, response.status_code)
like image 239
Sbrom Avatar asked Nov 13 '22 10:11

Sbrom


1 Answers

Instead of using the built-in test client, you could use Django's RequestFactory to build your request, then modify the request object before calling your function:

from django.test.client import RequestFactory
from django.test import TestCase

from yourapp.view import yourfunction

class YourTest(TestCase)
    def setUp(self):
        self.factory = RequestFactory()

    def testCase(self):
        request = self.factory.get('/yoururl/')
        del request.META['HTTP_REFERER']
        yourfunction(request)
like image 133
dm03514 Avatar answered Dec 27 '22 12:12

dm03514