def test_downloader_gist(self): test_url = "http://gist.github.com/facundobatista/6ff4f75760a9acc35e68bae8c1d7da1c" test_content = "test content of the remote script áéíóú" raw_service_response = test_content.encode("utf8") downloader = helpers._ScriptDownloader(test_url) with patch('urllib.request.urlopen') as mock_urlopen: with patch('http.client.HTTPResponse') as mock_http_response: mock_http_response.read.return_value = raw_service_response mock_urlopen.return_value = mock_http_response content = downloader.get() # check urlopen was called with the proper url, and passing correct headers headers = { 'Accept': 'text/plain', 'User-agent': helpers._ScriptDownloader.USER_AGENT, } (call,) = mock_urlopen.mock_calls (called_request,) = call[1] self.assertIsInstance(called_request, Request) self.assertEqual( called_request.full_url, "https://gist.github.com/facundobatista/6ff4f75760a9acc35e68bae8c1d7da1c/raw") self.assertEqual(called_request.headers, headers) self.assertEqual(content, test_content)
def test_downloader_raw_with_redirection(self): test_url = "http://bit.ly/will-redirect" final_url = "http://real-service.com/" raw_service_response = b"test content of the remote script" downloader = helpers._ScriptDownloader(test_url) response_contents = [ b"whatever; we don't care as we are redirectect", raw_service_response, ] with patch('urllib.request.urlopen') as mock_urlopen: with patch('http.client.HTTPResponse') as mock_http_response: mock_http_response.read.side_effect = lambda: response_contents.pop() mock_http_response.geturl.return_value = final_url mock_urlopen.return_value = mock_http_response content = downloader.get() # two calls, first to the service that will redirect us, second to the final one call1, call2 = mock_urlopen.mock_calls (called_request,) = call1[1] self.assertEqual(called_request.full_url, test_url) (called_request,) = call2[1] self.assertEqual(called_request.full_url, final_url) self.assertEqual(content, raw_service_response.decode("utf8")) self.assertLoggedInfo("Download redirect detect, now downloading from", final_url)
def test_downloader_linkode(self): test_url = "http://linkode.org/#02c5nESQBLEjgBRhUwJK74" test_content = "test content of the remote script áéíóú" raw_service_response = json.dumps({ 'content': test_content, 'morestuff': 'whocares', }).encode("utf8") downloader = helpers._ScriptDownloader(test_url) with patch('urllib.request.urlopen') as mock_urlopen: with patch('http.client.HTTPResponse') as mock_http_response: mock_http_response.read.return_value = raw_service_response mock_urlopen.return_value = mock_http_response content = downloader.get() # check urlopen was called with the proper url, and passing correct headers headers = { 'Accept': 'application/json', 'User-agent': helpers._ScriptDownloader.USER_AGENT, } (call,) = mock_urlopen.mock_calls (called_request,) = call[1] self.assertIsInstance(called_request, Request) self.assertEqual( called_request.full_url, "https://linkode.org/api/1/linkodes/02c5nESQBLEjgBRhUwJK74") self.assertEqual(called_request.headers, headers) self.assertEqual(content, test_content)
def test_downloader_pastebin(self): test_url = "http://pastebin.com/sZGwz7SL" real_url = "https://pastebin.com/raw/sZGwz7SL" test_content = "test content of the remote script áéíóú" raw_service_response = test_content.encode("utf8") downloader = helpers._ScriptDownloader(test_url) with patch('urllib.request.urlopen') as mock_urlopen: with patch('http.client.HTTPResponse') as mock_http_response: mock_http_response.read.return_value = raw_service_response mock_urlopen.return_value = mock_http_response mock_http_response.geturl.return_value = real_url content = downloader.get() # check urlopen was called with the proper url, and passing correct headers headers = { 'Accept': 'text/plain', 'User-agent': helpers._ScriptDownloader.USER_AGENT, } (call,) = mock_urlopen.mock_calls (called_request,) = call[1] self.assertIsInstance(called_request, Request) self.assertEqual(called_request.full_url, real_url) self.assertEqual(called_request.headers, headers) self.assertEqual(content, test_content)
def test_downloader_gist(self): test_url = "http://gist.github.com/facundobatista/6ff4f75760a9acc35e68bae8c1d7da1c" real_url = "https://gist.github.com/facundobatista/6ff4f75760a9acc35e68bae8c1d7da1c/raw" test_content = "test content of the remote script áéíóú" raw_service_response = test_content.encode("utf8") downloader = helpers._ScriptDownloader(test_url) with patch('urllib.request.urlopen') as mock_urlopen: with patch('http.client.HTTPResponse') as mock_http_response: mock_http_response.read.return_value = raw_service_response mock_urlopen.return_value = mock_http_response mock_http_response.geturl.return_value = real_url content = downloader.get() # check urlopen was called with the proper url, and passing correct headers headers = { 'Accept': 'text/plain', 'User-agent': helpers._ScriptDownloader.USER_AGENT, } (call,) = mock_urlopen.mock_calls (called_request,) = call[1] self.assertIsInstance(called_request, Request) self.assertEqual(called_request.full_url, real_url) self.assertEqual(called_request.headers, headers) self.assertEqual(content, test_content)
def test_downloader_raw(self): test_url = "http://scripts.com/foobar.py" raw_service_response = b"test content of the remote script" downloader = helpers._ScriptDownloader(test_url) with patch('urllib.request.urlopen') as mock_urlopen: with patch('http.client.HTTPResponse') as mock_http_response: mock_http_response.read.return_value = raw_service_response mock_urlopen.return_value = mock_http_response content = downloader.get() # check urlopen was called with the proper url, and passing correct headers headers = { 'Accept': 'text/plain', 'User-agent': helpers._ScriptDownloader.USER_AGENT, } (call,) = mock_urlopen.mock_calls (called_request,) = call[1] self.assertIsInstance(called_request, Request) self.assertEqual(called_request.full_url, test_url) self.assertEqual(called_request.headers, headers) self.assertEqual(content, raw_service_response.decode("utf8"))
def test_downloader_raw(self): test_url = "http://scripts.com/foobar.py" raw_service_response = b"test content of the remote script" downloader = helpers._ScriptDownloader(test_url) with patch('urllib.request.urlopen') as mock_urlopen: with patch('http.client.HTTPResponse') as mock_http_response: mock_http_response.read.return_value = raw_service_response mock_urlopen.return_value = mock_http_response mock_http_response.geturl.return_value = test_url content = downloader.get() # check urlopen was called with the proper url, and passing correct headers headers = { 'Accept': 'text/plain', 'User-agent': helpers._ScriptDownloader.USER_AGENT, } (call,) = mock_urlopen.mock_calls (called_request,) = call[1] self.assertIsInstance(called_request, Request) self.assertEqual(called_request.full_url, test_url) self.assertEqual(called_request.headers, headers) self.assertEqual(content, raw_service_response.decode("utf8"))
def test_decide_gist(self): url = "https://gist.github.com/facundobatista/6ff4f75760a9acc35e68bae8c1d7da1c" downloader = helpers._ScriptDownloader(url) name = downloader._decide() self.assertEqual(name, 'gist')
def test_decide_pastebin(self): url = "https://pastebin.com/sZGwz7SL" downloader = helpers._ScriptDownloader(url) name = downloader._decide() self.assertEqual(name, 'pastebin')
def test_decide_linkode(self): url = "http://linkode.org/#02c5nESQBLEjgBRhUwJK74" downloader = helpers._ScriptDownloader(url) name = downloader._decide() self.assertEqual(name, 'linkode')