def test_socket_closed_hook(self): server = vertx.create_sockjs_server(vertx.create_http_server()) bridge = server.bridge({'prefix': '/eventbus'}, [], []) @bridge.socket_closed_handler def handler(socket): tu.azzert(isinstance(socket, SockJSSocket)) tu.test_complete() bridge.hook.handleSocketClosed(None)
def test_http(): # Create an HTTP server which just sends back OK response immediately def req_handler(req): req.response.end() def resp_handler(resp): VertxAssert.assertTrue(200 == resp.status_code) # If we get here, the test is complete # You must always call `testComplete()` at the end. Remember that testing is *asynchronous* so # we cannot assume the test is complete by the time the test method has finished executing like # in standard synchronous tests VertxAssert.testComplete() def listen_handler(err, server): VertxAssert.assertNull(err) # The server is listening so send an HTTP request vertx.create_http_client().set_port(8181).get_now("/", resp_handler) vertx.create_http_server().request_handler(req_handler).listen(8181, "0.0.0.0", listen_handler)
def test_unregister_hook(self): server = vertx.create_sockjs_server(vertx.create_http_server()) bridge = server.bridge({'prefix': '/eventbus'}, [], []) @bridge.unregister_handler def handler(socket, address): tu.azzert(isinstance(socket, SockJSSocket)) tu.azzert(address == 'some-address') tu.test_complete() bridge.hook.handleUnregister(None, 'some-address')
def test_send_or_pub_hook(self): server = vertx.create_sockjs_server(vertx.create_http_server()) bridge = server.bridge({'prefix': '/eventbus'}, [], []) @bridge.send_or_pub_handler def handler(socket, send, message, address): tu.azzert(isinstance(socket, SockJSSocket)) tu.azzert(send == True) tu.azzert(message['foo'] == 'bar') tu.azzert(address == 'some-address') tu.test_complete() bridge.hook.handleSendOrPub(None, True, {'foo': 'bar'}, 'some-address')
def test_authorise_hook(self): server = vertx.create_sockjs_server(vertx.create_http_server()) bridge = server.bridge({'prefix': '/eventbus'}, [], []) @bridge.authorise_handler def handler(message, session_id, handler): tu.azzert(message['foo'] == 'bar') tu.azzert(session_id == 'some-id') handler(True) def done_handler(error, result): tu.azzert(error is None) tu.azzert(result) tu.test_complete() bridge.hook.handleAuthorise({'foo': 'bar'}, 'some-id', AsyncHandler(done_handler))
# # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx server = vertx.create_http_server() @server.request_handler def handle(req): print "Got request %s"% req.uri for header in req.headers.keys(): print "%s : %s"% (header, req.headers[header]) @req.data_handler def data_handler(data): print "Got data %s"% data @req.end_handler def end_handler(): req.response.chunked = True
# Copyright 2011 the original author or authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx server = vertx.create_http_server(ssl=True) server.key_store_path = "server-keystore.jks" server.key_store_password = "******" @server.request_handler def handle(req): req.response.end("<html><body><h1>Hello from vert.x over HTTPS!</h1></body></html>") server.listen(4443)
import vertx server = vertx.create_http_server() @server.websocket_handler def websocket_handler(ws): def data_handler(buffer): ws.write_buffer(buffer) ws.data_handler(data_handler) server.listen(8000)
# you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import vertx from core.http import RouteMatcher # Inspired from Sinatra / Express rm = RouteMatcher() def user_request_handler(req): req.response.end("User: %s ID: %s"% (req.params["user"], req.params["id"]) ) # Extract the params from the uri rm.get('/details/:user/:id', user_request_handler) def request_handler(req): req.response.send_file("route_match/index.html") # Catch all - serve the index page rm.get_re('.*', request_handler) vertx.create_http_server().request_handler(rm).listen(8080)