title: How to properly unit test a Django middleware
body: <p>I have to write unit tests for several Django Middlewares (Django > 1.10 styled middlewares).</p>
  
  <p>Project is an API, done with Django==2.2.3 and djangorestframework==3.9.4, I'm using standard Django unit testing module, and APIRequestFactory() and APIClient() test functions from Django REST framework to create mock requests for my tests.</p>
  
  <p>Here is an example of one middleware I want to test:</p>
  
  <pre><code>from . import models
  
  class BrowserId():
  
      def __init__(self, get_response):
          self.get_response = get_response
  
      def __call__(self, request):
          if hasattr(request, 'COOKIES') and 'mydomain.bid' in request.COOKIES:
              bid = request.COOKIES.get('mydomain.bid', None)
          else:
              bid = None
  
          request.bid = models.BrowserId(bid) if bid else models.BrowserId.random()
  
          response = self.get_response(request)
  
          response.set_cookie(
              conf.BID_COOKIE_KEY,
              value=request.bid,
              max_age=conf.COOKIE_MAX_AGE,
              domain=conf.BID_COOKIE_DOMAIN
          )
  
          return response
  </code></pre>
  
  <p>I want to test:</p>
  
  <ul>
  <li><p>That browser ID has successfully been added to the request</p></li>
  <li><p>That cookie has successfully been set on the response</p></li>
  </ul>
  
  <p>So, to be more precise, part of the tests would vaguely have to look like this:</p>
  
  <pre><code>from rest_framework.test import APIRequestFactory, APIClient, APITestCase
  from my_api.auth import middlewares, models
  
  class MiddlewaresTestCase(APITestCase):
  
      def test_fresh_browser_id_request(self):
          ??? SOME CODE TO PROCESS THE REQUEST BY THE MIDDLEWARE 
          assert hasattr(request, 'bid')
          assert len(request.bid) == 30
  
      def test_existing_browser_id_request(self):
          req = APIRequestFactory().get('/')
          mock_bid: str = 'abcd1234'
          req.COOKIES = {cookie_key: mock_bid}
          ??? SOME CODE TO PROCESS THE REQUEST BY THE MIDDLEWARE
          assert hasattr(req, 'bid')
          assert req.bid == mock_bid
  
      def test_browser_id_response(self):
          mock_bid: str = 'abc123'
          ??? SOME CODE TO PROCESS THE REQUEST BY THE MIDDLEWARE
          ??? SOME CODE TO PROCESS THE REPONSE BY THE MIDDLEWARE
          response = APIClient().get('/')
          assert res.cookies.get(cookie_key) is not None
          assert res.cookies.get(cookie_key).value == mock_bid
  </code></pre>
  
  <p>etc.</p>
  
  <p>But, as one can see, I don't know exactly how to individually do that. More precisely:</p>
  
  <ul>
  <li><p>I don't know what to pass as a <code>get_response</code> function when initializing the middleware. I know it is supposed to be the processing view function, or the next middleware, but I don't know how to pass that and anyway I want to test it regardless of the rest;</p></li>
  <li><p>I don't know how to test only the processed request and not the returned response.</p></li>
  </ul>