def test_missing_content_type(self): request = RequestFactory().get('/') response = HttpResponse('') del response['Content-Type'] middleware = RandomCommentMiddleware() processed_response = middleware.process_response(request, response) self.assertEqual(response, processed_response)
def test_exemption(self): html = '''<html> <head><title>Test</title></head> <body><p>Test body.</p></body> </html>''' response = HttpResponse(html) response._random_comment_exempt = True request = RequestFactory().get('/') middleware = RandomCommentMiddleware() response = middleware.process_response(request, response) self.assertEqual(force_text(response.content), html)
def test_unicode_characters(self): html = '''<!doctype html> <html> <head> <title>Page title</title> </head> <body> <h1>Test</h1> <p>{0}</p> </body> </html>'''.format(''.join(chr(x) for x in range(9999))) response = HttpResponse(html, content_type='text/html') request = RequestFactory().get('/') middleware = RandomCommentMiddleware() response = middleware.process_response(request, response) self.assertNotEqual(force_text(response.content), force_text(html))
def test_html_content_type(self): html = '''<!doctype html> <html> <head> <title>Page title</title> </head> <body> <h1>Test</h1> <p>Lorem ipsum</p> </body> </html>''' response = HttpResponse(html, content_type='text/html') request = RequestFactory().get('/') middleware = RandomCommentMiddleware() response = middleware.process_response(request, response) self.assertNotEqual(response.content, html)
def test_empty_response_body_ignored(self): request = RequestFactory().get('/') response = HttpResponse('') middleware = RandomCommentMiddleware() processed_response = middleware.process_response(request, response) self.assertEqual(len(processed_response.content), 0)
def test_noop_on_wrong_content_type(self): response = HttpResponse('abc', content_type='text/plain') request = RequestFactory().get('/') middleware = RandomCommentMiddleware() response = middleware.process_response(request, response) self.assertEqual(response.content, b'abc')