tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_recording.py (2119B)


      1 #!/usr/bin/env python
      2 import datetime
      3 import os
      4 from builtins import Exception
      5 
      6 import mozinfo
      7 import mozunit
      8 import requests
      9 from mozproxy import get_playback
     10 from support import tempdir
     11 
     12 here = os.path.dirname(__file__)
     13 os.environ["MOZPROXY_DIR"] = os.path.join(here, "files")
     14 
     15 
     16 def get_status_code(url, playback):
     17    response = requests.get(
     18        url=url, proxies={"http": "http://%s:%s/" % (playback.host, playback.port)}
     19    )
     20    return response.status_code
     21 
     22 
     23 def test_record_and_replay(*args):
     24    # test setup
     25 
     26    basename = "recording"
     27    suffix = datetime.datetime.now().strftime("%y%m%d_%H%M%S")
     28    filename = "_".join([basename, suffix])
     29    recording_file = os.path.join(here, "files", ".".join([filename, "zip"]))
     30 
     31    # Record part
     32    config = {
     33        "playback_tool": "mitmproxy",
     34        "recording_file": recording_file,
     35        "playback_version": "8.1.1",
     36        "platform": mozinfo.os,
     37        "run_local": "MOZ_AUTOMATION" not in os.environ,
     38        "binary": "firefox",
     39        "app": "firefox",
     40        "host": "127.0.0.1",
     41        "record": True,
     42    }
     43 
     44    with tempdir() as obj_path:
     45        config["obj_path"] = obj_path
     46        record = get_playback(config)
     47        assert record is not None
     48 
     49        try:
     50            record.start()
     51 
     52            url = "https://m.media-amazon.com/images/G/01/csm/showads.v2.js"
     53            assert get_status_code(url, record) == 200
     54        finally:
     55            record.stop()
     56 
     57        # playback part
     58        config["record"] = False
     59        config["recording_file"] = None
     60        config["playback_files"] = [recording_file]
     61        playback = get_playback(config)
     62        assert playback is not None
     63        try:
     64            playback.start()
     65 
     66            url = "https://m.media-amazon.com/images/G/01/csm/showads.v2.js"
     67            assert get_status_code(url, playback) == 200
     68        finally:
     69            playback.stop()
     70 
     71    # Cleanup
     72    try:
     73        os.remove(recording_file)
     74        os.remove(os.path.join("distribution", "policies.json"))
     75    except Exception:
     76        pass
     77 
     78 
     79 if __name__ == "__main__":
     80    mozunit.main(runwith="pytest")