Ejemplo n.º 1
0
    def testNonBlockingRequestStreamFancyJson(self):
        """
        Test NonBlocking Http client
        """
        console.terse("{0}\n".format(
            self.testNonBlockingRequestStreamFancyJson.__doc__))

        console.reinit(verbosity=console.Wordage.profuse)

        wireLogBeta = nonblocking.WireLog(buffify=True)
        result = wireLogBeta.reopen()

        eha = ('127.0.0.1', 8080)
        beta = nonblocking.Outgoer(ha=eha, bufsize=131072, wlog=wireLogBeta)
        self.assertIs(beta.reopen(), True)
        self.assertIs(beta.accepted, False)
        self.assertIs(beta.cutoff, False)

        console.terse("Connecting beta to server ...\n")
        while True:
            beta.serviceConnect()
            #alpha.serviceAccepts()
            if beta.accepted:  # and beta.ca in alpha.ixes
                break
            time.sleep(0.05)

        self.assertIs(beta.accepted, True)
        self.assertIs(beta.cutoff, False)
        self.assertEqual(beta.ca, beta.cs.getsockname())
        self.assertEqual(beta.ha, beta.cs.getpeername())
        self.assertEqual(eha, beta.ha)

        console.terse("{0}\n".format("Building Request ...\n"))
        host = u'127.0.0.1'
        port = 8080
        method = u'GET'
        path = u'/fancy?idify=true;jsonify=true'
        console.terse("{0} from  {1}:{2}{3} ...\n".format(
            method, host, port, path))
        headers = odict([('Accept', 'application/json')])
        request = httping.Requester(hostname=host,
                                    port=port,
                                    method=method,
                                    path=path,
                                    headers=headers)
        msgOut = request.rebuild()
        lines = [
            b'GET /fancy?idify=true;jsonify=true HTTP/1.1',
            b'Host: 127.0.0.1:8080',
            b'Accept-Encoding: identity',
            b'Content-Length: 0',
            b'Accept: application/json',
            b'',
            b'',
        ]
        for i, line in enumerate(lines):
            self.assertEqual(line, request.lines[i])

        self.assertEqual(
            request.head,
            b'GET /fancy?idify=true;jsonify=true HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nAccept-Encoding: identity\r\nContent-Length: 0\r\nAccept: application/json\r\n\r\n'
        )
        self.assertEqual(msgOut, request.head)

        beta.tx(msgOut)
        while beta.txes or not beta.rxbs:
            beta.serviceTxes()
            beta.serviceReceives()
            time.sleep(0.05)
        beta.serviceReceives()

        msgIn, index = beta.tailRxbs(0)

        response = httping.Respondent(msg=beta.rxbs,
                                      method=method,
                                      path=path,
                                      reconnectable=True)

        timer = Timer(duration=3.0)
        while response.parser and not timer.expired:
            response.parse()
            beta.serviceReceives()
            time.sleep(0.01)

        if response.parser:
            response.parser.close()
            response.parser = None

        response.dictify()

        self.assertEqual(response.eventSource.retry, 1000)
        self.assertTrue(len(response.events) > 2)
        event = response.events.popleft()
        self.assertEqual(event, {
            'id': '0',
            'name': '',
            'data': 'START',
            'json': None
        })
        event = response.events.popleft()
        self.assertEqual(event, {
            'id': '1',
            'name': '',
            'data': None,
            'json': {
                'count': 1
            }
        })
        event = response.events.popleft()
        self.assertEqual(event, {
            'id': '2',
            'name': '',
            'data': None,
            'json': {
                'count': 2
            }
        })
        self.assertTrue(len(response.body) == 0)
        self.assertTrue(len(response.eventSource.raw) == 0)

        beta.close()

        wireLogBeta.close()
        console.reinit(verbosity=console.Wordage.concise)
