Skip to content

Commit

Permalink
modernize SessionDetails (#1558)
Browse files Browse the repository at this point in the history
* modernize SessionDetails
* update changelog, bump release version
  • Loading branch information
oberstet committed May 3, 2022
1 parent 06b3161 commit c349b2f
Show file tree
Hide file tree
Showing 11 changed files with 565 additions and 177 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ test_transport_details:
USE_ASYNCIO=1 trial autobahn.wamp.test.test_wamp_transport_details
USE_TWISTED=1 trial autobahn.wamp.test.test_wamp_transport_details

test_session_details:
USE_ASYNCIO=1 trial autobahn.wamp.test.test_wamp_session_details
USE_TWISTED=1 trial autobahn.wamp.test.test_wamp_session_details

test_tx_protocol:
USE_TWISTED=1 trial autobahn.twisted.test.test_tx_protocol

Expand Down
2 changes: 1 addition & 1 deletion autobahn/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
#
###############################################################################

__version__ = '22.4.1.dev9'
__version__ = '22.4.1'

__build__ = '00000000-0000000'
2 changes: 1 addition & 1 deletion autobahn/asyncio/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def create_transport_details(transport, is_server: bool) -> TransportDetails:
channel_id = {
'tls-unique': transport_channel_id(transport, is_server, 'tls-unique'),
}
channel_type = TransportDetails.CHANNEL_TYPE_TLS_TCP
channel_type = TransportDetails.CHANNEL_TYPE_TLS
peer_cert = None
else:
channel_id = {}
Expand Down
2 changes: 1 addition & 1 deletion autobahn/twisted/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def create_transport_details(transport: Union[ITransport, IProcessTransport], is
channel_id = {
'tls-unique': transport_channel_id(transport, is_server, 'tls-unique'),
}
channel_type = TransportDetails.CHANNEL_TYPE_TLS_TCP
channel_type = TransportDetails.CHANNEL_TYPE_TLS
peer_cert = extract_peer_certificate(transport)
else:
channel_id = {}
Expand Down
4 changes: 2 additions & 2 deletions autobahn/wamp/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ def success(res):
authprovider=self._authprovider,
authextra=msg.authextra,
serializer=self._transport._serializer.SERIALIZER_ID,
transport=self._transport.peer_transport if hasattr(self._transport, 'peer_transport') else None,
transport=self._transport.transport_details,
resumed=msg.resumed,
resumable=msg.resumable,
resume_token=msg.resume_token,
Expand Down Expand Up @@ -1381,7 +1381,7 @@ def _errback_outstanding_requests(self, exc):
return d

@public
def onLeave(self, details: SessionDetails):
def onLeave(self, details: CloseDetails):
"""
Implements :meth:`autobahn.wamp.interfaces.ISession.onLeave`
"""
Expand Down
6 changes: 6 additions & 0 deletions autobahn/wamp/test/test_wamp_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from autobahn.wamp.exception import InvalidUri, ProtocolError
from autobahn.wamp.auth import create_authenticator
from autobahn.wamp.interfaces import IAuthenticator
from autobahn.wamp.types import TransportDetails

class MockTransport(object):

Expand All @@ -65,6 +66,11 @@ def __init__(self, handler):
self._handler.onMessage(msg)
self._fake_router_session = ApplicationSession()

self._transport_details = TransportDetails()

def transport_details(self):
return self._transport_details

def drop_registration(self, reg_id):
self._handler.onMessage(
message.Unregistered(0, reg_id)
Expand Down
87 changes: 87 additions & 0 deletions autobahn/wamp/test/test_wamp_session_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
###############################################################################
#
# The MIT License (MIT)
#
# Copyright (c) Crossbar.io Technologies GmbH
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
###############################################################################

from autobahn.wamp.types import TransportDetails, SessionDetails

import unittest

from .test_wamp_transport_details import TRANSPORT_DETAILS_1


class TestSessionDetails(unittest.TestCase):

def test_empty(self):
sd1 = SessionDetails()
data = sd1.marshal()
self.assertEqual(data, {
'realm': None,
'session': None,
'authid': None,
'authrole': None,
'authmethod': None,
'authprovider': None,
'authextra': None,
'serializer': None,
'transport': None,
'resumed': None,
'resumable': None,
'resume_token': None,
})
sd2 = SessionDetails.parse(data)
self.assertEqual(sd2, sd1)

def test_attributes(self):
sd1 = SessionDetails()
td1 = TransportDetails.parse(TRANSPORT_DETAILS_1)
sd1.realm = 'realm1'
sd1.session = 666
sd1.authid = 'homer'
sd1.authrole = 'user'
sd1.authmethod = 'wampcra'
sd1.authprovider = 'static'
sd1.authextra = {'foo': 'bar', 'baz': [1, 2, 3]}
sd1.serializer = 'json'
sd1.transport = td1
sd1.resumed = False
sd1.resumable = True
sd1.resume_token = '8713e25a-d4f5-48b7-9d6d-eda66603a1ab'
data = sd1.marshal()
self.assertEqual(data, {
'realm': sd1.realm,
'session': sd1.session,
'authid': sd1.authid,
'authrole': sd1.authrole,
'authmethod': sd1.authmethod,
'authprovider': sd1.authprovider,
'authextra': sd1.authextra,
'serializer': sd1.serializer,
'transport': sd1.transport.marshal(),
'resumed': sd1.resumed,
'resumable': sd1.resumable,
'resume_token': sd1.resume_token,
})
sd2 = SessionDetails.parse(data)
self.assertEqual(sd2, sd1)
111 changes: 63 additions & 48 deletions autobahn/wamp/test/test_wamp_transport_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,52 @@

import unittest

TRANSPORT_DETAILS_1 = {
# TransportDetails.CHANNEL_TYPE_TO_STR[TransportDetails.CHANNEL_TYPE_TCP]
'channel_type': 'tcp',

# TransportDetails.CHANNEL_FRAMING_TO_STR[TransportDetails.CHANNEL_FRAMING_WEBSOCKET]
'channel_framing': 'websocket',

# TransportDetails.CHANNEL_SERIALIZER_TO_STR[TransportDetails.CHANNEL_SERIALIZER_CBOR]
'channel_serializer': 'cbor',

# This end of the connection
'own': 'ws://localhost:8080/ws',
'own_pid': 9182731,
'own_tid': 7563483,
'own_fd': 20914571,

# Peer of the connection
'peer': 'tcp4:127.0.0.1:48576',
'is_server': True,

# TLS
'is_secure': False,
'channel_id': None,
'peer_cert': None,

# only filled when using WebSocket
'websocket_protocol': 'wamp.2.cbor.batched',
'websocket_extensions_in_use': None,

# only filled when using HTTP (including regular WebSocket)
'http_headers_received': {'cache-control': 'no-cache',
'connection': 'Upgrade',
'host': 'localhost:8080',
'pragma': 'no-cache',
'sec-websocket-extensions': 'permessage-deflate; '
'client_no_context_takeover; '
'client_max_window_bits',
'sec-websocket-key': 'Q+t++aGQJPaFLzDW7LktEQ==',
'sec-websocket-protocol': 'wamp.2.cbor.batched,wamp.2.cbor,wamp.2.msgpack.batched,wamp.2.msgpack,wamp.2.ubjson.batched,wamp.2.ubjson,wamp.2.json.batched,wamp.2.json',
'sec-websocket-version': '13',
'upgrade': 'WebSocket',
'user-agent': 'AutobahnPython/22.4.1.dev5'},
'http_headers_sent': {'Set-Cookie': 'cbtid=JD27oZC18xS+O4VE9+x5iyKR;max-age=604800'},
'http_cbtid': 'JD27oZC18xS+O4VE9+x5iyKR',
}


class TestTransportDetails(unittest.TestCase):

Expand Down Expand Up @@ -71,53 +117,22 @@ def test_attributes(self):
self.assertEqual(td.channel_serializer, channel_serializer)

def test_parse(self):
data = {
# TransportDetails.CHANNEL_TYPE_TO_STR[TransportDetails.CHANNEL_TYPE_TCP]
'channel_type': 'tcp',

# TransportDetails.CHANNEL_FRAMING_TO_STR[TransportDetails.CHANNEL_FRAMING_WEBSOCKET]
'channel_framing': 'websocket',

# TransportDetails.CHANNEL_SERIALIZER_TO_STR[TransportDetails.CHANNEL_SERIALIZER_CBOR]
'channel_serializer': 'cbor',

# This end of the connection
'own': 'ws://localhost:8080/ws',
'own_pid': 9182731,
'own_tid': 7563483,
'own_fd': 20914571,

# Peer of the connection
'peer': 'tcp4:127.0.0.1:48576',
'is_server': True,

# TLS
'is_secure': False,
'channel_id': None,
'peer_cert': None,

# only filled when using WebSocket
'websocket_protocol': 'wamp.2.cbor.batched',
'websocket_extensions_in_use': None,

# only filled when using HTTP (including regular WebSocket)
'http_headers_received': {'cache-control': 'no-cache',
'connection': 'Upgrade',
'host': 'localhost:8080',
'pragma': 'no-cache',
'sec-websocket-extensions': 'permessage-deflate; '
'client_no_context_takeover; '
'client_max_window_bits',
'sec-websocket-key': 'Q+t++aGQJPaFLzDW7LktEQ==',
'sec-websocket-protocol': 'wamp.2.cbor.batched,wamp.2.cbor,wamp.2.msgpack.batched,wamp.2.msgpack,wamp.2.ubjson.batched,wamp.2.ubjson,wamp.2.json.batched,wamp.2.json',
'sec-websocket-version': '13',
'upgrade': 'WebSocket',
'user-agent': 'AutobahnPython/22.4.1.dev5'},
'http_headers_sent': {'Set-Cookie': 'cbtid=JD27oZC18xS+O4VE9+x5iyKR;max-age=604800'},
'http_cbtid': 'JD27oZC18xS+O4VE9+x5iyKR',
}

td = TransportDetails.parse(data)
td = TransportDetails.parse(TRANSPORT_DETAILS_1)
data2 = td.marshal()
self.maxDiff = None
self.assertEqual(data2, data)
self.assertEqual(data2, TRANSPORT_DETAILS_1)

def test_channel_typeid(self):
# test empty
td = TransportDetails()
self.assertEqual(td.channel_typeid, 'null-null-null')

# test all combinations
for channel_type in TransportDetails.CHANNEL_TYPE_TO_STR:
for channel_framing in TransportDetails.CHANNEL_FRAMING_TO_STR:
for channel_serializer in TransportDetails.CHANNEL_SERIALIZER_TO_STR:
td = TransportDetails(channel_type=channel_type, channel_framing=channel_framing, channel_serializer=channel_serializer)
channel_typeid = '{}-{}-{}'.format(TransportDetails.CHANNEL_TYPE_TO_STR[channel_type],
TransportDetails.CHANNEL_FRAMING_TO_STR[channel_framing],
TransportDetails.CHANNEL_SERIALIZER_TO_STR[channel_serializer])
self.assertEqual(td.channel_typeid, channel_typeid)
5 changes: 5 additions & 0 deletions autobahn/wamp/test/test_wamp_user_handler_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@
from autobahn.wamp import message, role
from autobahn.wamp.exception import ProtocolError
from autobahn.twisted.wamp import ApplicationSession
from autobahn.wamp.types import TransportDetails

class MockTransport:
def __init__(self):
self.messages = []
self._transport_details = TransportDetails()

def transport_details(self):
return self._transport_details

def send(self, msg):
self.messages.append(msg)
Expand Down

0 comments on commit c349b2f

Please sign in to comment.