Skip to content

Commit c349b2f

Browse files
authored
modernize SessionDetails (#1558)
* modernize SessionDetails * update changelog, bump release version
1 parent 06b3161 commit c349b2f

11 files changed

+565
-177
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ test_transport_details:
204204
USE_ASYNCIO=1 trial autobahn.wamp.test.test_wamp_transport_details
205205
USE_TWISTED=1 trial autobahn.wamp.test.test_wamp_transport_details
206206

207+
test_session_details:
208+
USE_ASYNCIO=1 trial autobahn.wamp.test.test_wamp_session_details
209+
USE_TWISTED=1 trial autobahn.wamp.test.test_wamp_session_details
210+
207211
test_tx_protocol:
208212
USE_TWISTED=1 trial autobahn.twisted.test.test_tx_protocol
209213

autobahn/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424
#
2525
###############################################################################
2626

27-
__version__ = '22.4.1.dev9'
27+
__version__ = '22.4.1'
2828

2929
__build__ = '00000000-0000000'

autobahn/asyncio/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def create_transport_details(transport, is_server: bool) -> TransportDetails:
134134
channel_id = {
135135
'tls-unique': transport_channel_id(transport, is_server, 'tls-unique'),
136136
}
137-
channel_type = TransportDetails.CHANNEL_TYPE_TLS_TCP
137+
channel_type = TransportDetails.CHANNEL_TYPE_TLS
138138
peer_cert = None
139139
else:
140140
channel_id = {}

autobahn/twisted/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def create_transport_details(transport: Union[ITransport, IProcessTransport], is
264264
channel_id = {
265265
'tls-unique': transport_channel_id(transport, is_server, 'tls-unique'),
266266
}
267-
channel_type = TransportDetails.CHANNEL_TYPE_TLS_TCP
267+
channel_type = TransportDetails.CHANNEL_TYPE_TLS
268268
peer_cert = extract_peer_certificate(transport)
269269
else:
270270
channel_id = {}

autobahn/wamp/protocol.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ def success(res):
602602
authprovider=self._authprovider,
603603
authextra=msg.authextra,
604604
serializer=self._transport._serializer.SERIALIZER_ID,
605-
transport=self._transport.peer_transport if hasattr(self._transport, 'peer_transport') else None,
605+
transport=self._transport.transport_details,
606606
resumed=msg.resumed,
607607
resumable=msg.resumable,
608608
resume_token=msg.resume_token,
@@ -1381,7 +1381,7 @@ def _errback_outstanding_requests(self, exc):
13811381
return d
13821382

13831383
@public
1384-
def onLeave(self, details: SessionDetails):
1384+
def onLeave(self, details: CloseDetails):
13851385
"""
13861386
Implements :meth:`autobahn.wamp.interfaces.ISession.onLeave`
13871387
"""

autobahn/wamp/test/test_wamp_protocol.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
from autobahn.wamp.exception import InvalidUri, ProtocolError
4444
from autobahn.wamp.auth import create_authenticator
4545
from autobahn.wamp.interfaces import IAuthenticator
46+
from autobahn.wamp.types import TransportDetails
4647

4748
class MockTransport(object):
4849

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

69+
self._transport_details = TransportDetails()
70+
71+
def transport_details(self):
72+
return self._transport_details
73+
6874
def drop_registration(self, reg_id):
6975
self._handler.onMessage(
7076
message.Unregistered(0, reg_id)
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
###############################################################################
2+
#
3+
# The MIT License (MIT)
4+
#
5+
# Copyright (c) Crossbar.io Technologies GmbH
6+
#
7+
# Permission is hereby granted, free of charge, to any person obtaining a copy
8+
# of this software and associated documentation files (the "Software"), to deal
9+
# in the Software without restriction, including without limitation the rights
10+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice shall be included in
15+
# all copies or substantial portions of the Software.
16+
#
17+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
# THE SOFTWARE.
24+
#
25+
###############################################################################
26+
27+
from autobahn.wamp.types import TransportDetails, SessionDetails
28+
29+
import unittest
30+
31+
from .test_wamp_transport_details import TRANSPORT_DETAILS_1
32+
33+
34+
class TestSessionDetails(unittest.TestCase):
35+
36+
def test_empty(self):
37+
sd1 = SessionDetails()
38+
data = sd1.marshal()
39+
self.assertEqual(data, {
40+
'realm': None,
41+
'session': None,
42+
'authid': None,
43+
'authrole': None,
44+
'authmethod': None,
45+
'authprovider': None,
46+
'authextra': None,
47+
'serializer': None,
48+
'transport': None,
49+
'resumed': None,
50+
'resumable': None,
51+
'resume_token': None,
52+
})
53+
sd2 = SessionDetails.parse(data)
54+
self.assertEqual(sd2, sd1)
55+
56+
def test_attributes(self):
57+
sd1 = SessionDetails()
58+
td1 = TransportDetails.parse(TRANSPORT_DETAILS_1)
59+
sd1.realm = 'realm1'
60+
sd1.session = 666
61+
sd1.authid = 'homer'
62+
sd1.authrole = 'user'
63+
sd1.authmethod = 'wampcra'
64+
sd1.authprovider = 'static'
65+
sd1.authextra = {'foo': 'bar', 'baz': [1, 2, 3]}
66+
sd1.serializer = 'json'
67+
sd1.transport = td1
68+
sd1.resumed = False
69+
sd1.resumable = True
70+
sd1.resume_token = '8713e25a-d4f5-48b7-9d6d-eda66603a1ab'
71+
data = sd1.marshal()
72+
self.assertEqual(data, {
73+
'realm': sd1.realm,
74+
'session': sd1.session,
75+
'authid': sd1.authid,
76+
'authrole': sd1.authrole,
77+
'authmethod': sd1.authmethod,
78+
'authprovider': sd1.authprovider,
79+
'authextra': sd1.authextra,
80+
'serializer': sd1.serializer,
81+
'transport': sd1.transport.marshal(),
82+
'resumed': sd1.resumed,
83+
'resumable': sd1.resumable,
84+
'resume_token': sd1.resume_token,
85+
})
86+
sd2 = SessionDetails.parse(data)
87+
self.assertEqual(sd2, sd1)

autobahn/wamp/test/test_wamp_transport_details.py

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,52 @@
2828

2929
import unittest
3030

31+
TRANSPORT_DETAILS_1 = {
32+
# TransportDetails.CHANNEL_TYPE_TO_STR[TransportDetails.CHANNEL_TYPE_TCP]
33+
'channel_type': 'tcp',
34+
35+
# TransportDetails.CHANNEL_FRAMING_TO_STR[TransportDetails.CHANNEL_FRAMING_WEBSOCKET]
36+
'channel_framing': 'websocket',
37+
38+
# TransportDetails.CHANNEL_SERIALIZER_TO_STR[TransportDetails.CHANNEL_SERIALIZER_CBOR]
39+
'channel_serializer': 'cbor',
40+
41+
# This end of the connection
42+
'own': 'ws://localhost:8080/ws',
43+
'own_pid': 9182731,
44+
'own_tid': 7563483,
45+
'own_fd': 20914571,
46+
47+
# Peer of the connection
48+
'peer': 'tcp4:127.0.0.1:48576',
49+
'is_server': True,
50+
51+
# TLS
52+
'is_secure': False,
53+
'channel_id': None,
54+
'peer_cert': None,
55+
56+
# only filled when using WebSocket
57+
'websocket_protocol': 'wamp.2.cbor.batched',
58+
'websocket_extensions_in_use': None,
59+
60+
# only filled when using HTTP (including regular WebSocket)
61+
'http_headers_received': {'cache-control': 'no-cache',
62+
'connection': 'Upgrade',
63+
'host': 'localhost:8080',
64+
'pragma': 'no-cache',
65+
'sec-websocket-extensions': 'permessage-deflate; '
66+
'client_no_context_takeover; '
67+
'client_max_window_bits',
68+
'sec-websocket-key': 'Q+t++aGQJPaFLzDW7LktEQ==',
69+
'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',
70+
'sec-websocket-version': '13',
71+
'upgrade': 'WebSocket',
72+
'user-agent': 'AutobahnPython/22.4.1.dev5'},
73+
'http_headers_sent': {'Set-Cookie': 'cbtid=JD27oZC18xS+O4VE9+x5iyKR;max-age=604800'},
74+
'http_cbtid': 'JD27oZC18xS+O4VE9+x5iyKR',
75+
}
76+
3177

3278
class TestTransportDetails(unittest.TestCase):
3379

@@ -71,53 +117,22 @@ def test_attributes(self):
71117
self.assertEqual(td.channel_serializer, channel_serializer)
72118

73119
def test_parse(self):
74-
data = {
75-
# TransportDetails.CHANNEL_TYPE_TO_STR[TransportDetails.CHANNEL_TYPE_TCP]
76-
'channel_type': 'tcp',
77-
78-
# TransportDetails.CHANNEL_FRAMING_TO_STR[TransportDetails.CHANNEL_FRAMING_WEBSOCKET]
79-
'channel_framing': 'websocket',
80-
81-
# TransportDetails.CHANNEL_SERIALIZER_TO_STR[TransportDetails.CHANNEL_SERIALIZER_CBOR]
82-
'channel_serializer': 'cbor',
83-
84-
# This end of the connection
85-
'own': 'ws://localhost:8080/ws',
86-
'own_pid': 9182731,
87-
'own_tid': 7563483,
88-
'own_fd': 20914571,
89-
90-
# Peer of the connection
91-
'peer': 'tcp4:127.0.0.1:48576',
92-
'is_server': True,
93-
94-
# TLS
95-
'is_secure': False,
96-
'channel_id': None,
97-
'peer_cert': None,
98-
99-
# only filled when using WebSocket
100-
'websocket_protocol': 'wamp.2.cbor.batched',
101-
'websocket_extensions_in_use': None,
102-
103-
# only filled when using HTTP (including regular WebSocket)
104-
'http_headers_received': {'cache-control': 'no-cache',
105-
'connection': 'Upgrade',
106-
'host': 'localhost:8080',
107-
'pragma': 'no-cache',
108-
'sec-websocket-extensions': 'permessage-deflate; '
109-
'client_no_context_takeover; '
110-
'client_max_window_bits',
111-
'sec-websocket-key': 'Q+t++aGQJPaFLzDW7LktEQ==',
112-
'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',
113-
'sec-websocket-version': '13',
114-
'upgrade': 'WebSocket',
115-
'user-agent': 'AutobahnPython/22.4.1.dev5'},
116-
'http_headers_sent': {'Set-Cookie': 'cbtid=JD27oZC18xS+O4VE9+x5iyKR;max-age=604800'},
117-
'http_cbtid': 'JD27oZC18xS+O4VE9+x5iyKR',
118-
}
119-
120-
td = TransportDetails.parse(data)
120+
td = TransportDetails.parse(TRANSPORT_DETAILS_1)
121121
data2 = td.marshal()
122122
self.maxDiff = None
123-
self.assertEqual(data2, data)
123+
self.assertEqual(data2, TRANSPORT_DETAILS_1)
124+
125+
def test_channel_typeid(self):
126+
# test empty
127+
td = TransportDetails()
128+
self.assertEqual(td.channel_typeid, 'null-null-null')
129+
130+
# test all combinations
131+
for channel_type in TransportDetails.CHANNEL_TYPE_TO_STR:
132+
for channel_framing in TransportDetails.CHANNEL_FRAMING_TO_STR:
133+
for channel_serializer in TransportDetails.CHANNEL_SERIALIZER_TO_STR:
134+
td = TransportDetails(channel_type=channel_type, channel_framing=channel_framing, channel_serializer=channel_serializer)
135+
channel_typeid = '{}-{}-{}'.format(TransportDetails.CHANNEL_TYPE_TO_STR[channel_type],
136+
TransportDetails.CHANNEL_FRAMING_TO_STR[channel_framing],
137+
TransportDetails.CHANNEL_SERIALIZER_TO_STR[channel_serializer])
138+
self.assertEqual(td.channel_typeid, channel_typeid)

autobahn/wamp/test/test_wamp_user_handler_errors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,15 @@
3333
from autobahn.wamp import message, role
3434
from autobahn.wamp.exception import ProtocolError
3535
from autobahn.twisted.wamp import ApplicationSession
36+
from autobahn.wamp.types import TransportDetails
3637

3738
class MockTransport:
3839
def __init__(self):
3940
self.messages = []
41+
self._transport_details = TransportDetails()
42+
43+
def transport_details(self):
44+
return self._transport_details
4045

4146
def send(self, msg):
4247
self.messages.append(msg)

0 commit comments

Comments
 (0)