Ejemplo n.º 1
0
    def test_connection_failure(self):
        responses.add(responses.GET, 'http://example.com', body=RequestException())

        with pytest.raises(BadSource):
            result = fetch_url('http://example.com')

        assert len(responses.calls) == 1

        # ensure we use the cached domain-wide failure for the second call
        with pytest.raises(BadSource):
            result = fetch_url('http://example.com/foo/bar')

        assert len(responses.calls) == 1
Ejemplo n.º 2
0
    def test_connection_failure(self):
        responses.add(responses.GET, 'http://example.com', body=RequestException())

        with pytest.raises(BadSource):
            result = fetch_url('http://example.com')

        assert len(responses.calls) == 1

        # ensure we use the cached domain-wide failure for the second call
        with pytest.raises(BadSource):
            result = fetch_url('http://example.com/foo/bar')

        assert len(responses.calls) == 1
Ejemplo n.º 3
0
    def test_simple(self):
        responses.add(responses.GET, 'http://example.com', body='foo bar',
                      content_type='application/json')

        result = fetch_url('http://example.com')

        assert len(responses.calls) == 1

        assert result.url == 'http://example.com'
        assert result.body == 'foo bar'
        assert result.headers == {'content-type': 'application/json'}

        # ensure we use the cached result
        result2 = fetch_url('http://example.com')

        assert len(responses.calls) == 1

        assert result == result2
Ejemplo n.º 4
0
    def test_simple(self):
        responses.add(responses.GET, 'http://example.com', body='foo bar',
                      content_type='application/json')

        result = fetch_url('http://example.com')

        assert len(responses.calls) == 1

        assert result.url == 'http://example.com'
        assert result.body == 'foo bar'
        assert result.headers == {'content-type': 'application/json'}

        # ensure we use the cached result
        result2 = fetch_url('http://example.com')

        assert len(responses.calls) == 1

        assert result == result2
Ejemplo n.º 5
0
    def test_connection_failure(self, safe_urlread, safe_urlopen):
        safe_urlopen.side_effect = Exception()

        result = fetch_url('http://example.com')

        safe_urlopen.assert_called_once_with(
            'http://example.com', allow_redirects=True, timeout=5,
            headers=[], verify_ssl=False)
        assert not safe_urlread.mock_calls

        assert result == BAD_SOURCE

        # ensure we use the cached domain-wide failure for the second call
        result = fetch_url('http://example.com/foo/bar')

        safe_urlopen.assert_called_once()

        assert result == BAD_SOURCE
Ejemplo n.º 6
0
    def test_read_failure(self, safe_urlread, safe_urlopen):
        safe_urlopen.return_value.headers = (('content-type', 'application/json'),)
        safe_urlread.side_effect = Exception()

        result = fetch_url('http://example.com')

        safe_urlopen.assert_called_once_with(
            'http://example.com', allow_redirects=True, timeout=5,
            headers=[], verify_ssl=False)
        safe_urlread.assert_called_once_with(safe_urlopen.return_value)

        assert result == BAD_SOURCE

        # ensure we use the cached failure for the second call
        result = fetch_url('http://example.com')

        safe_urlopen.assert_called_once()

        assert result == BAD_SOURCE
Ejemplo n.º 7
0
    def test_simple(self, safe_urlread, safe_urlopen):
        safe_urlopen.return_value.headers = (('content-type', 'application/json'),)
        safe_urlread.return_value = u'foo bar'

        result = fetch_url('http://example.com')

        safe_urlopen.assert_called_once_with(
            'http://example.com', allow_redirects=True, timeout=5,
            headers=[], verify_ssl=False)
        safe_urlread.assert_called_once_with(safe_urlopen.return_value)

        assert result.url == 'http://example.com'
        assert result.body == u'foo bar'
        assert result.headers == {'content-type': 'application/json'}

        # ensure we use the cached result
        result2 = fetch_url('http://example.com')

        safe_urlopen.assert_called_once()

        assert result == result2
Ejemplo n.º 8
0
    def test_with_token(self):
        responses.add(responses.GET, 'http://example.com', body='foo bar',
                      content_type='application/json')

        self.project.update_option('sentry:token', 'foobar')
        self.project.update_option('sentry:origins', ['*'])

        result = fetch_url('http://example.com', project=self.project)

        assert len(responses.calls) == 1
        assert responses.calls[0].request.headers['X-Sentry-Token'] == 'foobar'

        assert result.url == 'http://example.com'
        assert result.body == 'foo bar'
        assert result.headers == {'content-type': 'application/json'}
Ejemplo n.º 9
0
    def test_with_token(self):
        responses.add(responses.GET, 'http://example.com', body='foo bar',
                      content_type='application/json')

        self.project.update_option('sentry:token', 'foobar')
        self.project.update_option('sentry:origins', ['*'])

        result = fetch_url('http://example.com', project=self.project)

        assert len(responses.calls) == 1
        assert responses.calls[0].request.headers['X-Sentry-Token'] == 'foobar'

        assert result.url == 'http://example.com'
        assert result.body == 'foo bar'
        assert result.headers == {'content-type': 'application/json'}
Ejemplo n.º 10
0
    def test_with_token(self, safe_urlread, safe_urlopen):
        self.project.update_option('sentry:token', 'foobar')
        self.project.update_option('sentry:origins', ['*'])

        safe_urlopen.return_value.headers = (('content-type', 'application/json'),)
        safe_urlread.return_value = u'foo bar'

        result = fetch_url('http://example.com', project=self.project)

        safe_urlopen.assert_called_once_with(
            'http://example.com', allow_redirects=True, timeout=5,
            headers=[('X-Sentry-Token', 'foobar')], verify_ssl=False)
        safe_urlread.assert_called_once_with(safe_urlopen.return_value)

        assert result.url == 'http://example.com'
        assert result.body == u'foo bar'
        assert result.headers == {'content-type': 'application/json'}

        # ensure we use the cached result
        result2 = fetch_url('http://example.com')

        safe_urlopen.assert_called_once()

        assert result == result2