Ejemplo n.º 2
0
    def testNonBlockingRequestMultipart(self):
        """
        Test NonBlocking Http client
        """
        console.terse("{0}\n".format(
            self.testNonBlockingRequestMultipart.__doc__))

        console.reinit(verbosity=console.Wordage.profuse)

        wireLogBeta = nonblocking.WireLog(buffify=True)
        result = wireLogBeta.reopen()

        eha = ('127.0.0.1', 8080)
        beta = nonblocking.Outgoer(ha=eha, bufsize=131072)
        self.assertIs(beta.reopen(), True)
        self.assertIs(beta.accepted, False)
        self.assertIs(beta.cutoff, False)

        console.terse("Connecting beta to server ...\n")
        while True:
            beta.serviceConnect()
            #alpha.serviceAccepts()
            if beta.accepted:  # and beta.ca in alpha.ixes
                break
            time.sleep(0.05)

        self.assertIs(beta.accepted, True)
        self.assertIs(beta.cutoff, False)
        self.assertEqual(beta.ca, beta.cs.getsockname())
        self.assertEqual(beta.ha, beta.cs.getpeername())
        self.assertEqual(eha, beta.ha)

        console.terse("{0}\n".format("Building Request ...\n"))
        host = u'127.0.0.1'
        port = 8080
        method = u'POST'
        path = u'/echo'
        console.terse("{0} from  {1}:{2}{3} ...\n".format(
            method, host, port, path))
        headers = odict([(u'Accept', u'application/json'),
                         (u'Content-Type', u'multipart/form-data')])
        fargs = odict([("text", "This is the life,\nIt is the best.\n"),
                       ("html", "<html><body></body><html>")])
        request = httping.Requester(hostname=host,
                                    port=port,
                                    method=method,
                                    path=path,
                                    headers=headers)

        msgOut = request.rebuild(fargs=fargs)

        beta.tx(msgOut)
        while beta.txes or not beta.rxbs:
            beta.serviceTxes()
            beta.serviceReceives()
            time.sleep(0.05)
        beta.serviceReceives()

        msgIn, index = beta.tailRxbs(0)

        response = httping.Respondent(msg=beta.rxbs, method=method)

        while response.parser:
            response.parse()

        response.dictify()

        self.assertEqual(
            response.data, {
                'action':
                None,
                'content':
                None,
                'form': [['text', 'This is the life,\nIt is the best.\n'],
                         ['html', '<html><body></body><html>']],
                'query': {},
                'url':
                'http://127.0.0.1:8080/echo',
                'verb':
                'POST'
            })

        self.assertEqual(len(beta.rxbs), 0)

        #alpha.close()
        beta.close()

        wireLogBeta.close()
        console.reinit(verbosity=console.Wordage.concise)
Ejemplo n.º 3
0
    def testNonBlockingRequestEcho(self):
        """
        Test NonBlocking Http client
        """
        console.terse("{0}\n".format(self.testNonBlockingRequestEcho.__doc__))

        console.reinit(verbosity=console.Wordage.profuse)

        wireLogBeta = nonblocking.WireLog(buffify=True)
        result = wireLogBeta.reopen()

        eha = ('127.0.0.1', 8080)
        beta = nonblocking.Outgoer(ha=eha, bufsize=131072)
        self.assertIs(beta.reopen(), True)
        self.assertIs(beta.accepted, False)
        self.assertIs(beta.cutoff, False)

        console.terse("Connecting beta to server ...\n")
        while True:
            beta.serviceConnect()
            #alpha.serviceAccepts()
            if beta.accepted:  # and beta.ca in alpha.ixes
                break
            time.sleep(0.05)

        self.assertIs(beta.accepted, True)
        self.assertIs(beta.cutoff, False)
        self.assertEqual(beta.ca, beta.cs.getsockname())
        self.assertEqual(beta.ha, beta.cs.getpeername())
        self.assertEqual(eha, beta.ha)

        console.terse("{0}\n".format("Building Request ...\n"))
        host = u'127.0.0.1'
        port = 8080
        method = u'GET'
        path = u'/echo?name=fame'
        console.terse("{0} from  {1}:{2}{3} ...\n".format(
            method, host, port, path))
        headers = odict([('Accept', 'application/json')])
        request = httping.Requester(hostname=host,
                                    port=port,
                                    method=method,
                                    path=path,
                                    headers=headers)
        msgOut = request.rebuild()
        lines = [
            b'GET /echo?name=fame HTTP/1.1',
            b'Host: 127.0.0.1:8080',
            b'Accept-Encoding: identity',
            b'Content-Length: 0',
            b'Accept: application/json',
            b'',
            b'',
        ]
        for i, line in enumerate(lines):
            self.assertEqual(line, request.lines[i])

        self.assertEqual(
            request.head,
            b'GET /echo?name=fame HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nAccept-Encoding: identity\r\nContent-Length: 0\r\nAccept: application/json\r\n\r\n'
        )
        self.assertEqual(
            msgOut,
            b'GET /echo?name=fame HTTP/1.1\r\nHost: 127.0.0.1:8080\r\nAccept-Encoding: identity\r\nContent-Length: 0\r\nAccept: application/json\r\n\r\n'
        )

        beta.tx(msgOut)
        while beta.txes or not beta.rxbs:
            beta.serviceTxes()
            beta.serviceReceives()
            time.sleep(0.05)
        beta.serviceReceives()

        msgIn, index = beta.tailRxbs(0)
        self.assertTrue(
            msgIn.endswith(
                b'{"content": null, "query": {"name": "fame"}, "verb": "GET", "path": "http://127.0.0.1:8080/echo?name=fame", "action": null}'
            ))

        #response = httping.HttpResponseNb(msgIn, method=method, path=path)
        response = httping.Respondent(msg=beta.rxbs, method=method, path=path)

        while response.parser:
            response.parse()

        response.dictify()

        self.assertEqual(
            bytes(response.body),
            b'{"content": null, "query": {"name": "fame"}, "verb": "GET", "url": "http://127.0.0.1:8080/echo?name=fame", "action": null}'
        )

        self.assertEqual(len(beta.rxbs), 0)

        #alpha.close()
        beta.close()

        wireLogBeta.close()
        console.reinit(verbosity=console.Wordage.concise)