def test_transport(self):
        url = base_url + '/000/' + str(uuid.uuid4())
        r = GET(url + '/jsonp?c=%63allback')
        self.assertEqual(r.status, 200)
        self.assertEqual(r['Content-Type'],
                         'application/javascript; charset=UTF-8')
        # As JsonPolling is requested using GET we must be very
        # carefull not to allow it being cached.
        self.verify_not_cached(r)
        self.verify_cookie(r)

        self.assertEqual(r.body, 'callback("o");\r\n')

        r = POST(url + '/jsonp_send', body='d=%5B%22x%22%5D',
                 headers={'Content-Type': 'application/x-www-form-urlencoded'})
        # Konqueror does weird things on 204. As a workaround we need
        # to respond with something - let it be the string `ok`.
        self.assertEqual(r.body, 'ok')
        self.assertEqual(r.status, 200)
        self.assertFalse(r['Content-Type'])
        self.verify_cookie(r)

        r = GET(url + '/jsonp?c=%63allback')
        self.assertEqual(r.status, 200)
        self.assertEqual(r.body, 'callback("a[\\"x\\"]");\r\n')
    def test_cacheability(self):
        r1 = GET(base_url + '/iframe.html')
        r2 = GET(base_url + '/iframe.html')
        self.assertEqual(r1['etag'], r2['etag'])
        self.assertTrue(r1['etag']) # Let's make sure ETag isn't None.

        r = GET(base_url + '/iframe.html', headers={'If-None-Match': r1['etag']})
        self.assertEqual(r.status, 304)
        self.assertFalse(r['content-type'])
        self.assertFalse(r.body)
    def test_content_types(self):
        url = base_url + '/000/' + str(uuid.uuid4())
        r = GET(url + '/jsonp?c=x')
        self.assertEqual(r.body, 'x("o");\r\n')

        r = POST(url + '/jsonp_send', body='d=%5B%22abc%22%5D',
                 headers={'Content-Type': 'application/x-www-form-urlencoded'})
        self.assertEqual(r.body, 'ok')
        r = POST(url + '/jsonp_send', body='["%61bc"]',
                 headers={'Content-Type': 'text/plain'})
        self.assertEqual(r.body, 'ok')

        r = GET(url + '/jsonp?c=x')
        self.assertEqual(r.status, 200)
        self.assertEqual(r.body, 'x("a[\\"abc\\",\\"%61bc\\"]");\r\n')
 def test_greeting(self):
     for url in [base_url, base_url + '/']:
         r = GET(url)
         self.assertEqual(r.status, 200)
         self.assertEqual(r['content-type'], 'text/plain; charset=UTF-8')
         self.assertEqual(r.body, 'Welcome to SockJS!\n')
         self.verify_no_cookie(r)
    def test_invalid_json(self):
        url = base_url + '/000/' + str(uuid.uuid4())
        r = GET(url + '/jsonp?c=x')
        self.assertEqual(r.body, 'x("o");\r\n')

        r = POST(url + '/jsonp_send', body='d=%5B%22x',
                 headers={'Content-Type': 'application/x-www-form-urlencoded'})
        self.assertEqual(r.body.strip(), "Broken JSON encoding.")
        self.assertEqual(r.status, 500)

        for data in ['', 'd=', 'p=p']:
            r = POST(url + '/jsonp_send', body=data,
                     headers={'Content-Type': 'application/x-www-form-urlencoded'})
            self.assertEqual(r.body.strip(), "Payload expected.")
            self.assertEqual(r.status, 500)

        r = POST(url + '/jsonp_send', body='d=%5B%22b%22%5D',
                 headers={'Content-Type': 'application/x-www-form-urlencoded'})
        self.assertEqual(r.body, 'ok')

        r = GET(url + '/jsonp?c=x')
        self.assertEqual(r.status, 200)
        self.assertEqual(r.body, 'x("a[\\"b\\"]");\r\n')
    def verify(self, url):
        r = GET(url)
        self.assertEqual(r.status, 200)
        self.assertEqual(r['content-type'], 'text/html; charset=UTF-8')
        # The iframe page must be strongly cacheable, supply
        # Cache-Control, Expires and Etag headers and avoid
        # Last-Modified header.
        self.assertTrue(re.search('public', r['Cache-Control']))
        self.assertTrue(re.search('max-age=[1-9][0-9]{6}', r['Cache-Control']),
                        "max-age must be large, one year (31536000) is best")
        self.assertTrue(r['Expires'])
        self.assertTrue(r['ETag'])
        self.assertFalse(r['last-modified'])

        # Body must be exactly as specified, with the exception of
        # `sockjs_url`, which should be configurable.
        match = self.iframe_body.match(r.body.strip())
        self.assertTrue(match)
        # `Sockjs_url` must be a valid url and should utilize caching.
        sockjs_url = match.group('sockjs_url')
        self.assertTrue(sockjs_url.startswith('/') or
                        sockjs_url.startswith('http'))
        self.verify_no_cookie(r)
        return r
 def test_no_callback(self):
     r = GET(base_url + '/a/a/jsonp')
     self.assertEqual(r.status, 500)
     self.assertEqual(r.body.strip(), '"callback" parameter required')
 def test_invalidConnectionHeader(self):
     r = GET(base_url + '/0/0/websocket', headers={'Upgrade': 'WebSocket',
                                                   'Connection': 'close'})
     self.assertEqual(r.status, 400)
     self.assertEqual(r.body, '"Connection" must be "Upgrade".')
 def test_httpMethod(self):
     r = GET(base_url + '/0/0/websocket')
     self.assertEqual(r.status, 400)
     self.assertEqual(r.body, 'Can "Upgrade" only to "WebSocket".')
Esempio n. 10
0
 def test_disabledTransport(self):
     r = GET(wsoff_base_url + '/0/0/websocket')
     self.verify404(r)
     if r.body:
         self.assertLess(len(r.body), 1025)
Esempio n. 11
0
 def test_invalidPaths(self):
     for suffix in ['//', '/a./a', '/a/a.', '/./.' ,'/', '///']:
         self.verify404(GET(base_url + suffix + '/xhr'))
         self.verify404(POST(base_url + suffix + '/xhr'))
Esempio n. 12
0
 def test_invalidUrl(self):
     for suffix in ['/iframe.htm', '/iframe', '/IFRAME.HTML', '/IFRAME',
                    '/iframe.HTML', '/iframe.xml', '/iframe-/.html']:
         r = GET(base_url + suffix)
         self.verify404(r)
Esempio n. 13
0
 def test_notFound(self):
     for suffix in ['/a', '/a.html', '//', '///', '/a/a', '/a/a/', '/a',
                    '/a/']:
         self.verify404(GET(base_url + suffix))