# Path: opentracing_instrumentation/http_server.py
# class WSGIRequestWrapper(AbstractRequestWrapper):
#     """
#     Wraps WSGI environment and exposes several properties
#     used by the tracing methods.
#     """
# 
#     def __init__(self, wsgi_environ, headers):
#         self.wsgi_environ = wsgi_environ
#         self._headers = headers
# 
#     @classmethod
#     def from_wsgi_environ(cls, wsgi_environ):
#         instance = cls(wsgi_environ=wsgi_environ,
#                        headers=cls._parse_wsgi_headers(wsgi_environ))
#         return instance
# 
#     @staticmethod
#     def _parse_wsgi_headers(wsgi_environ):
#         """
#         HTTP headers are presented in WSGI environment with 'HTTP_' prefix.
#         This method finds those headers, removes the prefix, converts
#         underscores to dashes, and converts to lower case.
# 
#         :param wsgi_environ:
#         :return: returns a dictionary of headers
#         """
#         prefix = 'HTTP_'
#         p_len = len(prefix)
#         # use .items() despite suspected memory pressure bc GC occasionally
#         # collects wsgi_environ.iteritems() during iteration.
#         headers = {
#             key[p_len:].replace('_', '-').lower():
#                 val for (key, val) in wsgi_environ.items()
#             if key.startswith(prefix)}
#         return headers
# 
#     @property
#     def full_url(self):
#         """
#         Taken from
#         http://legacy.python.org/dev/peps/pep-3333/#url-reconstruction
# 
#         :return: Reconstructed URL from WSGI environment.
#         """
#         environ = self.wsgi_environ
#         url = environ['wsgi.url_scheme'] + '://'
# 
#         if environ.get('HTTP_HOST'):
#             url += environ['HTTP_HOST']
#         else:
#             url += environ['SERVER_NAME']
# 
#             if environ['wsgi.url_scheme'] == 'https':
#                 if environ['SERVER_PORT'] != '443':
#                     url += ':' + environ['SERVER_PORT']
#             else:
#                 if environ['SERVER_PORT'] != '80':
#                     url += ':' + environ['SERVER_PORT']
# 
#         url += urllib.parse.quote(environ.get('SCRIPT_NAME', ''))
#         url += urllib.parse.quote(environ.get('PATH_INFO', ''))
#         if environ.get('QUERY_STRING'):
#             url += '?' + environ['QUERY_STRING']
#         return url
# 
#     @property
#     def headers(self):
#         return self._headers
# 
#     @property
#     def method(self):
#         return self.wsgi_environ.get('REQUEST_METHOD')
# 
#     @property
#     def remote_ip(self):
#         return self.wsgi_environ.get('REMOTE_ADDR', None)
# 
#     @property
#     def remote_port(self):
#         return self.wsgi_environ.get('REMOTE_PORT', None)
# 
#     @property
#     def server_port(self):
#         return self.wsgi_environ.get('SERVER_PORT', None)


import mock
from opentracing_instrumentation.http_server import WSGIRequestWrapper
    from opentracing_instrumentation import config

def test_url():
    environ = {
        'wsgi.url_scheme': 'http',
        'HTTP_HOST': 'bender.com'
    }
    request = WSGIRequestWrapper.from_wsgi_environ(environ)
    assert request.full_url == 'http://bender.com'

    environ = {
        'wsgi.url_scheme': 'http',
        'SERVER_NAME': 'bender.com',
        'SERVER_PORT': '80'
    }
    request = WSGIRequestWrapper.from_wsgi_environ(environ)
    assert request.full_url == 'http://bender.com'

    environ['SERVER_PORT'] = '8888'
    request = WSGIRequestWrapper.from_wsgi_environ(environ)
    assert request.full_url == 'http://bender.com:8888'

    environ['wsgi.url_scheme'] = 'https'
    request = WSGIRequestWrapper.from_wsgi_environ(environ)
    assert request.full_url == 'https://bender.com:8888'

    environ['SERVER_PORT'] = '443'
    request = WSGIRequestWrapper.from_wsgi_environ(environ)
    assert request.full_url == 'https://bender.com'

    environ['SCRIPT_NAME'] = '/Farnsworth'

