File: test_adapter.py

package info (click to toggle)
python-apeye 1.4.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 940 kB
  • sloc: python: 2,724; makefile: 10
file content (62 lines) | stat: -rw-r--r-- 1,341 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# SPDX-FileCopyrightText: 2015 Eric Larson
#
# SPDX-License-Identifier: Apache-2.0
#
# From https://github.com/ionrock/cachecontrol

# 3rd party
import pytest
from cachecontrol.wrapper import CacheControl
from requests import Session

# this package
from apeye.rate_limiter import RateLimitAdapter


def use_wrapper():
	print("Using helper")
	sess = CacheControl(Session())
	return sess


def use_adapter():
	print("Using adapter")
	sess = Session()
	sess.mount("http://", RateLimitAdapter())
	return sess


@pytest.fixture(params=[use_adapter, use_wrapper])
def sess(url, request):
	sess = request.param()
	sess.get(url)
	yield sess

	# closing session object
	sess.close()


class TestSessionActions:

	def test_get_caches(self, url, sess):
		r2 = sess.get(url)
		assert r2.from_cache is True

	def test_get_with_no_cache_does_not_cache(self, url, sess):
		r2 = sess.get(url, headers={"Cache-Control": "no-cache"})
		assert not r2.from_cache

	def test_put_invalidates_cache(self, url, sess):
		r2 = sess.put(url, data={"foo": "bar"})
		sess.get(url)
		assert not r2.from_cache

	def test_patch_invalidates_cache(self, url, sess):
		r2 = sess.patch(url, data={"foo": "bar"})
		sess.get(url)
		assert not r2.from_cache

	def test_delete_invalidates_cache(self, url, sess):
		r2 = sess.delete(url)
		sess.get(url)
		assert not r2.from_cache