Пример #1
0
 def testServiceAtLeastOnceForCryingOutLoud(self):
     task = DUMMY_TASK()
     task.service()
     expected = [
         "HTTP/1.0 403 Forbidden", "content-length: 48",
         "content-type: text/plain", "server: stub server", "",
         "Request forbidden -- authorization will not help"
     ]
     expected = '\r\n'.join(expected)
     actual = task.channel.getvalue()
     self.assertEqual(expected, actual)
Пример #2
0
    def testFailInDevMode(self):
        task = DUMMY_TASK()
        task.dev_mode = True
        task.channel = StubChannel()
        try:
            raise Exception("Yarrr!")
        except:
            task.fail()

        # Traceback and content length depend on incidental circumstances.
        expected = [
            "HTTP/1.0 500 Internal Server Error"
            # "Content-Length: x"
            ,
            "Content-Type: text/plain",
            "",
            "Internal Server Error",
            "",
            "Traceback (most recent call last):"
            # ...
            ,
            'Exception: Yarrr!'
        ]
        actual = task.channel.getvalue().splitlines()
        actual = actual[:1] + actual[2:7] + actual[-1:]
        self.assertEqual(expected, actual)
Пример #3
0
    def testFailInDepMode(self):
        task = DUMMY_TASK()
        task.dev_mode = False
        task.channel = StubChannel()
        try:
            raise Exception("Yarrr!")
        except:
            task.fail()

        expected = [
            "HTTP/1.0 500 Internal Server Error", "Content-Length: 25",
            "Content-Type: text/plain", "", "Internal Server Error", ""
        ]
        actual = task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)
Пример #4
0
 def setUp(self):
     TestCaseHttpy.setUp(self)
     self.task = DUMMY_TASK()
     self.task.server.deploy_mode = True
Пример #5
0
class TestCase(TestCaseHttpy):
    def setUp(self):
        TestCaseHttpy.setUp(self)
        self.task = DUMMY_TASK()
        self.task.server.deploy_mode = True

    # Demonstration
    # =============

    def testBasic(self):
        response = Response(200)
        response.headers['content-type'] = 'text/plain'
        response.body = "Greetings, program!"
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 200 OK', 'content-length: 19',
            'content-type: text/plain', 'server: stub server', '',
            'Greetings, program!'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    # Response Line
    # =============

    def testBadResponseCode(self):
        response = Response(600)
        self.assertRaises(StandardError, self.task.respond, response)

    def testStatusLineGetsWritten(self):
        response = Response(505)
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 505 HTTP Version not supported', 'content-length: 23',
            'content-type: text/plain', 'server: stub server', '',
            'Cannot fulfill request.'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testButReasonMessageCanBeOverriden(self):
        response = Response(505)
        response.body = "Just leave me alone, ok!"
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 505 HTTP Version not supported', 'content-length: 24',
            'content-type: text/plain', 'server: stub server', '',
            'Just leave me alone, ok!'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    # Headers
    # =======

    def testHeadersAreOptional(self):
        response = Response(200)
        response.headers = {}  # the default
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 200 OK', 'content-length: 0',
            'content-type: application/octet-stream', 'server: stub server', ''
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testYouCanAddArbitraryHeaders_TheyWillBeLowerCasedButWillMakeIt(self):
        response = Response(200)
        response.headers['cheese'] = 'yummy'
        response.headers['FOO'] = 'Bar'
        response.body = "Greetings, program!"
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 200 OK', 'cheese: yummy', 'content-length: 19',
            'foo: Bar', 'content-type: application/octet-stream',
            'server: stub server', '', 'Greetings, program!'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testYouCanOverrideServerAndContentType(self):
        response = Response(200)
        response.headers['server'] = 'MY SERVER! MINE!'
        response.headers['content-type'] = 'cheese/yummy'
        response.body = "Greetings, program!"
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 200 OK', 'content-length: 19',
            'content-type: cheese/yummy', 'server: MY SERVER! MINE!', '',
            'Greetings, program!'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testButYouCantOverrideContentLength(self):
        response = Response(200)
        response.headers['content-length'] = 50000
        response.body = "Greetings, program!"
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 200 OK', 'content-length: 19',
            'content-type: application/octet-stream', 'server: stub server',
            '', 'Greetings, program!'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testContentTypeDefaultsToApplicationOctetStream(self):
        response = Response(200)
        response.body = "Greetings, program!"
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 200 OK', 'content-length: 19',
            'content-type: application/octet-stream', 'server: stub server',
            '', 'Greetings, program!'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testExceptForNonSuccessfulRequests_InWhichCaseItIsTextPlain(self):
        response = Response(301)
        response.headers['location'] = 'http://www.google.com/'
        response.body = "Greetings, program!"
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 301 Moved Permanently', 'content-length: 19',
            'content-type: text/plain', 'location: http://www.google.com/',
            'server: stub server', '', 'Greetings, program!'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    # body
    # ====

    def testBodyNotWrittenFor304(self):
        response = Response(304)
        response.body = "Greetings, program!"
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 304 Not modified', 'content-length: 19',
            'content-type: text/plain', 'server: stub server', ''
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testBodyNotWrittenForHEADRequest(self):
        self.task.request = ZopeRequest()
        self.task.request.received("HEAD / HTTP/1.1\r\n\r\n")

        response = Response(200)
        response.body = "Greetings, program!"
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 200 OK', 'content-length: 19',
            'content-type: application/octet-stream', 'server: stub server', ''
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testBodyIsFormattedFor537(self):
        response = Response(537)
        response.body = ('#' * 20, ['#' * 70])
        self.task.server.deploy_mode = False
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 537 HTTPY App Dev', 'content-length: 101',
            'content-type: text/plain', 'server: stub server', '',
            u"('####################',",
            u" ['######################################################################'])"
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testButOnlyForNonStrings(self):
        response = Response(537)
        response.body = 'foo'
        self.task.server.deploy_mode = False
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 537 HTTPY App Dev', 'content-length: 3',
            'content-type: text/plain', 'server: stub server', '', 'foo'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testAndEmptyValuesArePreserved(self):
        response = Response(537)
        response.body = {}
        self.task.server.deploy_mode = False
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 537 HTTPY App Dev', 'content-length: 2',
            'content-type: text/plain', 'server: stub server', '', '{}'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testJustLikeFor200(self):
        response = Response(200)
        response.headers['content-type'] = 'text/plain'
        response.body = ''
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 200 OK', 'content-length: 0', 'content-type: text/plain',
            'server: stub server', ''
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testIncludingNoneForExample(self):
        response = Response(537)
        response.body = None
        self.task.server.deploy_mode = False
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 537 HTTPY App Dev', 'content-length: 4',
            'content-type: text/plain', 'server: stub server', '', 'None'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    def testAnd537NotAvailableInDeploymentMode(self):
        response = Response(537)
        response.body = ('#' * 20, ['#' * 70])
        self.task.server.deploy_mode = True
        self.assertRaises(StandardError, self.task.respond, response)

    def testOtherwiseBodyIsWritten(self):
        response = Response(200)
        response.body = "Greetings, program!"
        self.task.deliver(response)

        expected = [
            'HTTP/1.0 200 OK', 'content-length: 19',
            'content-type: application/octet-stream', 'server: stub server',
            '', 'Greetings, program!'
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)
Пример #6
0
 def setUp(self):
     TestCaseHttpy.setUp(self)
     self.task = DUMMY_TASK()
     self.task.dev_mode = True
