Do not go through the test client, in this case. If you change the behavior of the test client and then make a request through it, you will not test your input code, which it will really need to process.
You should not trust the test client to create HTTP requests in the same way as the WSGI client. It does a good enough job to get the query parameters in your view, but this is certainly not the same as what you would get from a real query.
The goal of the test client is to protect you from all the messy details of the real request and response objects and just give you the opportunity to check how your views respond to input parameters. In your case, however, you need to check these details.
You must create an HTTPRequest object just like Django, use as many Django functions as you need to create it, and then call your view or your middleware directly with this HTTPRequest object. If your view should throw an exception, use the assertRaises function to test it. If it should return an HTTPResponse with a status code set to 500, then check this - or check them together with a custom assert method if you need to.
source share