1

我正在使用python 响应库来模拟带有请求的调用,但出现此错误:

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 参数有关吗?我错过了什么吗?

4

1 回答 1

2

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

在尝试模拟请求之前,OAuth1由于他试图访问密钥而失败,这可能在您的测试中未正确初始化。

你可以给这个键一个假值,甚至模拟OAuth1对象,responses然后就可以模拟你的调用。

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

于 2016-11-29T10:13:25.217 回答