title: Testing custom Django middleware without using Django itself
body: <p>I have coded my custom Django middleware in the <a href="https://docs.djangoproject.com/en/1.10/topics/http/middleware/" rel="noreferrer">1.10 style</a>, similar to this:</p>
  
  <pre><code>class MyMiddleware(object):
      def __init__(self, get_response):
          self.get_response = get_response
          # some initialization stuff here
  
      def __call__(self, request):
          # Code executed before view functions are called. 
          # Purpose of this middeware is to add new attribute to request
  
          # In brief:
          request.new_attribute = some_function_returning_some_object()
          response = self.get_response(request)
  
          # Code to be executed for each request/response after
          # the view is called.
  
          return response
  </code></pre>
  
  <p>Note, that this middleware is being threaten as a separate Python module, not belonging to any particular application in my project, but living outside and being installed like any other package, via pip. It does not work itself, but only if installed in Django app.  </p>
  
  <p>It works fine, however, I would like to test it. What I've made so far is something like this in <code>my_tests.py</code>:</p>
  
  <pre><code>from my_middleware_module import MyMiddleware
  # some @patches
  def test_mymiddleware():
      request = Mock()
      assert hasattr(request, 'new_attribute') is False # passes obviously
      # CALL MIDDLEWARE ON REQUEST HERE
      assert hasattr(request, 'new_attribute') is True # I want it to pass
  </code></pre>
  
  <p>I don't know how to call middleware on <code>request</code> variable to modify it. I think it would be much easier if I used function-like middleware style, but what if I'm stuck with what I have and I am supposed ony to write tests, without modifying middleware?</p>