Example #1
0
 def do_inject(self, encoding=None):
     inject = self.portal.restrictedTraverse('@@mailrouter-inject')
     inject.request = makerequest(self.portal)
     inject.request.response = HTTPResponse()
     mailfile = open_mailfile('mail_plain.txt')
     mail = mailfile.read()
     mailfile.close()
     if encoding is not None:
         mail = safe_unicode(mail).encode(encoding)
     inject.request.stdin = StringIO(mail)
     return inject()
Example #2
0
    def test_handleException(self):
        response = HTTPResponse()
        try:
            raise ValueError(1)
        except:
            exc_info = sys.exc_info()

        response.handleException(exc_info)
        self.assertEquals(response.getHeader("content-type"),
                          "text/html;charset=utf-8")
        self.assertEquals(response.getStatus(), 500)
        self.assert_(response.consumeBody() in [
            "<html><head><title>&lt;type 'exceptions.ValueError'&gt;</title></head>\n"
            "<body><h2>&lt;type 'exceptions.ValueError'&gt;</h2>\n"
            "A server error occurred.\n"
            "</body></html>\n",
            "<html><head><title>ValueError</title></head>\n"
            "<body><h2>ValueError</h2>\n"
            "A server error occurred.\n"
            "</body></html>\n"
        ])
Example #3
0
    def test_handleException(self):
        response = HTTPResponse()
        try:
            raise ValueError(1)
        except:
            exc_info = sys.exc_info()

        response.handleException(exc_info)
        self.assertEqual(response.getHeader("content-type"),
            "text/html;charset=utf-8")
        self.assertEqual(response.getStatus(), 500)
        self.assertTrue(response.consumeBody() in
             [b"<html><head>"
              b"<title>&lt;type 'exceptions.ValueError'&gt;</title></head>\n"
              b"<body><h2>&lt;type 'exceptions.ValueError'&gt;</h2>\n"
              b"A server error occurred.\n"
              b"</body></html>\n",
              b"<html><head><title>ValueError</title></head>\n"
              b"<body><h2>ValueError</h2>\n"
              b"A server error occurred.\n"
              b"</body></html>\n"]
             )
Example #4
0
 def _createResponse(self):
     response = HTTPResponse()
     return response
Example #5
0
 def testInterface(self):
     rp = HTTPResponse()
     verifyObject(IHTTPResponse, rp)
     verifyObject(IHTTPApplicationResponse, rp)
     verifyObject(IResponse, rp)
    def testContentType(self):
        eq = self.assertEqual

        headers, body = self._getResultFromResponse(b"test", "utf-8")
        eq("", headers.get("Content-Type", ""))
        eq(b"test", body)

        headers, body = self._getResultFromResponse(
            u"test", headers={"content-type": "text/plain"})
        eq("text/plain;charset=utf-8", headers["Content-Type"])
        eq(b"test", body)

        headers, body = self._getResultFromResponse(
            u"test", "utf-8", {"content-type": "text/html"})
        eq("text/html;charset=utf-8", headers["Content-Type"])
        eq(b"test", body)

        headers, body = self._getResultFromResponse(
            u"test", "utf-8", {"content-type": "text/plain;charset=cp1251"})
        eq("text/plain;charset=cp1251", headers["Content-Type"])
        eq(b"test", body)

        # see https://bugs.launchpad.net/zope.publisher/+bug/98395
        # RFC 3023 types and */*+xml output as unicode

        headers, body = self._getResultFromResponse(
            u"test", "utf-8", {"content-type": "text/xml"})
        eq("text/xml;charset=utf-8", headers["Content-Type"])
        eq(b"test", body)

        headers, body = self._getResultFromResponse(
            u"test", "utf-8", {"content-type": "application/xml"})
        eq("application/xml;charset=utf-8", headers["Content-Type"])
        eq(b"test", body)

        headers, body = self._getResultFromResponse(
            u"test", "utf-8",
            {"content-type": "text/xml-external-parsed-entity"})
        eq("text/xml-external-parsed-entity;charset=utf-8",
           headers["Content-Type"])
        eq(b"test", body)

        headers, body = self._getResultFromResponse(
            u"test", "utf-8",
            {"content-type": "application/xml-external-parsed-entity"})
        eq("application/xml-external-parsed-entity;charset=utf-8",
           headers["Content-Type"])
        eq(b"test", body)

        # Mozilla XUL
        headers, body = self._getResultFromResponse(
            u"test", "utf-8", {"content-type": "application/vnd+xml"})
        eq("application/vnd+xml;charset=utf-8", headers["Content-Type"])
        eq(b"test", body)

        # end RFC 3023 / xml as unicode

        headers, body = self._getResultFromResponse(
            b"test", "utf-8", {"content-type": "image/gif"})
        eq("image/gif", headers["Content-Type"])
        eq(b"test", body)

        headers, body = self._getResultFromResponse(
            u"test", "utf-8", {"content-type": "application/json"})
        eq("application/json", headers["Content-Type"])
        eq(b"test", body)

        # zope.app.exception.browser.unauthorized in combination with
        # zope.pluggableauth.plugins.session.SessionCredentialsPlugin (which
        # redirects to login form on 401/403) produces a None body
        response = HTTPResponse()
        response.setResult(None)
        eq(b"", response.consumeBody())

        response = HTTPResponse()
        response.setResult(b'')
        eq(b"", response.consumeBody())

        response = HTTPResponse()
        self.assertRaises(ValueError, response.setResult, u'')

        response = HTTPResponse()
        response.setHeader('Content-Type', 'text/plain')
        response.setResult(u'')
        eq(b"", response.consumeBody())