Ejemplo n.º 1
0
def mock_session_with_class(session, cls, url):
    """
    Context Manager

    Mock the responses with a particular session
    to any private methods for the URLs

    :param session: The requests session object
    :type  session: :class:`requests.Session`

    :param cls: The class instance with private methods for URLs
    :type  cls: ``object``

    :param url: The base URL to mock, e.g. http://mock.com, http://
        supports a single URL or a list
    :type  url: ``str`` or ``list``
    """
    _orig_adapters = session.adapters
    mock_adapter = adapter.ClassAdapter(cls)
    session.adapters = OrderedDict()
    if isinstance(url, (list, tuple)):
        for u in url:
            session.mount(u, mock_adapter)
    else:
        session.mount(url, mock_adapter)
    yield
    session.adapters = _orig_adapters
Ejemplo n.º 2
0
        def _cache_wrapper(self, caller, *args, **kwargs):
            # Create a unique key including the types (in order to differentiate between 1 and '1'):
            kwargs_key = "".join(
                map(lambda x: str(x) + str(type(kwargs[x])) + str(kwargs[x]),
                    sorted(kwargs)))
            key = "".join(map(lambda x: str(type(x)) + str(x),
                              args)) + kwargs_key

            # Check if caller exists, if not create one:
            if caller not in self._caches_dict:
                self._caches_dict[caller] = [OrderedDict(), time.time()]
            else:
                # Validate in case the refresh time has passed:
                if self._timeout is not None:
                    if time.time(
                    ) - self._caches_dict[caller][1] > self._timeout:
                        self.cache_clear(caller)

            # Check if the key exists, if so - return it:
            cur_caller_cache_dict = self._caches_dict[caller][0]
            if key in cur_caller_cache_dict:
                return cur_caller_cache_dict[key]

            # Validate we didn't exceed the max_size:
            if len(cur_caller_cache_dict) >= self._max_size:
                # Delete the first item in the dict:
                cur_caller_cache_dict.popitem(False)

            # Call the function and store the data in the cache (call it with
            # the caller in case it's an instance function - Ternary condition):
            cur_caller_cache_dict[key] = self._input_func(
                caller, *args, **
                kwargs) if caller is not None else self._input_func(
                    *args, **kwargs)
            return cur_caller_cache_dict[key]
Ejemplo n.º 3
0
def mock_session_with_fixtures(session, path, url):
    """
    Context Manager

    Mock the responses with a particular session
    to any files found within a static path

    :param session: The requests session object
    :type  session: :class:`requests.Session`

    :param path: The path to the fixtures
    :type  path: ``str``

    :param url: The base URL to mock, e.g. http://mock.com, http://
        supports a single URL or a list
    :type  url: ``str`` or ``list``
    """
    _orig_adapters = session.adapters
    mock_adapter = adapter.Adapter()
    session.adapters = OrderedDict()
    if isinstance(url, (list, tuple)):
        for u in url:
            session.mount(u, mock_adapter)
    else:
        session.mount(url, mock_adapter)
    mock_adapter.register_path(path)
    yield
    session.adapters = _orig_adapters
Ejemplo n.º 4
0
 def test_params_original_order_is_preserved_by_default(self):
     param_ordered_dict = OrderedDict(
         (('z', 1), ('a', 1), ('k', 1), ('d', 1)))
     session = requests.Session()
     request = requests.Request('GET',
                                'http://example.com/',
                                params=param_ordered_dict)
     prep = session.prepare_request(request)
     assert prep.url == 'http://example.com/?z=1&a=1&k=1&d=1'
def test_class_adapter():
    class_session = Session()

    class TestMockClass(BaseMockClass):
        def _test_json(self, request):
            return "hello alabama"

    a = ClassAdapter(TestMockClass)
    class_session.adapters = OrderedDict()
    class_session.mount("http://test.com", a)
    response = class_session.get('http://test.com/test.json')
    assert response.text == "hello alabama"
Ejemplo n.º 6
0
class RecentOrderedDict(MutableMapping):
    """
    A custom variant of the OrderedDict that ensures that the object most
    recently inserted or retrieved from the dictionary is at the top of the
    dictionary enumeration.
    """

    def __init__(self, *args, **kwargs):
        self._data = OrderedDict(*args, **kwargs)

    def __setitem__(self, key, value):
        if key in self._data:
            del self._data[key]
        self._data[key] = value

    def __getitem__(self, key):
        value = self._data[key]
        del self._data[key]
        self._data[key] = value
        return value

    def __delitem__(self, key):
        del self._data[key]

    def __iter__(self):
        return iter(self._data)

    def __len__(self):
        return len(self._data)

    def __contains__(self, value):
        return self._data.__contains__(value)

    def items(self):
        return self._data.items()

    def keys(self):
        return self._data.keys()
