aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@corte.si>2017-12-12 09:10:05 +1300
committerGitHub <noreply@github.com>2017-12-12 09:10:05 +1300
commit25cf3db65853d66832e8bef81e7a431181172bd2 (patch)
tree170034aac0b28e93dfa7e6735b873593d5a5a30a
parentb09c28d8a6fcd2af4a43b968b800d44c5a8c836e (diff)
parent4912920573c71a49670928b19893b965d199f836 (diff)
downloadmitmproxy-25cf3db65853d66832e8bef81e7a431181172bd2.tar.gz
mitmproxy-25cf3db65853d66832e8bef81e7a431181172bd2.tar.bz2
mitmproxy-25cf3db65853d66832e8bef81e7a431181172bd2.zip
Merge pull request #2661 from mhils/browser-win
Fix browser addon on Windows
-rw-r--r--mitmproxy/addons/browser.py30
-rw-r--r--test/mitmproxy/addons/test_browser.py13
2 files changed, 33 insertions, 10 deletions
diff --git a/mitmproxy/addons/browser.py b/mitmproxy/addons/browser.py
index 6e8b2585..247c356b 100644
--- a/mitmproxy/addons/browser.py
+++ b/mitmproxy/addons/browser.py
@@ -1,15 +1,27 @@
+import shutil
import subprocess
-import sys
import tempfile
+import typing
from mitmproxy import command
from mitmproxy import ctx
-platformPaths = {
- "linux": "google-chrome",
- "win32": "chrome.exe",
- "darwin": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
-}
+
+def get_chrome_executable() -> typing.Optional[str]:
+ for browser in (
+ "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
+ # https://stackoverflow.com/questions/40674914/google-chrome-path-in-windows-10
+ r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe",
+ r"C:\Program Files (x86)\Google\Application\chrome.exe",
+ # Linux binary names from Python's webbrowser module.
+ "google-chrome",
+ "chrome",
+ "chromium",
+ "chromium-browser",
+ ):
+ if shutil.which(browser):
+ return browser
+ return None
class Browser:
@@ -29,8 +41,8 @@ class Browser:
else:
self.done()
- cmd = platformPaths.get(sys.platform)
- if not cmd: # pragma: no cover
+ cmd = get_chrome_executable()
+ if not cmd:
ctx.log.alert("Your platform is not supported yet - please submit a patch.")
return
@@ -59,4 +71,4 @@ class Browser:
self.browser.kill()
self.tdir.cleanup()
self.browser = None
- self.tdir = None \ No newline at end of file
+ self.tdir = None
diff --git a/test/mitmproxy/addons/test_browser.py b/test/mitmproxy/addons/test_browser.py
index d1b32186..407a3fe6 100644
--- a/test/mitmproxy/addons/test_browser.py
+++ b/test/mitmproxy/addons/test_browser.py
@@ -5,7 +5,8 @@ from mitmproxy.test import taddons
def test_browser():
- with mock.patch("subprocess.Popen") as po:
+ with mock.patch("subprocess.Popen") as po, mock.patch("shutil.which") as which:
+ which.return_value = "chrome"
b = browser.Browser()
with taddons.context() as tctx:
b.start()
@@ -18,3 +19,13 @@ def test_browser():
assert tctx.master.has_log("already running")
b.done()
assert not b.browser
+
+
+def test_no_browser():
+ with mock.patch("shutil.which") as which:
+ which.return_value = False
+
+ b = browser.Browser()
+ with taddons.context() as tctx:
+ b.start()
+ assert tctx.master.has_log("platform is not supported")