'A POST HTTPS request with a bad referer is rejected.'

@override_settings(DEBUG=True)
def test_https_malformed_referer(self):

    malformed_referer_msg = 'Referer    checking    failed    -    Referer    is    malformed.'
    req = self._get_POST_request_with_token()
    req._is_secure_override = True
    req.META['HTTP_REFERER'] = 'http://http://www.example.com/'
    response = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
    self.assertContains(response, 'Referer    checking    failed    -    Referer    is    insecure    while    host    is    secure.', status_code=403)
    req.META['HTTP_REFERER'] = ''
    response = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
    self.assertContains(response, malformed_referer_msg, status_code=403)
    req.META['HTTP_REFERER'] = '\xc3\x98B\xc3\xb6I\xc3\x9f'
    response = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
    self.assertContains(response, malformed_referer_msg, status_code=403)
    req.META['HTTP_REFERER'] = '//example.com/'
    response = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
    self.assertContains(response, malformed_referer_msg, status_code=403)
    req.META['HTTP_REFERER'] = 'https://'
    response = CsrfViewMiddleware().process_view(req, post_form_view, (), {})
    self.assertContains(response, malformed_referer_msg, status_code=403)