Ejemplo n.º 7
0
class TestSession(requests.Session):
    """Just like requests.Session but solves an issue with adapter ordering
       for requests 1.2.0 and lower. It also doesn't connect the default
       handlers for HTTP and HTTPS so you will be notified if your requests
       unintentionally try to reach external websites in your unit tests."""
    def __init__(self):
        super(TestSession, self).__init__()
        self.adapters = OrderedDict()

    def mount(self, prefix, adapter):
        """Registers a connection adapter to a prefix.

        Adapters are sorted in descending order by key length."""
        self.adapters[prefix] = adapter
        keys_to_move = [k for k in self.adapters if len(k) < len(prefix)]
        for key in keys_to_move:
            self.adapters[key] = self.adapters.pop(key)
class TestSession(requests.Session):
    """Just like requests.Session but solves an issue with adapter ordering
       for requests 1.2.0 and lower. It also doesn't connect the default
       handlers for HTTP and HTTPS so you will be notified if your requests
       unintentionally try to reach external websites in your unit tests."""

    def __init__(self):
        super(TestSession, self).__init__()
        self.adapters = OrderedDict()

    def mount(self, prefix, adapter):
        """Registers a connection adapter to a prefix.

        Adapters are sorted in descending order by key length."""
        self.adapters[prefix] = adapter
        keys_to_move = [k for k in self.adapters if len(k) < len(prefix)]
        for key in keys_to_move:
            self.adapters[key] = self.adapters.pop(key)
Ejemplo n.º 9
0
    def _prepare_headers(self, headers):
        headers_order = [
            'Host',
            'User-Agent',
            'Accept',
            'Accept-Language',
            'Accept-Encoding',
            'Referer',
            'Cookie',
            'Connection',
            'Content-Type'
        ]

        self.headers = OrderedDict()
        if headers:
            for header_name in filter(lambda x: x in headers, headers_order):
                self.headers[header_name] = headers.get(header_name)
            for header_name in filter(lambda x: x not in self.headers, headers):
                self.headers[header_name] = headers.get(header_name)
Ejemplo n.º 10
0
def generate_request(method, url, body):
    """
    Generate our own custom request, so we can calculate digest auth.
    """
    method = method.upper()
    url = url
    files = []
    body = body
    json_string = None
    headers = CaseInsensitiveDict({
        'Accept': 'application/json',
        'Accept-Encoding': 'gzip, deflate',
        'Connection': 'keep-alive',
        'Content-Length': str(len(json.dumps(body))),
        'Content-Type': 'application/json',
        'User-Agent': 'custom_user_agent'
    })
    params = OrderedDict()
    auth = {
        'id': '34T89RR6UW2JWTTUCB0CF8D87',
        'secret': 'm2dPlw8ql20JdyPKA5uUB3Ppgs4nNSp45IJsqRRdp0g'}
    cookies = RequestsCookieJar()
    hooks = {'response': []}

    pr = PreparedRequest()
    pr.prepare(
        method=method.upper(),
        url=url,
        files=files,
        data=json.dumps(body),
        json=json_string,
        headers=headers,
        params=params,
        auth=auth,
        cookies=cookies,
        hooks=hooks,
    )

    return pr
Ejemplo n.º 11
0
 def __init__(self):
     super(TestSession, self).__init__()
     self.adapters = OrderedDict()
def _get_session():
    session = Session()
    a = Adapter('tests/fixtures')
    session.adapters = OrderedDict()
    session.mount("http://test.com", a)
    return session
 def __init__(self):
     super(TestSession, self).__init__()
     self.adapters = OrderedDict()
Ejemplo n.º 14
0
 def params(self):
     return urlencode(
         OrderedDict([('cbtt', self.station), ('interval', self.interval),
                      ('format', 1), ('back', self.start_index)]))
Ejemplo n.º 15
0
 def __init__(self, *args, **kwargs):
     self._data = OrderedDict(*args, **kwargs)
Ejemplo n.º 16
0
 def cache_clear(self, caller=None):
     # Remove the cache for the caller, only if exists:
     if caller in self._caches_dict:
         del self._caches_dict[caller]
         self._caches_dict[caller] = [OrderedDict(), time.time()]