Skip to content

Commit 891b041

Browse files
author
Luiko Czub
committed
refactor ProxiedTransport - first draft to fix #127
two separate transport classes, one for py27 support other for py3x
1 parent 8e4a332 commit 891b041

8 files changed

+146
-128
lines changed

src/testlink/proxiedtransport.py

-103
This file was deleted.

src/testlink/proxiedtransport2.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#! /usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
4+
# Copyright 2014-2020 Mario Benito, Luiko Czub, TestLink-API-Python-client developers
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# ------------------------------------------------------------------------
19+
# XMLRPC ProxiedTransport for Py27 as described in
20+
# https://docs.python.org/2.7/library/xmlrpclib.html#example-of-client-usage
21+
# ------------------------------------------------------------------------
22+
23+
import xmlrpclib, httplib
24+
25+
class ProxiedTransport(xmlrpclib.Transport):
26+
''' XMLRPC ProxiedTransport for Py27 as described in
27+
https://docs.python.org/2.7/library/xmlrpclib.html#example-of-client-usage
28+
'''
29+
30+
def set_proxy(self, proxy):
31+
self.proxy = proxy
32+
33+
def make_connection(self, host):
34+
self.realhost = host
35+
h = httplib.HTTPConnection(self.proxy)
36+
return h
37+
38+
def send_request(self, connection, handler, request_body):
39+
connection.putrequest("POST", 'http://%s%s' % (self.realhost, handler))
40+
41+
def send_host(self, connection, host):
42+
connection.putheader('Host', self.realhost)
43+

src/testlink/proxiedtransport3.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#! /usr/bin/python
2+
# -*- coding: UTF-8 -*-
3+
4+
# Copyright 2020 Luiko Czub, TestLink-API-Python-client developers
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
# ------------------------------------------------------------------------
19+
20+
from http.client import HTTPConnection
21+
from xmlrpc.client import Transport
22+
from urllib.parse import urlparse, urlunparse
23+
24+
class ProxiedTransport(Transport):
25+
''' XMLRPC ProxiedTransport for Py37+ as described in
26+
https://docs.python.org/3.8/library/xmlrpc.client.html#example-of-client-usage
27+
'''
28+
29+
def set_proxy(self, host, port=None, headers=None):
30+
''' if host includes a port definition (e.g. http://myHost:1111)
31+
this will be used instead the optional PORT arg
32+
'''
33+
34+
u1 = urlparse(host)
35+
uport = u1.port
36+
u2 = u1._replace(netloc=u1.hostname)
37+
uhost = urlunparse(u2)
38+
39+
self.proxy = uhost, uport or port
40+
self.proxy_headers = headers
41+
42+
def make_connection(self, host):
43+
connection = HTTPConnection(*self.proxy)
44+
connection.set_tunnel(host, headers=self.proxy_headers)
45+
self._connection = host, connection
46+
return connection

src/testlink/testlinkapigeneric.py

+5-7
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@
2121
IS_PY3 = sys.version_info[0] > 2
2222
if not IS_PY3:
2323
# importing required py2 modules
24-
import xmlrpclib
24+
import xmlrpclib as xmlrpc_client
2525
# in py 3 encodestring is deprecated and an alias for encodebytes
2626
# see issue #39 and compare py2 py3 doc
2727
# https://docs.python.org/2/library/base64.html#base64.encodestring
2828
# https://docs.python.org/3/library/base64.html#base64.encodebytes
2929
from base64 import encodestring as encodebytes
3030
else:
3131
# importing required py3 modules
32-
import xmlrpc.client as xmlrpclib
32+
import xmlrpc.client as xmlrpc_client
3333
from base64 import encodebytes
3434

