def test_closeSession_another_connection(self):
        # When server is closing session, it should unlink current
        # request. That means, if a new request appears, it should
        # receive an application close message rather than "Another
        # connection still open" message.
        url = close_base_url + '/000/' + str(uuid.uuid4())
        r1 = POST_async(url + '/xhr_streaming')

        r1.read() # prelude
        self.assertEqual(r1.read(), 'o\n')
        self.assertEqual(r1.read(), 'c[3000,"Go away!"]\n')

        r2 = POST_async(url + '/xhr_streaming')
        r2.read() # prelude
        self.assertEqual(r2.read(), 'c[3000,"Go away!"]\n')

        ''' TODO: should request be automatically closed after close?
    def test_simpleSession(self):
        trans_url = base_url + '/000/' + str(uuid.uuid4())
        r = POST(trans_url + '/xhr')
        "New line is a frame delimiter specific for xhr-polling"
        self.assertEqual(r.body, 'o\n')
        self.assertEqual(r.status, 200)

        # After a session was established the server needs to accept
        # requests for sending messages.
        "Xhr-polling accepts messages as a list of JSON-encoded strings."
        payload = '["a"]'
        r = POST(trans_url + '/xhr_send', body=payload)
        self.assertFalse(r.body)
        self.assertEqual(r.status, 204)

        '''We're using an echo service - we'll receive our message
        back. The message is encoded as an array 'a'.'''
        r = POST(trans_url + '/xhr')
        self.assertEqual(r.body, 'a["a"]\n')
        self.assertEqual(r.status, 200)

        # Sending messages to not existing sessions is invalid.
        payload = '["a"]'
        r = POST(base_url + '/000/bad_session/xhr_send', body=payload)
        self.verify404(r, cookie=True)

        # The session must time out after 5 seconds of not having a
        # receiving connection. The server must send a heartbeat frame
        # every 25 seconds. The heartbeat frame contains a single `h`
        # character. This delays may be configurable.
        pass
        # The server must not allow two receiving connections to wait
        # on a single session. In such case the server must send a
        # close frame to the new connection.
        r1 = POST_async(trans_url + '/xhr', load=False)
        r2 = POST(trans_url + '/xhr')
        r1.close()
        self.assertEqual(r2.body, 'c[2010,"Another connection still open"]\n')
        self.assertEqual(r2.status, 200)
    def test_basic(self):
        t0 = time.time()
        r = POST_async(base_url + '/chunking_test')
        self.assertEqual(r.status, 200)
        self.assertEqual(r['content-type'],
                         'application/javascript; charset=UTF-8')
        self.verify_no_cookie(r)
        self.verify_cors(r)

        # In first chunk the server must send a 'h' frame:
        self.assertEqual(r.read(), 'h\n')
        # As second chunk the server must send 2KiB prelude.
        self.assertEqual(r.read(), ' ' * 2048 + 'h\n')
        # Later the server must send a `h` byte.
        self.assertEqual(r.read(), 'h\n')
        # In third chunk the server must send a `h` byte.
        self.assertEqual(r.read(), 'h\n')

        # At least 30 ms must have passed since the request.
        t1 = time.time()
        self.assertGreater((t1-t0) * 1000., 30.)
        r.close()
    def test_transport(self):
        url = base_url + '/000/' + str(uuid.uuid4())
        r = POST_async(url + '/xhr_streaming')
        self.assertEqual(r.status, 200)
        self.assertEqual(r['Content-Type'],
                         'application/javascript; charset=UTF-8')
        self.verify_cookie(r)
        self.verify_cors(r)

        # The transport must first send 2KiB of `h` bytes as prelude.
        self.assertEqual(r.read(), 'h' *  2048 + '\n')

        self.assertEqual(r.read(), 'o\n')

        r1 = POST(url + '/xhr_send', body='["x"]')
        self.assertFalse(r1.body)
        self.assertEqual(r1.status, 204)

        self.assertEqual(r.read(), 'a["x"]\n')
        r.close()