Пример #7
0
class TestCase(TestCaseHttpy):
    def setUp(self):
        TestCaseHttpy.setUp(self)
        self.task = DUMMY_TASK()
        self.task.server.deploy_mode = True

    # fail
    # ====

    def testFailInNonDeploymentMode(self):
        self.task.server.deploy_mode = False
        try:
            raise StandardError("Yarrr!")
        except:
            self.task.fail()

        # Traceback and content length depend on incidental circumstances.
        expected = [
            "HTTP/1.0 500 Internal Server Error"
            # "Content-Length: x"
            ,
            "Content-Type: text/plain",
            "",
            "Internal Server Error",
            "",
            "Traceback (most recent call last):"
            # ...
            ,
            'StandardError: Yarrr!'
        ]
        actual = self.task.channel.getvalue().splitlines()
        actual = actual[:1] + actual[2:7] + actual[-1:]
        self.assertEqual(expected, actual)

    def testFailInDepMode(self):
        try:
            raise StandardError("Yarrr!")
        except:
            self.task.fail()

        expected = [
            "HTTP/1.0 500 Internal Server Error", "Content-Length: 21",
            "Content-Type: text/plain", "", "Internal Server Error"
        ]
        actual = self.task.channel.getvalue().splitlines()
        self.assertEqual(expected, actual)

    # service
    # =======

    def testServiceAtLeastOnceForCryingOutLoud(self):
        self.task.service()
        expected = [
            "HTTP/1.0 403 Forbidden", "content-length: 48",
            "content-type: text/plain", "server: stub server", "",
            "Request forbidden -- authorization will not help"
        ]
        expected = '\r\n'.join(expected)
        actual = self.task.channel.getvalue()
        self.assertEqual(expected, actual)

    def testExceptionInDeliverStillProducesResult(self):
        def bad_deliver(self, request):
            raise Exception("MUAHAHAHAHAHHAHA!!!!!!!!!!!!!!!")

        self.task.deliver = bad_deliver
        self.task.service()

        expected = [
            "HTTP/1.0 500 Internal Server Error", "Content-Length: 21",
            "Content-Type: text/plain", "", "Internal Server Error"
        ]
        expected = '\r\n'.join(expected)
        actual = self.task.channel.getvalue()
        self.assertEqual(expected, actual)