Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

testing with betamax?

未关闭
#212 5 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
30/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
python
领域
networking

调研方向

Start with CacheControlAdapter.send(), the existing cachecontrol test suite, and Betamax's old_adapters handling. Define tests for cache hits and misses when CacheControl is combined with Betamax, with completion shown by requests being served from the cache when present and delegated without unintended network access otherwise.

由索引模型根据 Issue 内容生成。

描述

is there a way to test this module alongside betamax?

the idea would be that the cachecontrol module would hit its own cache if present, otherwise bounce back into betamax. I am already using betamax extensively in my project to "mock" HTTP requests (record and replay, really) and it's been quite useful. But when I pair it with cachecontrol, I don't get good results.

either:

  1. i add cachecontrol as an "old_adapter" in the betamax session, which works in the sense that requests never hit the network, but then they never hit the cachecontrol adapter either, because the query gets cached in betamax. even if they would hit cachecontrol, they would need to hit the network first
  2. I mount cachecontrol on top of a betamax session, in which case cachecontrol completely obliterates the betamax adapter and sends requests on the network (instead of tapping in betamax)

Those two possible scenarios are pretty much covered in this ifcase:

cache_adapter = cachecontrol.CacheControlAdapter(cache=FeedContentCacheStorage(self.db_path))
# assume we mount over http and https all at once so check
# only the latter
adapter = self._session.adapters.get('https://', None)
if hasattr(adapter, 'old_adapters'):
    # looks like a betamax session was setup, hook ourselves behind it
    logging.debug('appending cache adapter (%r) to existing betamax adapter (%r)', cache_adapter, adapter)
    adapter.old_adapters['http://'] = cache_adapter
    adapter.old_adapters['https://'] = cache_adapter
else:
    logging.debug('mounting cache adapter (%r)', cache_adapter)
    # override existing adapters to use the cache adapter instead
    self._session.mount('http://', cache_adapter)
    self._session.mount('https://', cache_adapter)

That's pretty much case 1 above, which falls back to case 2 if we don't detect a "betamax" adapter in place.

And of course, it doesn't work.

What I would need is something like betamax's "old_adapters" system. Instead of calling the parent class, CacheControlAdapter.send() should allow the caller to define a list of adapters to call instead. The way betamax does this is fairly simple: there's an old_adapters parameter that you can pass and that it uses for sending its requests..

I have looked into the cachecontrol test suite to figure out if there could be inspiration there, but it seems to "mock" an http adapter instead of delegating requests... Since I don't want to reimplement the entire thing myself (and I can't figure out how to hook betamax in there either), could we add a similar functionality here?

Thanks!

PS: it seems I can mock a parent class but that seems horribly dirty - I am not sure I want to go that far...

主要语言
Python
星标
505
派生
153
平均合并
5 天 6 小时
30 天内合并 PR
5

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

psf/cachecontrol 的其他 Issue

查看 psf/cachecontrol 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。