3535
from platform import python_version
@@ -68,9 +68,7 @@ def __init__(self, server_url, devKey, **args):
6868
allow_none=args.get('allow_none',False)
6969
use_datetime = args.get('use_datetime', False)
7070
context = args.get('context', None)
71-
# named arg context is used, cause in Py36 it is place in a different
72-
# order as in Py27 and Py35
73-
self.server = xmlrpclib.Server(server_url, transport, encoding,
71+
self.server = xmlrpc_client.ServerProxy(server_url, transport, encoding,
7472
verbose, allow_none, use_datetime,
7573
context=context)
7674
self.devKey = devKey
@@ -2070,11 +2068,11 @@ def _callServer(self, methodNameAPI, argsAPI=None):
20702068
response = getattr(self.server.tl, methodNameAPI)()
20712069
else:
20722070
response = getattr(self.server.tl, methodNameAPI)(argsAPI)
2073-
except (IOError, xmlrpclib.ProtocolError) as msg:
2071+
except (IOError, xmlrpc_client.ProtocolError) as msg:
20742072
new_msg = 'problems connecting the TestLink Server %s\n%s' %\
20752073
(self._server_url, msg)
20762074
raise testlinkerrors.TLConnectionError(new_msg)
2077-
except xmlrpclib.Fault as msg:
2075+
except xmlrpc_client.Fault as msg:
20782076
new_msg = 'problems calling the API method %s\n%s' %\
20792077
(methodNameAPI, msg)
20802078
raise testlinkerrors.TLAPIError(new_msg)

src/testlink/testlinkhelper.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /usr/bin/python
22
# -*- coding: UTF-8 -*-
33

4-
# Copyright 2012-2019 Luiko Czub, TestLink-API-Python-client developers
4+
# Copyright 2012-2020 Luiko Czub, TestLink-API-Python-client developers
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -17,11 +17,17 @@
1717
#
1818
# ------------------------------------------------------------------------
1919

20-
import os
20+
import os, sys
2121
from argparse import ArgumentParser
2222
from .version import VERSION
2323
import ssl
2424

25+
IS_PY3 = sys.version_info[0] > 2
26+
if IS_PY3:
27+
from .proxiedtransport3 import ProxiedTransport
28+
else:
29+
from .proxiedtransport2 import ProxiedTransport
30+
2531
class TestLinkHelper(object):
2632
""" Helper Class to find out the TestLink connection parameters.
2733
a) TestLink Server URL of XML-RPC
@@ -131,7 +137,6 @@ def setParamsFromArgs(self, usage=DEFAULT_DESCRIPTION, args=None):
131137
def _getProxiedTransport(self):
132138
""" creates and return a ProxiedTransport with self._proxy settings """
133139

134-
from .proxiedtransport import ProxiedTransport
135140
a_pt = ProxiedTransport()
136141
a_pt.set_proxy(self._proxy)
137142
return a_pt

src/testlink/version.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /usr/bin/python
22
# -*- coding: UTF-8 -*-
33

4-
# Copyright 2013-2019 TestLink-API-Python-client developers
4+
# Copyright 2013-2020 TestLink-API-Python-client developers
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -17,5 +17,5 @@
1717
#
1818
# ------------------------------------------------------------------------
1919

20-
VERSION = '0.8.1-fix131'
20+
VERSION = '0.8.1-fix127'
2121
TL_RELEASE = '1.9.20-fixed'

test/utest-offline/test_py3_vs_py2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#! /usr/bin/python
22
# -*- coding: UTF-8 -*-
33

4-
# Copyright 2018-2019 Luiko Czub, TestLink-API-Python-client developers
4+
# Copyright 2018-2020 Luiko Czub, TestLink-API-Python-client developers
55
#
66
# Licensed under the Apache License, Version 2.0 (the "License");
77
# you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@
2525
#from conftest import api_general_client, api_generic_client
2626

2727
def test_IS_PY3_same_state():
28-
from testlink.proxiedtransport import IS_PY3 as proxie_is_py3
28+
from testlink.testlinkhelper import IS_PY3 as proxie_is_py3
2929
from testlink.testlinkapigeneric import IS_PY3 as tl_is_py3
3030
assert proxie_is_py3 == tl_is_py3
3131

test/utest-offline/testlinkhelper_test.py

+40-11
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
# this test works WITHOUT an online TestLink Server
2222
# no calls are send to a TestLink Server
2323

24-
import pytest
24+
import pytest, sys
25+
IS_PY3 = sys.version_info[0] > 2
2526

2627
class DummyTestLinkAPI(object):
2728
""" Dummy for Simulation TestLinkAPICLient.
@@ -140,22 +141,50 @@ def test_connect(api_helper_class):
140141
assert 'DEVKEY-51' == a_tl_api.devKey
141142
assert {} == a_tl_api.args
142143

143-
def test_getProxiedTransport(api_helper_class):
144-
""" create a ProxiedTransportTestLink API dummy """
145-
a_helper = api_helper_class('SERVER-URL-61', 'DEVKEY-61', 'PROXY-61')
144+
def test_getProxiedTransport_py2(api_helper_class):
145+
""" create a TestLink Helper with ProxiedTransport - py27 """
146+
if IS_PY3:
147+
pytest.skip("py27 specific test")
148+
149+
a_helper = api_helper_class('SERVER-URL-611', 'DEVKEY-611', 'PROXY-611:8080')
146150
#'http://fast.proxy.com.de/')
147151
a_pt = a_helper._getProxiedTransport()
148152
assert 'ProxiedTransport' == a_pt.__class__.__name__
149-
assert 'PROXY-61' == a_pt.proxy
153+
assert 'PROXY-611:8080' == a_pt.proxy
150154

155+
def test_getProxiedTransport_py3(api_helper_class):
156+
""" create a TestLink Helper with ProxiedTransport - py3x """
157+
158+
if not IS_PY3:
159+
pytest.skip("py3 specific test")
160+
161+
a_helper = api_helper_class('SERVER-URL-612', 'DEVKEY-612', 'http://PROXY-612:8080')
162+
#'http://fast.proxy.com.de/')
163+
a_pt = a_helper._getProxiedTransport()
164+
assert 'ProxiedTransport' == a_pt.__class__.__name__
165+
assert ('http://proxy-612', 8080) == a_pt.proxy
151166

152-
def test_connect_with_proxy(api_helper_class):
153-
""" create a TestLink API dummy with ProxiedTransport"""
154-
a_helper = api_helper_class('SERVER-URL-71', 'DEVKEY-71', 'PROXY-71')
167+
def test_connect_with_proxy2(api_helper_class):
168+
""" create a TestLink API dummy with ProxiedTransport - py27"""
169+
if IS_PY3:
170+
pytest.skip("py27 specific test")
171+
172+
a_helper = api_helper_class('SERVER-URL-711', 'DEVKEY-711', 'PROXY-71:8080')
155173
a_tl_api = a_helper.connect(DummyTestLinkAPI)
156-
assert 'SERVER-URL-71' == a_tl_api.server
157-
assert 'DEVKEY-71' == a_tl_api.devKey
158-
assert 'PROXY-71' == a_tl_api.args['transport'].proxy
174+
assert 'SERVER-URL-711' == a_tl_api.server
175+
assert 'DEVKEY-711' == a_tl_api.devKey
176+
assert 'PROXY-711' == a_tl_api.args['transport'].proxy
177+
178+
def test_connect_with_proxy3(api_helper_class):
179+
""" create a TestLink API dummy with ProxiedTransport - py3x"""
180+
if not IS_PY3:
181+
pytest.skip("py3 specific test")
182+
183+
a_helper = api_helper_class('SERVER-URL-712', 'DEVKEY-712', 'https://PROXY-712:8080')
184+
a_tl_api = a_helper.connect(DummyTestLinkAPI)
185+
assert 'SERVER-URL-712' == a_tl_api.server
186+
assert 'DEVKEY-712' == a_tl_api.devKey
187+
assert ('https://proxy-712', 8080) == a_tl_api.args['transport'].proxy
159188

160189
def test_connect_ignoring_proxy_env(api_helper_class, monkeypatch):
161190
""" create a TestLink API dummy ignoring PROXY env - pullRequest #121 """

0 commit comments

Comments
 (0)