File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change 1+ import re
12import six
23
4+
35def decode_utf8 (string , encoding = 'utf-8' ):
46 if hasattr (string , 'decode' ):
57 return string .decode (encoding )
68
79 return string
810
11+
912def chain_to_bytes (* strings ):
1013 return b'' .join ([six .b (string ) if isinstance (string , six .string_types ) else string for string in strings ])
14+
15+
16+ def get_hostname_for_ssl_match (hostname ):
17+ match = re .match (r'^((?P<subdomain>[^\.]+)\.)?(?P<domain>[^\./]+\.[^/]+)/?.*$' , hostname )
18+ domain = match .group ('domain' )
19+ return '*.{domain}' .format (domain = domain ) if match .group ('subdomain' ) else domain
Original file line number Diff line number Diff line change 11import pytest
22from mock import Mock
3- from rethinkdb .helpers import decode_utf8 , chain_to_bytes
3+ from rethinkdb .helpers import decode_utf8 , chain_to_bytes , get_hostname_for_ssl_match
44
55@pytest .mark .unit
66class TestDecodeUTF8Helper (object ):
@@ -42,3 +42,24 @@ def test_mixed_chaining(self):
4242 result = chain_to_bytes ('iron' , ' ' , b'man' )
4343
4444 assert result == expected_string
45+
46+
47+ @pytest .mark .unit
48+ class TestSSLMatchHostHostnameHelper (object ):
49+ def test_subdomain_replaced_to_star (self ):
50+ expected_string = '*.example.com'
51+
52+ result = get_hostname_for_ssl_match ('test.example.com' )
53+
54+ assert result == expected_string
55+
56+ def test_no_subdomain_to_replace (self ):
57+ expected_string = 'example.com'
58+
59+ result = get_hostname_for_ssl_match ('example.com' )
60+
61+ assert result == expected_string
62+
63+ def test_no_match (self ):
64+ with pytest .raises (AttributeError ) as exc :
65+ get_hostname_for_ssl_match ('' )
You can’t perform that action at this time.
0 commit comments