# Path: opentracing_instrumentation/http_server.py
# class WSGIRequestWrapper(AbstractRequestWrapper):
#     """
#     Wraps WSGI environment and exposes several properties
#     used by the tracing methods.
#     """
# 
#     def __init__(self, wsgi_environ, headers):
#         self.wsgi_environ = wsgi_environ
#         self._headers = headers
# 
#     @classmethod
#     def from_wsgi_environ(cls, wsgi_environ):
#         instance = cls(wsgi_environ=wsgi_environ,
#                        headers=cls._parse_wsgi_headers(wsgi_environ))
#         return instance
# 
#     @staticmethod
#     def _parse_wsgi_headers(wsgi_environ):
#         """
#         HTTP headers are presented in WSGI environment with 'HTTP_' prefix.
#         This method finds those headers, removes the prefix, converts
#         underscores to dashes, and converts to lower case.
# 
#         :param wsgi_environ:
#         :return: returns a dictionary of headers
#         """
#         prefix = 'HTTP_'
#         p_len = len(prefix)
#         # use .items() despite suspected memory pressure bc GC occasionally
#         # collects wsgi_environ.iteritems() during iteration.
#         headers = {
#             key[p_len:].replace('_', '-').lower():
#                 val for (key, val) in wsgi_environ.items()
#             if key.startswith(prefix)}
#         return headers
# 
#     @property
#     def full_url(self):
#         """
#         Taken from
#         http://legacy.python.org/dev/peps/pep-3333/#url-reconstruction
# 
#         :return: Reconstructed URL from WSGI environment.
#         """
#         environ = self.wsgi_environ
#         url = environ['wsgi.url_scheme'] + '://'
# 
#         if environ.get('HTTP_HOST'):
#             url += environ['HTTP_HOST']
#         else:
#             url += environ['SERVER_NAME']
# 
#             if environ['wsgi.url_scheme'] == 'https':
#                 if environ['SERVER_PORT'] != '443':
#                     url += ':' + environ['SERVER_PORT']
#             else:
#                 if environ['SERVER_PORT'] != '80':
#                     url += ':' + environ['SERVER_PORT']
# 
#         url += urllib.parse.quote(environ.get('SCRIPT_NAME', ''))
#         url += urllib.parse.quote(environ.get('PATH_INFO', ''))
#         if environ.get('QUERY_STRING'):
#             url += '?' + environ['QUERY_STRING']
#         return url
# 
#     @property
#     def headers(self):
#         return self._headers
# 
#     @property
#     def method(self):
#         return self.wsgi_environ.get('REQUEST_METHOD')
# 
#     @property
#     def remote_ip(self):
#         return self.wsgi_environ.get('REMOTE_ADDR', None)
# 
#     @property
#     def remote_port(self):
#         return self.wsgi_environ.get('REMOTE_PORT', None)
# 
#     @property
#     def server_port(self):
#         return self.wsgi_environ.get('SERVER_PORT', None)


# Path: tests/opentracing_instrumentation/test_wsgi_request.py
import mock
from opentracing_instrumentation.http_server import WSGIRequestWrapper
    from opentracing_instrumentation import config


def test_url():
    environ = {
        'wsgi.url_scheme': 'http',
        'HTTP_HOST': 'bender.com'
    }
    request = WSGIRequestWrapper.from_wsgi_environ(environ)
    assert request.full_url == 'http://bender.com'

    environ = {
        'wsgi.url_scheme': 'http',
        'SERVER_NAME': 'bender.com',
        'SERVER_PORT': '80'
    }
    request = WSGIRequestWrapper.from_wsgi_environ(environ)
    assert request.full_url == 'http://bender.com'

    environ['SERVER_PORT'] = '8888'
    request = WSGIRequestWrapper.from_wsgi_environ(environ)
    assert request.full_url == 'http://bender.com:8888'

    environ['wsgi.url_scheme'] = 'https'
    request = WSGIRequestWrapper.from_wsgi_environ(environ)
    assert request.full_url == 'https://bender.com:8888'

    environ['SERVER_PORT'] = '443'
    request = WSGIRequestWrapper.from_wsgi_environ(environ)
    assert request.full_url == 'https://bender.com'

    environ['SCRIPT_NAME'] = '/Farnsworth'

    request = WSGIRequestWrapper.from_wsgi_environ(environ)