|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import base64 |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from playwright.async_api import Error |
| 20 | + |
| 21 | + |
| 22 | +async def test_should_throw_for_bad_server_value(browser_factory): |
| 23 | + with pytest.raises(Error) as exc_info: |
| 24 | + await browser_factory(proxy={"server": 123}) |
| 25 | + assert "proxy.server: expected string, got number" in exc_info.value.message |
| 26 | + |
| 27 | + |
| 28 | +async def test_should_use_proxy(browser_factory, server): |
| 29 | + server.set_route( |
| 30 | + "/target.html", |
| 31 | + lambda r: ( |
| 32 | + r.write(b"<html><title>Served by the proxy</title></html>"), |
| 33 | + r.finish(), |
| 34 | + ), |
| 35 | + ) |
| 36 | + browser = await browser_factory(proxy={"server": f"localhost:{server.PORT}"}) |
| 37 | + page = await browser.new_page() |
| 38 | + await page.goto("http://non-existent.com/target.html") |
| 39 | + assert await page.title() == "Served by the proxy" |
| 40 | + |
| 41 | + |
| 42 | +async def test_should_use_proxy_for_second_page(browser_factory, server): |
| 43 | + server.set_route( |
| 44 | + "/target.html", |
| 45 | + lambda r: ( |
| 46 | + r.write(b"<html><title>Served by the proxy</title></html>"), |
| 47 | + r.finish(), |
| 48 | + ), |
| 49 | + ) |
| 50 | + browser = await browser_factory(proxy={"server": f"localhost:{server.PORT}"}) |
| 51 | + |
| 52 | + page1 = await browser.new_page() |
| 53 | + await page1.goto("http://non-existent.com/target.html") |
| 54 | + assert await page1.title() == "Served by the proxy" |
| 55 | + |
| 56 | + page2 = await browser.new_page() |
| 57 | + await page2.goto("http://non-existent.com/target.html") |
| 58 | + assert await page2.title() == "Served by the proxy" |
| 59 | + |
| 60 | + |
| 61 | +async def test_should_work_with_ip_port_notion(browser_factory, server): |
| 62 | + server.set_route( |
| 63 | + "/target.html", |
| 64 | + lambda r: ( |
| 65 | + r.write(b"<html><title>Served by the proxy</title></html>"), |
| 66 | + r.finish(), |
| 67 | + ), |
| 68 | + ) |
| 69 | + browser = await browser_factory(proxy={"server": f"127.0.0.1:{server.PORT}"}) |
| 70 | + page = await browser.new_page() |
| 71 | + await page.goto("http://non-existent.com/target.html") |
| 72 | + assert await page.title() == "Served by the proxy" |
| 73 | + |
| 74 | + |
| 75 | +async def test_should_authenticate(browser_factory, server): |
| 76 | + def handler(req): |
| 77 | + print(req) |
| 78 | + auth = req.getHeader("proxy-authorization") |
| 79 | + if not auth: |
| 80 | + req.setHeader( |
| 81 | + b"Proxy-Authenticate", b'Basic realm="Access to internal site"' |
| 82 | + ) |
| 83 | + req.setResponseCode(407) |
| 84 | + else: |
| 85 | + req.write(f"<html><title>{auth}</title></html>".encode("utf-8")) |
| 86 | + req.finish() |
| 87 | + |
| 88 | + server.set_route("/target.html", handler) |
| 89 | + |
| 90 | + browser = await browser_factory( |
| 91 | + proxy={ |
| 92 | + "server": f"localhost:{server.PORT}", |
| 93 | + "username": "user", |
| 94 | + "password": "secret", |
| 95 | + } |
| 96 | + ) |
| 97 | + page = await browser.new_page() |
| 98 | + await page.goto("http://non-existent.com/target.html") |
| 99 | + assert await page.title() == "Basic " + base64.b64encode(b"user:secret").decode( |
| 100 | + "utf-8" |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +async def test_should_authenticate_with_empty_password(browser_factory, server): |
| 105 | + def handler(req): |
| 106 | + print(req) |
| 107 | + auth = req.getHeader("proxy-authorization") |
| 108 | + if not auth: |
| 109 | + req.setHeader( |
| 110 | + b"Proxy-Authenticate", b'Basic realm="Access to internal site"' |
| 111 | + ) |
| 112 | + req.setResponseCode(407) |
| 113 | + else: |
| 114 | + req.write(f"<html><title>{auth}</title></html>".encode("utf-8")) |
| 115 | + req.finish() |
| 116 | + |
| 117 | + server.set_route("/target.html", handler) |
| 118 | + |
| 119 | + browser = await browser_factory( |
| 120 | + proxy={"server": f"localhost:{server.PORT}", "username": "user", "password": ""} |
| 121 | + ) |
| 122 | + page = await browser.new_page() |
| 123 | + await page.goto("http://non-existent.com/target.html") |
| 124 | + assert await page.title() == "Basic " + base64.b64encode(b"user:").decode("utf-8") |
0 commit comments