python - 使用 Oauth1 证书模拟 python HTTP 请求

标签 python python-3.x mocking python-requests python-responses

我正在使用python response library模拟带有请求的调用,但我收到此错误:

File "/lib/python3.5/site-packages/requests/api.py", line 110, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/lib/python3.5/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/lib/python3.5/site-packages/requests/sessions.py", line 474, in request
    prep = self.prepare_request(req)
  File "/lib/python3.5/site-packages/requests/sessions.py", line 407, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "/lib/python3.5/site-packages/requests/models.py", line 306, in prepare
    self.prepare_auth(auth, url)
  File "/lib/python3.5/site-packages/requests/models.py", line 518, in prepare_auth
    r = auth(self)
  File "/lib/python3.5/site-packages/requests_oauthlib/oauth1_auth.py", line 88, in __call__
    unicode(r.url), unicode(r.method), None, r.headers)
  File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 313, in sign
    ('oauth_signature', self.get_oauth_signature(request)))
  File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 150, in get_oauth_signature
    sig = self.SIGNATURE_METHODS[self.signature_method](base_string, self)
  File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 505, in sign_rsa_sha1_with_client
    return sign_rsa_sha1(base_string, client.rsa_key)
  File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 497, in sign_rsa_sha1
    key = _prepare_key_plus(alg, rsa_private_key)
  File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 574, in _prepare_key_plus
    return alg.prepare_key(keystr)
  File "/lib/python3.5/site-packages/jwt/algorithms.py", line 169, in prepare_key
    key = load_pem_public_key(key, backend=default_backend())
  File "/lib/python3.5/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
    return backend.load_pem_public_key(data)
  File "/lib/python3.5/site-packages/cryptography/hazmat/backends/multibackend.py", line 312, in load_pem_public_key
    return b.load_pem_public_key(data)
  File "/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1074, in load_pem_public_key
    self._handle_key_loading_error()
  File "/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1289, in _handle_key_loading_error
    raise ValueError("Could not unserialize key data.")
ValueError: Could not unserialize key data

我尝试模拟的代码如下:

def function_to_test():
        oauth = OAuth1(
            self.consumer_key,
            client_secret=self.consumer_secret,
            resource_owner_key=self.oauth_token,
            resource_owner_secret=self.oauth_token_secret,
            rsa_key=self.rsa_key,
            signature_method=self._signature_method
        )
        return requests.post(url="https://example.com", auth=oauth, cert="/path/to/certificate")

以及测试代码:

@responses.activate
def test_token_expired(self):
    responses.add(responses.POST, url='https://example.com/',
                  body='my_expected_result',
                  status=200)
    response = function_to_test()
    self.assertEqual(response.content, 'my_expected_result)

测试失败并显示上面的错误,这是否意味着响应模拟不起作用?它与我的 requests.post 中的 auth/cert 参数有关吗?我错过了什么吗?

最佳答案

我认为您的问题与responses库无关,而是与OAuth1初始化有关。

在尝试模拟请求之前,OAuth1 会失败,因为他正在尝试访问 key ,而该 key 在您的测试中可能未正确初始化。

您可以为此 key 提供一个假值,甚至在模拟 OAuth1 对象时,responses 将能够模拟您的调用。

我们本可以期望响应完全模拟请求调用,但似乎在此之前仍然发生了一些初始化。

关于python - 使用 Oauth1 证书模拟 python HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40863051/

相关文章:

python - 单击在 python 中使用自动化 id

python - 安装 Anaconda3 会将 Mac OS X 默认 Python 版本更改为 3.4 吗?

javascript - 您使用过哪些 JavaScript 单元测试和模拟框架?

python - 根据列标准过滤行的问题

python - 如何打印全年日历(2019)

python - 基维 : line color in canvas won't update

python - Python 导入错误(linux)

android - 如何模拟 Retrofit Response

javascript - Apollo MockedProvider 总是返回未定义的数据字段,即使在加载之后

python - Scrapy>索引错误: list index out of range