def test_referer_follow_link(httpbin): browser = mechanicalsoup.StatefulBrowser() open_legacy_httpbin(browser, httpbin) start_url = browser.url response = browser.follow_link("/headers") referer = response.json()["headers"]["Referer"] actual_ref = re.sub('/*$', '', referer) expected_ref = re.sub('/*$', '', start_url) assert actual_ref == expected_ref
def test_referer_follow_link(httpbin): browser = mechanicalsoup.StatefulBrowser() open_legacy_httpbin(browser, httpbin) start_url = browser.get_url() response = browser.follow_link("/headers") referer = response.json()["headers"]["Referer"] actual_ref = re.sub('/*$', '', referer) expected_ref = re.sub('/*$', '', start_url) assert actual_ref == expected_ref
def test_download_link_nofile(httpbin): """Test downloading the contents of a link without saving it.""" browser = mechanicalsoup.StatefulBrowser() open_legacy_httpbin(browser, httpbin) current_url = browser.url current_page = browser.page response = browser.download_link(link='image/png') # Check that the browser state has not changed assert browser.url == current_url assert browser.page == current_page # Check that we actually downloaded a PNG file assert response.content[:4] == b'\x89PNG'
def test_download_link_nofile(httpbin): """Test downloading the contents of a link without saving it.""" browser = mechanicalsoup.StatefulBrowser() open_legacy_httpbin(browser, httpbin) current_url = browser.get_url() current_page = browser.get_current_page() response = browser.download_link(link='image/png') # Check that the browser state has not changed assert browser.get_url() == current_url assert browser.get_current_page() == current_page # Check that we actually downloaded a PNG file assert response.content[:4] == b'\x89PNG'
def test_download_link_nofile_ua(httpbin): """Test downloading the contents of a link without saving it.""" browser = mechanicalsoup.StatefulBrowser() open_legacy_httpbin(browser, httpbin) current_url = browser.url current_page = browser.page requests_kwargs = {'headers': {"User-Agent": '007'}} response = browser.download_link(link='image/png', requests_kwargs=requests_kwargs) # Check that the browser state has not changed assert browser.url == current_url assert browser.page == current_page # Check that we actually downloaded a PNG file assert response.content[:4] == b'\x89PNG' # Check that we actually set the User-agent outbound assert response.request.headers['user-agent'] == '007'
def test_download_link(httpbin): """Test downloading the contents of a link to file.""" browser = mechanicalsoup.StatefulBrowser() open_legacy_httpbin(browser, httpbin) tmpdir = tempfile.mkdtemp() tmpfile = tmpdir + '/nosuchfile.png' current_url = browser.url current_page = browser.page response = browser.download_link(file=tmpfile, link='image/png') # Check that the browser state has not changed assert browser.url == current_url assert browser.page == current_page # Check that the file was downloaded assert os.path.isfile(tmpfile) assert file_get_contents(tmpfile) == response.content # Check that we actually downloaded a PNG file assert response.content[:4] == b'\x89PNG'
def test_download_link(httpbin): """Test downloading the contents of a link to file.""" browser = mechanicalsoup.StatefulBrowser() open_legacy_httpbin(browser, httpbin) tmpdir = tempfile.mkdtemp() tmpfile = tmpdir + '/nosuchfile.png' current_url = browser.get_url() current_page = browser.get_current_page() response = browser.download_link(file=tmpfile, link='image/png') # Check that the browser state has not changed assert browser.get_url() == current_url assert browser.get_current_page() == current_page # Check that the file was downloaded assert os.path.isfile(tmpfile) assert file_get_contents(tmpfile) == response.content # Check that we actually downloaded a PNG file assert response.content[:4] == b'\x89PNG'
def test_download_link_to_existing_file(httpbin): """Test downloading the contents of a link to an existing file.""" browser = mechanicalsoup.StatefulBrowser() open_legacy_httpbin(browser, httpbin) tmpdir = tempfile.mkdtemp() tmpfile = tmpdir + '/existing.png' with open(tmpfile, "w") as f: f.write("initial content") current_url = browser.url current_page = browser.page response = browser.download_link('image/png', tmpfile) # Check that the browser state has not changed assert browser.url == current_url assert browser.page == current_page # Check that the file was downloaded assert os.path.isfile(tmpfile) assert file_get_contents(tmpfile) == response.content # Check that we actually downloaded a PNG file assert response.content[:4] == b'\x89PNG'
def test_download_link_to_existing_file(httpbin): """Test downloading the contents of a link to an existing file.""" browser = mechanicalsoup.StatefulBrowser() open_legacy_httpbin(browser, httpbin) tmpdir = tempfile.mkdtemp() tmpfile = tmpdir + '/existing.png' with open(tmpfile, "w") as f: f.write("initial content") current_url = browser.get_url() current_page = browser.get_current_page() response = browser.download_link('image/png', tmpfile) # Check that the browser state has not changed assert browser.get_url() == current_url assert browser.get_current_page() == current_page # Check that the file was downloaded assert os.path.isfile(tmpfile) assert file_get_contents(tmpfile) == response.content # Check that we actually downloaded a PNG file assert response.content[:4] == b'\x89PNG'