from django.http.response import HttpResponse
from django.test import TestCase
from mock import MagicMock

from rewrite_external_links.middleware import RewriteExternalLinksMiddleware


class TestRewriteExternalLinksMiddleware(TestCase):
    def setUp(self):
        self.middleware = RewriteExternalLinksMiddleware()

    def test_no_response_content(self):
        """response doesn't change if no response content."""
        request = MagicMock()
        content_type = 'application/thraud+xml'
        initial_response = HttpResponse(content_type=content_type)
        processed_response = self.middleware.process_response(
            request=request,
            response=initial_response,
        )
        self.assertEqual(processed_response, initial_response)

    def test_other_content_type(self):
        """response doesn't change if `Content-Type` is not `text/html`."""
        request = MagicMock()
        content_type = 'application/thraud+xml'
        initial_response = HttpResponse(content=b'0x0', content_type=content_type)
        processed_response = self.middleware.process_response(
            request=request,
            response=initial_response,
        )
        self.assertEqual(processed_response, initial_response)

    def test_other_request_path_info(self):
        request = MagicMock()
        request.META['PATH_INFO'] = '/another-url/'
        content_type = 'text/html'
        initial_response = HttpResponse(content=b'0x0', content_type=content_type)
        processed_response = self.middleware.process_response(
            request=request,
            response=initial_response,
        )
        self.assertEqual(processed_response, initial_response)

@@ -39,16 +39,69 @@
 esponse%0A
+from django.template.defaultfilters import urlencode%0A
 from dja
@@ -796,34 +796,28 @@
 response
-, initial_response
+.content, ''
 )%0A%0A    d
@@ -996,32 +996,87 @@
 ion/thraud+xml'%0A
+        content = b'%3Ca href=%22http://example.com%22%3E%3C/a%3E'%0A
         initial_
@@ -1099,38 +1099,39 @@
 esponse(content=
-b'0x0'
+content
 , content_type=c
@@ -1327,34 +1327,33 @@
 response
-, initial_response
+.content, content
 )%0A%0A    d
@@ -1445,9 +1445,12 @@
 META
-%5B
+ = %7B
 'PAT
@@ -1460,27 +1460,28 @@
 NFO'
-%5D = '/another-url
+: '/external-link
 /'
+%7D
 %0A   
@@ -1508,24 +1508,79 @@
 'text/html'%0A
+        content = b'%3Ca href=%22http://example.com%22%3E%3C/a%3E'%0A
         init
@@ -1619,14 +1619,15 @@
 ent=
-b'0x0'
+content
 , co
@@ -1839,24 +1839,760 @@
 onse
-, initial_response
+.content, content)%0A%0A    def test_response_content(self):%0A        path = 'test-path'%0A        link = 'http://example.com'%0A        request = MagicMock()%0A        request.META = %7B'PATH_INFO': '/another-url/'%7D%0A        request.path = path%0A        content_type = 'text/html'%0A        content = b'%3Ca    href=%22%7B%7D%22%3E%3C/a%3E'.format(link)%0A        initial_response = HttpResponse(content=content, content_type=content_type)%0A        processed_response = self.middleware.process_response(%0A            request=request,%0A            response=initial_response,%0A        )%0A%0A        expected = '%3Ca    href=%22/external-link/?link=%7B%7D&next=%7B%7D%22%3E%3C/a%3E'.format(%0A            urlencode(link, safe=''),%0A            path,%0A        )%0A        self.assertEqual(processed_response.content, expected
 )%0A

Update tests