question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@mock_s3 permanently breaks requests library

See original GitHub issue

At first look, I thought this was a duplicate of https://github.com/spulec/moto/issues/303.

However, on further inspection, the problem is only present with the mock_s3 decorator, not with the mock_s3_deprecated decorator. Therefore, if my inspection of the code base is right, this looks like a problem related to the responses library or the use of it in moto, whereas https://github.com/spulec/moto/issues/303 concerned the HTTPretty library.

Anyway, the meat of the issue:

After using @mock_s3 in django tests, other test classes that use LiveserverTestCase and the requests library stop working, and fail with the error: ConnectionError: Connection refused.

This is not an inherent problem with responses itself, as the following test demonstrates:

python -c "from moto.packages.responses.responses import mock; import requests; mock.start(); mock.stop(); print requests.get('http://google.com')"
<Response [200]>

However, if mock.stop() is omitted, you see the same error as in the django test:

python -c "from moto.packages.responses.responses import mock; import requests; mock.start(); print requests.get('http://google.com')"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/REDACTED/env/local/lib/python2.7/site-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/home/REDACTED/env/local/lib/python2.7/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/REDACTED/env/local/lib/python2.7/site-packages/requests/sessions.py", line 502, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/REDACTED/env/local/lib/python2.7/site-packages/requests/sessions.py", line 612, in send
    r = adapter.send(request, **kwargs)
  File "/home/REDACTED/env/local/lib/python2.7/site-packages/moto/packages/responses/responses.py", line 302, in unbound_on_send
    return self._on_request(adapter, request, *a, **kwargs)
  File "/home/REDACTED/env/local/lib/python2.7/site-packages/moto/packages/responses/responses.py", line 244, in _on_request
    raise response
requests.exceptions.ConnectionError: Connection refused: GET http://google.com/

This suggests to me that, one way or another, moto is failing to call mock.stop().

moto version 1.0.1 Django version 1.11.3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:18
  • Comments:31 (9 by maintainers)

github_iconTop GitHub Comments

14reactions
JoostvDoorncommented, Apr 10, 2018

@yhvh A workaround for this for now is:

import requests
import responses
import moto
import unittest

class TestRequests(unittest.TestCase):
    @moto.mock_s3
    def test_requests(self):
        responses.add_passthru('https://')
        response = requests.get('https://www.google.com')
        self.assertEqual(response.status_code, 200)

unittest.main()
5reactions
kingbuzzmancommented, Sep 20, 2018

As of moto 1.3.5 this works:

    import responses

    responses.add_passthru('https://')
    responses.add_passthru('http://')
Read more comments on GitHub >

github_iconTop Results From Across the Web

"3 ways to test S3 in Python" - Sanjay Siddhanti ... - YouTube
Sanjay Siddhantihttps://2020.pycon.org.au/program/VXXUWE- Benefits of testing code that uses S3 - In-depth code examples of three ways to ...
Read more >
Amazon Simple Storage Service (S3) - AWS
Q: What is Amazon S3? Amazon S3 is object storage built to store and retrieve any amount of data from anywhere. It's a...
Read more >
How to mock S3 services in Python tests | by Lorenzo Peppoloni
Unit testing: we will use the unittest module to write out tests; AWS interaction: we will use the boto3 module to interact with...
Read more >
Mocking AWS with Jest (and TypeScript) - DEV Community ‍ ‍
Eventually tests break, get skipped or become permanent TODOs. ... Jest mentions a DynamoDB mocking library right in their docs.
Read more >
amazon s3 developer free account for testing purposes
Any reliable library to ease operations using .NET? And what it'd be if while transferring a file, the connection to amazon s3 server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found