def test_truncated(self): url = truncatechars('http://example.com', 3) with pytest.raises(CannotFetchSource) as exc: fetch_file(url) assert exc.value.data['type'] == EventError.JS_MISSING_SOURCE assert exc.value.data['url'] == url
def test_unicode_body(self): responses.add( responses.GET, "http://example.com", body=b'"f\xc3\xb4o bar"'.decode("utf-8"), content_type="application/json; charset=utf-8", ) result = fetch_file("http://example.com") assert len(responses.calls) == 1 assert result.url == "http://example.com" assert result.body == '"f\xc3\xb4o bar"' assert result.headers == { "content-type": "application/json; charset=utf-8" } assert result.encoding == "utf-8" # ensure we use the cached result result2 = fetch_file("http://example.com") assert len(responses.calls) == 1 assert result == result2
def test_too_large_for_cache(self): # make the cache fail domain_key = http.get_domain_key("http://example.com") original_get = cache.get def cache_get(key): if key == domain_key: return original_get(key) with patch("sentry.utils.cache.cache.get", side_effect=cache_get): responses.add( responses.GET, "http://example.com", body=b"Stuff", content_type="application/json; charset=utf-8", ) with pytest.raises(http.CannotFetch) as exc: fetch_file("http://example.com") assert exc.value.data["type"] == EventError.TOO_LARGE_FOR_CACHE assert cache.get(domain_key) == { "type": "too_large_for_cache", "url": "http://example.com", }
def test_truncated(self): url = truncatechars('http://example.com', 3) with pytest.raises(http.CannotFetch) as exc: fetch_file(url) assert exc.value.data['type'] == EventError.JS_MISSING_SOURCE assert exc.value.data['url'] == url
def test_non_unicode_release_file(self, mock_fetch_release_file): mock_fetch_release_file.return_value = ( {'content-type': 'application/octet-stream'}, '\xffff', # This is some random binary data 200 ) release = Release.objects.create(project=self.project, version='1') with pytest.raises(BadSource): fetch_file('/example.js', release=release)
def test_non_url_with_release_archive(self): compressed = BytesIO() with zipfile.ZipFile(compressed, mode="w") as zip_file: zip_file.writestr("example.js", b"foo") zip_file.writestr( "manifest.json", json.dumps({ "files": { "example.js": { "url": "/example.js", "headers": { "content-type": "application/json" }, } } }), ) release = Release.objects.create( version="1", organization_id=self.project.organization_id) release.add_project(self.project) file = File.objects.create(name=RELEASE_ARCHIVE_FILENAME, ) compressed.seek(0) file.putfile(compressed) ReleaseFile.objects.create( name=RELEASE_ARCHIVE_FILENAME, release=release, organization_id=self.project.organization_id, file=file, ) with self.options({"processing.use-release-archives-sample-rate": 1.0}): # Attempt to fetch nonexisting with pytest.raises(http.BadSource): fetch_file("does-not-exist.js", release=release) # Attempt to fetch nonexsting again (to check if cache works) with pytest.raises(http.BadSource): result = fetch_file("does-not-exist.js", release=release) result = fetch_file("/example.js", release=release) assert result.url == "/example.js" assert result.body == b"foo" assert isinstance(result.body, bytes) assert result.headers == {"content-type": "application/json"} assert result.encoding == "utf-8" # Make sure cache loading works: result2 = fetch_file("/example.js", release=release) assert result2 == result
def test_connection_failure(self): responses.add(responses.GET, 'http://example.com', body=RequestException()) with pytest.raises(BadSource): fetch_file('http://example.com') assert len(responses.calls) == 1 # ensure we use the cached domain-wide failure for the second call with pytest.raises(BadSource): fetch_file('http://example.com/foo/bar') assert len(responses.calls) == 1
def test_connection_failure(self): responses.add(responses.GET, 'http://example.com', body=RequestException()) with pytest.raises(http.BadSource): fetch_file('http://example.com') assert len(responses.calls) == 1 # ensure we use the cached domain-wide failure for the second call with pytest.raises(http.BadSource): fetch_file('http://example.com/foo/bar') assert len(responses.calls) == 1
def test_with_token(self): responses.add( responses.GET, re.compile(r'http://example.com/\d+/'), body='foo bar', content_type='application/json' ) self.project.update_option('sentry:token', 'foobar') self.project.update_option('sentry:origins', ['*']) default_header_name = 'X-Sentry-Token' header_pairs = [ (None, default_header_name), ('', default_header_name), ('X-Custom-Token-Header', 'X-Custom-Token-Header'), ] for i, (header_name_option_value, expected_request_header_name) in enumerate(header_pairs): self.project.update_option('sentry:token_header', header_name_option_value) url = 'http://example.com/{}/'.format(i) result = fetch_file(url, project=self.project) assert result.url == url assert result.body == 'foo bar' assert result.headers == {'content-type': 'application/json'} assert len(responses.calls) == i + 1 assert responses.calls[i].request.headers[expected_request_header_name] == 'foobar'
def test_with_token(self): responses.add( responses.GET, re.compile(r"http://example.com/\d+/"), body="foo bar", content_type="application/json", ) self.project.update_option("sentry:token", "foobar") self.project.update_option("sentry:origins", ["*"]) default_header_name = "X-Sentry-Token" header_pairs = [ (None, default_header_name), ("", default_header_name), ("X-Custom-Token-Header", "X-Custom-Token-Header"), ] for i, (header_name_option_value, expected_request_header_name) in enumerate(header_pairs): self.project.update_option("sentry:token_header", header_name_option_value) url = u"http://example.com/{}/".format(i) result = fetch_file(url, project=self.project) assert result.url == url assert result.body == "foo bar" assert result.headers == {"content-type": "application/json"} assert len(responses.calls) == i + 1 assert responses.calls[i].request.headers[ expected_request_header_name] == "foobar"
def test_with_token(self): responses.add(responses.GET, re.compile(r'http://example.com/\d+/'), body='foo bar', content_type='application/json') self.project.update_option('sentry:token', 'foobar') self.project.update_option('sentry:origins', ['*']) default_header_name = 'X-Sentry-Token' header_pairs = [ (None, default_header_name), ('', default_header_name), ('X-Custom-Token-Header', 'X-Custom-Token-Header'), ] for i, (header_name_option_value, expected_request_header_name) in enumerate(header_pairs): self.project.update_option('sentry:token_header', header_name_option_value) url = u'http://example.com/{}/'.format(i) result = fetch_file(url, project=self.project) assert result.url == url assert result.body == 'foo bar' assert result.headers == {'content-type': 'application/json'} assert len(responses.calls) == i + 1 assert responses.calls[i].request.headers[ expected_request_header_name] == 'foobar'
def test_non_url_with_release_archive(self): compressed = BytesIO() with zipfile.ZipFile(compressed, mode="w") as zip_file: zip_file.writestr("example.js", b"foo") zip_file.writestr( "manifest.json", json.dumps({ "files": { "example.js": { "url": "/example.js", "headers": { "content-type": "application/json" }, } } }), ) release = Release.objects.create( version="1", organization_id=self.project.organization_id) release.add_project(self.project) compressed.seek(0) file_ = File.objects.create(name="foo", type="release.bundle") file_.putfile(compressed) update_artifact_index(release, None, file_) # Attempt to fetch nonexisting with pytest.raises(http.BadSource): fetch_file("does-not-exist.js", release=release) # Attempt to fetch nonexsting again (to check if cache works) with pytest.raises(http.BadSource): result = fetch_file("does-not-exist.js", release=release) result = fetch_file("/example.js", release=release) assert result.url == "/example.js" assert result.body == b"foo" assert isinstance(result.body, bytes) assert result.headers == {"content-type": "application/json"} assert result.encoding == "utf-8" # Make sure cache loading works: result2 = fetch_file("/example.js", release=release) assert result2 == result
def test_simple(self): responses.add(responses.GET, 'http://example.com', body='foo bar', content_type='application/json') result = fetch_file('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_file('http://example.com') assert len(responses.calls) == 1 assert result == result2
def test_simple(self): responses.add(responses.GET, 'http://example.com', body='foo bar', content_type='application/json') result = fetch_file('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_file('http://example.com') assert len(responses.calls) == 1 assert result == result2
def test_simple(self): responses.add( responses.GET, "http://example.com", body="foo bar", content_type="application/json" ) result = fetch_file("http://example.com") assert len(responses.calls) == 1 assert result.url == "http://example.com" assert result.body == b"foo bar" assert result.headers == {"content-type": "application/json"} # ensure we use the cached result result2 = fetch_file("http://example.com") assert len(responses.calls) == 1 assert result == result2
def test_unicode_body(self): responses.add(responses.GET, 'http://example.com', body=u'"fôo bar"'.encode('utf-8'), content_type='application/json; charset=utf-8') result = fetch_file('http://example.com') assert len(responses.calls) == 1 assert result.url == 'http://example.com' assert result.body == u'"fôo bar"' assert result.headers == {'content-type': 'application/json; charset=utf-8'} # ensure we use the cached result result2 = fetch_file('http://example.com') assert len(responses.calls) == 1 assert result == result2
def test_non_url_with_release(self, mock_fetch_release_file): mock_fetch_release_file.return_value = ({ 'content-type': 'application/json' }, 'foo', 200) release = Release.objects.create(project=self.project, version='1') result = fetch_file('/example.js', release=release) assert result.url == '/example.js' assert result.body == 'foo' assert isinstance(result.body, unicode) assert result.headers == {'content-type': 'application/json'}
def test_non_url_with_release(self, mock_fetch_release_file): mock_fetch_release_file.return_value = ( {'content-type': 'application/json'}, 'foo', 200 ) release = Release.objects.create(project=self.project, version='1') result = fetch_file('/example.js', release=release) assert result.url == '/example.js' assert result.body == 'foo' assert result.headers == {'content-type': 'application/json'}
def test_non_url_with_release(self, mock_fetch_release_file): mock_fetch_release_file.return_value = http.UrlResult( "/example.js", {"content-type": "application/json"}, b"foo", 200, None ) release = Release.objects.create(version="1", organization_id=self.project.organization_id) release.add_project(self.project) result = fetch_file("/example.js", release=release) assert result.url == "/example.js" assert result.body == b"foo" assert isinstance(result.body, six.binary_type) assert result.headers == {"content-type": "application/json"} assert result.encoding is None
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_file('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'}
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_file('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'}
def test_non_url_with_release(self, mock_fetch_release_file): mock_fetch_release_file.return_value = http.UrlResult( '/example.js', {'content-type': 'application/json'}, 'foo', 200, None, ) release = Release.objects.create(version='1', organization_id=self.project.organization_id) release.add_project(self.project) result = fetch_file('/example.js', release=release) assert result.url == '/example.js' assert result.body == 'foo' assert isinstance(result.body, six.binary_type) assert result.headers == {'content-type': 'application/json'} assert result.encoding is None
def test_non_url_with_release(self, mock_fetch_release_file): mock_fetch_release_file.return_value = http.UrlResult( '/example.js', {'content-type': 'application/json'}, 'foo', 200, None, ) release = Release.objects.create(version='1', organization_id=self.project.organization_id) release.add_project(self.project) result = fetch_file('/example.js', release=release) assert result.url == '/example.js' assert result.body == 'foo' assert isinstance(result.body, six.binary_type) assert result.headers == {'content-type': 'application/json'} assert result.encoding is None
def test_non_url_without_release(self): with pytest.raises(http.BadSource): fetch_file('/example.js')
def test_non_url_without_release(self): with pytest.raises(BadSource): fetch_file('/example.js')