def test_connection_non_persistent(): """ Test WSGI service request response non persistent connection in request """ try: import bottle except ImportError as ex: logger.error("Bottle not available.\n") return tymist = tyming.Tymist(tyme=0.0) app = bottle.default_app() # create bottle app @app.get('/echo') @app.get('/echo/<action>') @app.post('/echo') @app.post('/echo/<action>') def echoGet(action=None): """ Echo back request data """ query = dict(bottle.request.query.items()) body = bottle.request.json raw = bottle.request.body.read() form = dict(bottle.request.forms) data = dict(verb=bottle.request.method, url=bottle.request.url, action=action, query=query, form=form, content=body) return data with http.openServer(port = 6101, bufsize=131072, app=app, \ tymth=tymist.tymen()) as alpha: assert alpha.servant.ha == ('0.0.0.0', 6101) assert alpha.servant.eha == ('127.0.0.1', 6101) path = "http://{0}:{1}/".format('localhost', alpha.servant.eha[1]) with http.openClient(bufsize=131072, path=path, reconnectable=True, \ tymth=tymist.tymen(),) as beta: assert not beta.connector.accepted assert not beta.connector.connected assert not beta.connector.cutoff request = dict([ ('method', u'GET'), ('path', u'/echo?name=fame'), ('qargs', dict()), ('fragment', u''), ('headers', dict([('Accept', 'application/json'), ('Connection', 'close')])), ]) beta.requests.append(request) while (beta.requests or beta.connector.txbs or not beta.responses or not alpha.idle()): alpha.service() time.sleep(0.05) beta.service() time.sleep(0.05) tymist.tick(tock=0.1) assert beta.connector.accepted assert beta.connector.connected assert not beta.connector.cutoff assert len(alpha.servant.ixes) == 1 assert len(alpha.reqs) == 1 assert len(alpha.reps) == 1 requestant = list(alpha.reqs.values())[0] assert requestant.method == request['method'] assert requestant.url == request['path'] assert requestant.headers == help.Hict([ ('Host', 'localhost:6101'), ('Accept-Encoding', 'identity'), ('Accept', 'application/json'), ('Connection', 'close') ]) assert len(beta.responses) == 1 response = beta.responses.popleft() assert response['status'] == 200 assert response['reason'] == 'OK' assert response['body'] == ( b'{"verb": "GET", "url": "http://localhost:6101/echo?name=fame", "' b'action": null, "query": {"name": "fame"}, "form": {}, "content":' b' null}') assert response['data'] == { 'action': None, 'content': None, 'form': {}, 'query': { 'name': 'fame' }, 'url': 'http://localhost:6101/echo?name=fame', 'verb': 'GET' } responder = list(alpha.reps.values())[0] assert responder.status.startswith(str(response['status'])) assert responder.headers == response['headers']
def test_sse_stream_tls(): """ Test WSGI service request response stream sse with tls """ try: import bottle except ImportError as ex: logger.error("Bottle not available.\n") return tymist = tyming.Tymist(tyme=0.0) app = bottle.default_app() # create bottle app @app.get('/stream') def streamGet(): """ Create test server sent event stream that sends count events """ tymer = tyming.Tymer(tymth=tymist.tymen(), duration=2.0) bottle.response.set_header('Content-Type', 'text/event-stream') #text bottle.response.set_header('Cache-Control', 'no-cache') # HTTP 1.1 servers detect text/event-stream and use Transfer-Encoding: chunked # Set client-side auto-reconnect timeout to 1000 ms. yield 'retry: 1000\n\n' i = 0 yield 'id: {0}\n'.format(i) i += 1 yield 'data: START\n\n' n = 1 while not tymer.expired: yield 'id: {0}\n'.format(i) i += 1 yield 'data: {0}\n\n'.format(n) n += 1 yield "data: END\n\n" serverCertCommonName = 'localhost' # match hostname uses servers's cert commonname #serverKeypath = '/etc/pki/tls/certs/server_key.pem' # local server private key #serverCertpath = '/etc/pki/tls/certs/server_cert.pem' # local server public cert #clientCafilepath = '/etc/pki/tls/certs/client.pem' # remote client public cert serverKeypath = certdirpath + '/server_key.pem' # local server private key serverCertpath = certdirpath + '/server_cert.pem' # local server public cert clientCafilepath = certdirpath + '/client.pem' # remote client public cert with http.openServer(port = 6101, bufsize=131072, scheme='https', \ keypath=serverKeypath, certpath=serverCertpath, \ cafilepath=clientCafilepath, app=app, tymth=tymist.tymen()) as alpha: assert alpha.servant.ha == ('0.0.0.0', 6101) assert alpha.servant.eha == ('127.0.0.1', 6101) #clientKeypath = '/etc/pki/tls/certs/client_key.pem' # local client private key #clientCertpath = '/etc/pki/tls/certs/client_cert.pem' # local client public cert #serverCafilepath = '/etc/pki/tls/certs/server.pem' # remote server public cert clientKeypath = certdirpath + '/client_key.pem' # local client private key clientCertpath = certdirpath + '/client_cert.pem' # local client public cert serverCafilepath = certdirpath + '/server.pem' # remote server public cert path = "https://{0}:{1}/".format('localhost', alpha.servant.eha[1]) with http.openClient(bufsize=131072, path=path, scheme='https', \ certedhost=serverCertCommonName, keypath=clientKeypath, \ certpath=clientCertpath, cafilepath=serverCafilepath, \ reconnectable=True, tymth=tymist.tymen(),) as beta: assert not beta.connector.accepted assert not beta.connector.connected assert not beta.connector.cutoff request = dict([ ('method', u'GET'), ('path', u'/stream'), ('qargs', dict()), ('fragment', u''), ('headers', dict([('Accept', 'application/json'), ('Content-Length', 0)])), ('body', None), ]) beta.requests.append(request) tymer = tyming.Tymer(tymth=tymist.tymen(), duration=1.0) while (not tymer.expired): alpha.service() time.sleep(0.05) beta.service() time.sleep(0.05) tymist.tick(tock=0.1) assert beta.connector.accepted assert beta.connector.connected assert not beta.connector.cutoff assert len(alpha.servant.ixes) == 1 assert len(alpha.reqs) == 1 assert len(alpha.reps) == 1 requestant = list(alpha.reqs.values())[0] assert requestant.method == request['method'] assert requestant.url == request['path'] assert requestant.headers == help.Hict([ ('Host', 'localhost:6101'), ('Accept-Encoding', 'identity'), ('Accept', 'application/json'), ('Content-Length', '0') ]) # timed out while body streaming still open so no completed responses # in .responses. But headers fully received assert beta.waited assert not beta.respondent.ended assert len(beta.responses) == 0 assert 'Content-Type' in beta.respondent.headers assert beta.respondent.headers[ 'Content-Type'] == 'text/event-stream' assert 'Transfer-Encoding' in beta.respondent.headers assert beta.respondent.headers['Transfer-Encoding'] == 'chunked' assert len(beta.events) == 3 assert beta.respondent.retry == 1000 assert int(beta.respondent.leid) >= 2 event = beta.events.popleft() assert event == {'id': '0', 'name': '', 'data': 'START'} event = beta.events.popleft() assert event == {'id': '1', 'name': '', 'data': '1'} event = beta.events.popleft() assert event == {'id': '2', 'name': '', 'data': '2'} assert not beta.events #keep going until ended tymer.restart(duration=1.5) while (not tymer.expired): alpha.service() time.sleep(0.05) beta.service() time.sleep(0.05) tymist.tick(tock=0.1) assert len(beta.events) == 8 assert beta.respondent.leid == '9' assert beta.events[-2] == {'id': '9', 'name': '', 'data': '9'} assert beta.events[-1] == {'id': '9', 'name': '', 'data': 'END'} beta.events.clear()
def test_server_with_bottle_tls(): """ Test WSGI service secure TLS request response """ try: import bottle except ImportError as ex: logger.error("Bottle not available.\n") return tymist = tyming.Tymist(tyme=0.0) app = bottle.default_app() # create bottle app @app.get('/echo') @app.get('/echo/<action>') @app.post('/echo') @app.post('/echo/<action>') def echoGet(action=None): """ Echo back request data """ query = dict(bottle.request.query.items()) body = bottle.request.json raw = bottle.request.body.read() form = dict(bottle.request.forms) data = dict(verb=bottle.request.method, url=bottle.request.url, action=action, query=query, form=form, content=body) return data serverCertCommonName = 'localhost' # match hostname uses servers's cert commonname #serverKeypath = '/etc/pki/tls/certs/server_key.pem' # local server private key #serverCertpath = '/etc/pki/tls/certs/server_cert.pem' # local server public cert #clientCafilepath = '/etc/pki/tls/certs/client.pem' # remote client public cert serverKeypath = certdirpath + '/server_key.pem' # local server private key serverCertpath = certdirpath + '/server_cert.pem' # local server public cert clientCafilepath = certdirpath + '/client.pem' # remote client public cert with http.openServer(port = 6101, bufsize=131072, scheme='https', \ keypath=serverKeypath, certpath=serverCertpath, \ cafilepath=clientCafilepath, app=app, tymth=tymist.tymen()) as alpha: assert alpha.servant.ha == ('0.0.0.0', 6101) assert alpha.servant.eha == ('127.0.0.1', 6101) #clientKeypath = '/etc/pki/tls/certs/client_key.pem' # local client private key #clientCertpath = '/etc/pki/tls/certs/client_cert.pem' # local client public cert #serverCafilepath = '/etc/pki/tls/certs/server.pem' # remote server public cert clientKeypath = certdirpath + '/client_key.pem' # local client private key clientCertpath = certdirpath + '/client_cert.pem' # local client public cert serverCafilepath = certdirpath + '/server.pem' # remote server public cert path = "https://{0}:{1}/".format('localhost', alpha.servant.eha[1]) with http.openClient(bufsize=131072, path=path, scheme='https', \ certedhost=serverCertCommonName, keypath=clientKeypath, \ certpath=clientCertpath, cafilepath=serverCafilepath, \ reconnectable=True, tymth=tymist.tymen(),) as beta: assert not beta.connector.accepted assert not beta.connector.connected assert not beta.connector.cutoff request = dict([ ('method', u'GET'), ('path', u'/echo?name=fame'), ('qargs', dict()), ('fragment', u''), ('headers', dict([('Accept', 'application/json'), ('Content-Length', 0)])), ]) beta.requests.append(request) while (beta.requests or beta.connector.txbs or not beta.responses or not alpha.idle()): alpha.service() time.sleep(0.05) beta.service() time.sleep(0.05) tymist.tick(tock=0.1) assert beta.connector.accepted assert beta.connector.connected assert not beta.connector.cutoff assert len(alpha.servant.ixes) == 1 assert len(alpha.reqs) == 1 assert len(alpha.reps), 1 requestant = list(alpha.reqs.values())[0] assert requestant.method == request['method'] assert requestant.url, request['path'] assert requestant.headers == help.Hict([ ('Host', 'localhost:6101'), ('Accept-Encoding', 'identity'), ('Accept', 'application/json'), ('Content-Length', '0') ]) assert len(beta.responses) == 1 response = beta.responses.popleft() assert response['status'] == 200 assert response['reason'] == 'OK' assert response['body'] == ( b'{"verb": "GET", ' b'"url": "https://localhost:6101/echo?name=fame", ' b'"action": null, ' b'"query": {"name": "fame"}, ' b'"form": {}, ' b'"content": null}') assert response['data'] == { 'action': None, 'content': None, 'form': {}, 'query': { 'name': 'fame' }, 'url': 'https://localhost:6101/echo?name=fame', 'verb': 'GET' } responder = list(alpha.reps.values())[0] assert responder.status.startswith(str(response['status'])) assert responder.headers == response['headers']
def test_get_backend(): """ Test get backend request functionality (webhook) """ tymist.tyme = 0.0 # reset for each test with http.openServer(port=8101, bufsize=131072, app=exapp, \ tymth=tymist.tymen()) as server: #server = http.Server(port=8101, #bufsize=131072, #store=store, #app=exapp,) #assert server.reopen() assert server.servant.ha == ('0.0.0.0', 8101) assert server.servant.eha == ('127.0.0.1', 8101) # request to ExampleBackendEnd at /example/backend path = "http://{}:{}{}".format('localhost', server.servant.eha[1], "/example/backend") headers = help.Hict([('Accept', 'application/json'), ('Content-Length', 0)]) with http.openClient(bufsize=131072, method='GET', path=path, \ headers=headers, reconnectable=True, \ tymth=tymist.tymen()) as client: #client = http.Client(bufsize=131072, #store=store, #method='GET', #path=path, #headers=headers, #reconnectable=True,) #assert client.reopen() assert client.connector.accepted == False assert client.connector.connected == False assert client.connector.cutoff == False client.transmit() while (client.requests or client.connector.txbs or not client.responses or not server.idle()): server.service() time.sleep(0.05) client.service() time.sleep(0.05) tymist.tick(tock=0.1) assert client.connector.accepted == True assert client.connector.connected == True assert client.connector.cutoff == False assert len(server.servant.ixes) == 1 assert len(server.reqs) == 1 assert len(server.reps) == 1 requestant = list(server.reqs.values())[0] assert requestant.method == client.requester.method assert requestant.url == client.requester.path assert requestant.headers == help.Hict([('Host', 'localhost:8101'), ('Accept-Encoding', 'identity'), ('Accept', 'application/json'), ('Content-Length', '0')]) assert len(client.responses) == 1 rep = client.responses.popleft() assert rep['status'] == 201 assert rep['reason'] == 'Created' assert rep['headers']['Location'] == '/example/backend/stuff?stuff=good' assert rep['body'] == (b'{\n "approved": true,\n "body": "\\nHello World\\n\\n"\n}') assert rep['data'] == dict([('approved', True), ('body', '\nHello World\n\n')]) responder = list(server.reps.values())[0] assert responder.status.startswith(str(rep['status'])) assert responder.headers == rep['headers'] # pipeline another request # test for error resulting from sending query arg path headers = help.Hict([('Accept', 'application/json'), ('Content-Length', 0)]) client.request(method='GET', path='/example/backend', qargs=dict(path='/unknown'), headers=headers) while (client.requests or client.connector.txbs or not client.responses or not server.idle()): server.service() time.sleep(0.05) client.service() time.sleep(0.05) tymist.tick(tock=0.1) assert len(client.responses) == 1 rep = client.responses.popleft() assert rep['status'] == 404 assert rep['reason'] == 'Not Found' assert rep['body'] == bytearray(b'404 Not Found\nBackend Validation' b' Error\nError backend validation.' b' unknown\n') assert not rep['data'] """Done Test """
def test_bare_server_echo(): """ Test BaserServer service request response of echo non blocking """ tymist = tyming.Tymist(tyme=0.0) with http.openServer(cls=http.BareServer, port = 6101, bufsize=131072, \ tymth=tymist.tymen()) as alpha: assert alpha.servant.ha == ('0.0.0.0', 6101) assert alpha.servant.eha == ('127.0.0.1', 6101) path = "http://{0}:{1}/".format('localhost', alpha.servant.eha[1]) with http.openClient(bufsize=131072, path=path, tymth=tymist.tymen(), \ reconnectable=True,) as beta: assert not beta.connector.accepted assert not beta.connector.connected assert not beta.connector.cutoff request = dict([ ('method', u'GET'), ('path', u'/echo?name=fame'), ('qargs', dict()), ('fragment', u''), ('headers', dict([('Accept', 'application/json'), ('Content-Length', 0)])), ]) beta.requests.append(request) while (beta.requests or beta.connector.txbs or not beta.responses or not alpha.servant.ixes or not alpha.idle()): alpha.service() time.sleep(0.05) beta.service() time.sleep(0.05) assert beta.connector.accepted assert beta.connector.connected assert not beta.connector.cutoff assert len(alpha.servant.ixes) == 1 assert len(alpha.stewards) == 1 requestant = list(alpha.stewards.values())[0].requestant assert requestant.method == request['method'] assert requestant.url == request['path'] assert requestant.headers == help.Hict([ ('Host', 'localhost:6101'), ('Accept-Encoding', 'identity'), ('Accept', 'application/json'), ('Content-Length', '0') ]) assert len(beta.responses) == 1 response = beta.responses.popleft() assert response['data'] == { 'version': 'HTTP/1.1', 'method': 'GET', 'path': '/echo', 'qargs': { 'name': 'fame' }, 'fragment': '', 'headers': [['Host', 'localhost:6101'], ['Accept-Encoding', 'identity'], ['Accept', 'application/json'], ['Content-Length', '0']], 'body': '', 'data': None } responder = list(alpha.stewards.values())[0].responder assert responder.status == response['status'] assert responder.headers == response['headers']
def test_wsgi_server_tls(): """ Test Valet WSGI service with secure TLS request response """ tymist = tyming.Tymist(tyme=0.0) def wsgiApp(environ, start_response): start_response('200 OK', [('Content-type', 'text/plain'), ('Content-length', '12')]) return [b"Hello World!"] serverCertCommonName = 'localhost' # match hostname uses servers's cert commonname #serverKeypath = '/etc/pki/tls/certs/server_key.pem' # local server private key #serverCertpath = '/etc/pki/tls/certs/server_cert.pem' # local server public cert #clientCafilepath = '/etc/pki/tls/certs/client.pem' # remote client public cert serverKeypath = certdirpath + '/server_key.pem' # local server private key serverCertpath = certdirpath + '/server_cert.pem' # local server public cert clientCafilepath = certdirpath + '/client.pem' # remote client public cert with http.openServer(port = 6101, bufsize=131072, app=wsgiApp, \ scheme='https', keypath=serverKeypath, \ certpath=serverCertpath, cafilepath=clientCafilepath, \ tymth=tymist.tymen()) as alpha: assert alpha.servant.ha == ('0.0.0.0', 6101) assert alpha.servant.eha == ('127.0.0.1', 6101) #clientKeypath = '/etc/pki/tls/certs/client_key.pem' # local client private key #clientCertpath = '/etc/pki/tls/certs/client_cert.pem' # local client public cert #serverCafilepath = '/etc/pki/tls/certs/server.pem' # remote server public cert clientKeypath = certdirpath + '/client_key.pem' # local client private key clientCertpath = certdirpath + '/client_cert.pem' # local client public cert serverCafilepath = certdirpath + '/server.pem' # remote server public cert path = "https://{0}:{1}/".format('localhost', alpha.servant.eha[1]) with http.openClient(bufsize=131072, path=path, scheme='https', \ certedhost=serverCertCommonName, keypath=clientKeypath, \ certpath=clientCertpath, cafilepath=serverCafilepath, \ tymth=tymist.tymen(), reconnectable=True,) as beta: assert not beta.connector.accepted assert not beta.connector.connected assert not beta.connector.cutoff request = dict([ ('method', u'GET'), ('path', u'/echo?name=fame'), ('qargs', dict()), ('fragment', u''), ('headers', dict([('Accept', 'application/json'), ('Content-Length', 0)])), ]) beta.requests.append(request) while (beta.requests or beta.connector.txbs or not beta.responses or not alpha.idle()): alpha.service() time.sleep(0.05) beta.service() time.sleep(0.05) assert beta.connector.accepted assert beta.connector.connected assert not beta.connector.cutoff assert len(alpha.servant.ixes) == 1 assert len(alpha.reqs) == 1 assert len(alpha.reps) == 1 requestant = list(alpha.reqs.values())[0] assert requestant.method == request['method'] assert requestant.url == request['path'] assert requestant.headers == help.Hict([ ('Host', 'localhost:6101'), ('Accept-Encoding', 'identity'), ('Accept', 'application/json'), ('Content-Length', '0') ]) assert len(beta.responses) == 1 response = beta.responses.popleft() assert response['body'] == (b'Hello World!') assert response['status'] == 200 responder = list(alpha.reps.values())[0] responder.status.startswith(str(response['status'])) assert responder.headers == response['headers']
def test_wsgi_server(): """ Test WSGI Server service request response """ tymist = tyming.Tymist(tyme=0.0) def wsgiApp(environ, start_response): start_response('200 OK', [('Content-type', 'text/plain'), ('Content-length', '12')]) return [b"Hello World!"] with http.openServer(port = 6101, bufsize=131072, app=wsgiApp, \ tymth=tymist.tymen()) as alpha: # passthrough assert alpha.servant.ha == ('0.0.0.0', 6101) assert alpha.servant.eha == ('127.0.0.1', 6101) path = "http://{0}:{1}/".format('localhost', alpha.servant.eha[1]) with http.openClient(bufsize=131072, path=path, reconnectable=True, \ tymth=tymist.tymen()) as beta: assert not beta.connector.accepted assert not beta.connector.connected assert not beta.connector.cutoff request = dict([ ('method', u'GET'), ('path', u'/echo?name=fame'), ('qargs', dict()), ('fragment', u''), ('headers', dict([('Accept', 'application/json'), ('Content-Length', 0)])), ]) beta.requests.append(request) while (beta.requests or beta.connector.txbs or not beta.responses or not alpha.idle()): alpha.service() time.sleep(0.05) beta.service() time.sleep(0.05) assert beta.connector.accepted assert beta.connector.connected assert not beta.connector.cutoff assert len(alpha.servant.ixes) == 1 assert len(alpha.reqs) == 1 assert len(alpha.reps) == 1 requestant = list(alpha.reqs.values())[0] assert requestant.method == request['method'] assert requestant.url == request['path'] assert requestant.headers == help.Hict([ ('Host', 'localhost:6101'), ('Accept-Encoding', 'identity'), ('Accept', 'application/json'), ('Content-Length', '0') ]) assert len(beta.responses) == 1 response = beta.responses.popleft() assert response['body'] == (b'Hello World!') assert response['status'] == 200 responder = list(alpha.reps.values())[0] assert responder.status.startswith(str(response['status'])) assert responder.headers == response['headers']