def test_PageKiteRequest(self):
    request = ['CONNECT PageKite:1 HTTP/1.0\r\n'] 
    zlibreq = 'X-PageKite-Features: ZChunks\r\n'
    reqbody = '\r\n'

    # Basic request, no servers.
    req = request[:]
    req.extend([zlibreq, reqbody])
    self.assertEqual(pagekite.HTTP_PageKiteRequest('x', {}),
                     ''.join(req))

    # Basic request, no servers, zchunks disabled.
    req = request[:]
    req.append(reqbody)
    self.assertEqual(pagekite.HTTP_PageKiteRequest('x', {}, nozchunks=True),
                     ''.join(req))
    
    # Full request, single server.
    bid = 'http:a'
    token = '0123456789'
    backends = {bid: ['a', 'b', 'c', 'd']}
    backends[bid][pagekite.BE_SECRET] = 'Secret'
    data = '%s:%s:%s' % (bid, pagekite.signToken(token=self.gSecret,
                                                 payload=self.gSecret,
                                                 secret='x'), token) 
    sign = pagekite.signToken(secret='Secret', payload=data, token=token)
    req = request[:]
    req.extend([zlibreq, 'X-PageKite: %s:%s\r\n' % (data, sign), reqbody])
    self.assertEqual(pagekite.HTTP_PageKiteRequest('x', backends,
                                                        tokens={bid: token},
                                                        testtoken=token),
                     ''.join(req))
    def test_PageKiteRequest(self):
        request = ['CONNECT PageKite:1 HTTP/1.0\r\n']
        zlibreq = 'X-PageKite-Features: ZChunks\r\n'
        reqbody = '\r\n'

        # Basic request, no servers.
        req = request[:]
        req.extend([zlibreq, reqbody])
        self.assertEqual(pagekite.HTTP_PageKiteRequest('x', {}), ''.join(req))

        # Basic request, no servers, zchunks disabled.
        req = request[:]
        req.append(reqbody)
        self.assertEqual(
            pagekite.HTTP_PageKiteRequest('x', {}, nozchunks=True),
            ''.join(req))

        # Full request, single server.
        bid = 'http:a'
        token = '0123456789'
        backends = {bid: ['a', 'b', 'c', 'd']}
        backends[bid][pagekite.BE_SECRET] = 'Secret'
        data = '%s:%s:%s' % (bid,
                             pagekite.signToken(token=self.gSecret,
                                                payload=self.gSecret,
                                                secret='x'), token)
        sign = pagekite.signToken(secret='Secret', payload=data, token=token)
        req = request[:]
        req.extend([zlibreq, 'X-PageKite: %s:%s\r\n' % (data, sign), reqbody])
        self.assertEqual(
            pagekite.HTTP_PageKiteRequest('x',
                                          backends,
                                          tokens={bid: token},
                                          testtoken=token), ''.join(req))
  def test_signToken(self):
    # Basic signature
    self.assertEqual(pagekite.signToken(token='1234567812',
                                              secret='Bjarni',
                                              payload='Bjarni'),
                     '1234567843b16458418175599012be884a18')

    # Make sure it varies based on all variables
    self.assertNotEqual(pagekite.signToken(token='1234567812345689',
                                                 secret='BjarniTheDude',
                                                 payload='Bjarni'),
                     '1234567843b16458418175599012be884a18')
    self.assertNotEqual(pagekite.signToken(token='234567812345689',
                                                 secret='Bjarni',
                                                 payload='Bjarni'),
                     '1234567843b16458418175599012be884a18')
    self.assertNotEqual(pagekite.signToken(token='1234567812345689',
                                                 secret='Bjarni',
                                                 payload='BjarniTheDude'),
                     '1234567843b16458418175599012be884a18')

    # Test non-standard signature lengths
    self.assertEqual(pagekite.signToken(token='1234567812',
                                              secret='Bjarni',
                                              payload='Bjarni',
                                              length=1000),
                     '1234567843b16458418175599012be884a18963f10be4670')
    def test_signToken(self):
        # Basic signature
        self.assertEqual(
            pagekite.signToken(token='1234567812',
                               secret='Bjarni',
                               payload='Bjarni'),
            '1234567843b16458418175599012be884a18')

        # Make sure it varies based on all variables
        self.assertNotEqual(
            pagekite.signToken(token='1234567812345689',
                               secret='BjarniTheDude',
                               payload='Bjarni'),
            '1234567843b16458418175599012be884a18')
        self.assertNotEqual(
            pagekite.signToken(token='234567812345689',
                               secret='Bjarni',
                               payload='Bjarni'),
            '1234567843b16458418175599012be884a18')
        self.assertNotEqual(
            pagekite.signToken(token='1234567812345689',
                               secret='Bjarni',
                               payload='BjarniTheDude'),
            '1234567843b16458418175599012be884a18')

        # Test non-standard signature lengths
        self.assertEqual(
            pagekite.signToken(token='1234567812',
                               secret='Bjarni',
                               payload='Bjarni',
                               length=1000),
            '1234567843b16458418175599012be884a18963f10be4670')