1515import json
1616import os
1717import re
18+ import zipfile
1819from pathlib import Path
1920
2021from playwright .sync_api import Browser
@@ -33,6 +34,24 @@ def test_should_work(browser: Browser, server: Server, tmpdir: Path) -> None:
3334
3435
3536def test_should_omit_content (browser : Browser , server : Server , tmpdir : Path ) -> None :
37+ path = os .path .join (tmpdir , "log.har" )
38+ context = browser .new_context (record_har_path = path , record_har_content = "omit" )
39+ page = context .new_page ()
40+ page .goto (server .PREFIX + "/har.html" )
41+ context .close ()
42+ with open (path ) as f :
43+ data = json .load (f )
44+ assert "log" in data
45+ log = data ["log" ]
46+
47+ content1 = log ["entries" ][0 ]["response" ]["content" ]
48+ assert "text" not in content1
49+ assert "encoding" not in content1
50+
51+
52+ def test_should_omit_content_legacy (
53+ browser : Browser , server : Server , tmpdir : Path
54+ ) -> None :
3655 path = os .path .join (tmpdir , "log.har" )
3756 context = browser .new_context (record_har_path = path , record_har_omit_content = True )
3857 page = context .new_page ()
@@ -44,10 +63,67 @@ def test_should_omit_content(browser: Browser, server: Server, tmpdir: Path) ->
4463 log = data ["log" ]
4564
4665 content1 = log ["entries" ][0 ]["response" ]["content" ]
47- assert "text" in content1
66+ assert "text" not in content1
4867 assert "encoding" not in content1
4968
5069
70+ def test_should_attach_content (browser : Browser , server : Server , tmpdir : Path ) -> None :
71+ path = os .path .join (tmpdir , "log.har.zip" )
72+ context = browser .new_context (
73+ record_har_path = path ,
74+ record_har_content = "attach" ,
75+ )
76+ page = context .new_page ()
77+ page .goto (server .PREFIX + "/har.html" )
78+ page .evaluate ("() => fetch('/pptr.png').then(r => r.arrayBuffer())" )
79+ context .close ()
80+ with zipfile .ZipFile (path ) as z :
81+ with z .open ("har.har" ) as har :
82+ entries = json .load (har )["log" ]["entries" ]
83+
84+ assert "encoding" not in entries [0 ]["response" ]["content" ]
85+ assert (
86+ entries [0 ]["response" ]["content" ]["mimeType" ]
87+ == "text/html; charset=utf-8"
88+ )
89+ assert (
90+ "75841480e2606c03389077304342fac2c58ccb1b"
91+ in entries [0 ]["response" ]["content" ]["_file" ]
92+ )
93+ assert entries [0 ]["response" ]["content" ]["size" ] >= 96
94+ assert entries [0 ]["response" ]["content" ]["compression" ] == 0
95+
96+ assert "encoding" not in entries [1 ]["response" ]["content" ]
97+ assert (
98+ entries [1 ]["response" ]["content" ]["mimeType" ]
99+ == "text/css; charset=utf-8"
100+ )
101+ assert (
102+ "79f739d7bc88e80f55b9891a22bf13a2b4e18adb"
103+ in entries [1 ]["response" ]["content" ]["_file" ]
104+ )
105+ assert entries [1 ]["response" ]["content" ]["size" ] >= 37
106+ assert entries [1 ]["response" ]["content" ]["compression" ] == 0
107+
108+ assert "encoding" not in entries [2 ]["response" ]["content" ]
109+ assert entries [2 ]["response" ]["content" ]["mimeType" ] == "image/png"
110+ assert (
111+ "a4c3a18f0bb83f5d9fe7ce561e065c36205762fa"
112+ in entries [2 ]["response" ]["content" ]["_file" ]
113+ )
114+ assert entries [2 ]["response" ]["content" ]["size" ] >= 6000
115+ assert entries [2 ]["response" ]["content" ]["compression" ] == 0
116+
117+ with z .open ("75841480e2606c03389077304342fac2c58ccb1b.html" ) as f :
118+ assert b"HAR Page" in f .read ()
119+
120+ with z .open ("79f739d7bc88e80f55b9891a22bf13a2b4e18adb.css" ) as f :
121+ assert b"pink" in f .read ()
122+
123+ with z .open ("a4c3a18f0bb83f5d9fe7ce561e065c36205762fa.png" ) as f :
124+ assert len (f .read ()) == entries [2 ]["response" ]["content" ]["size" ]
125+
126+
51127def test_should_include_content (browser : Browser , server : Server , tmpdir : Path ) -> None :
52128 path = os .path .join (tmpdir , "log.har" )
53129 context = browser .new_context (record_har_path = path )
@@ -64,6 +140,41 @@ def test_should_include_content(browser: Browser, server: Server, tmpdir: Path)
64140 assert "HAR Page" in content1 ["text" ]
65141
66142
143+ def test_should_default_to_full_mode (
144+ browser : Browser , server : Server , tmpdir : Path
145+ ) -> None :
146+ path = os .path .join (tmpdir , "log.har" )
147+ context = browser .new_context (
148+ record_har_path = path ,
149+ )
150+ page = context .new_page ()
151+ page .goto (server .PREFIX + "/har.html" )
152+ context .close ()
153+ with open (path ) as f :
154+ data = json .load (f )
155+ assert "log" in data
156+ log = data ["log" ]
157+ assert log ["entries" ][0 ]["request" ]["bodySize" ] >= 0
158+
159+
160+ def test_should_support_minimal_mode (
161+ browser : Browser , server : Server , tmpdir : Path
162+ ) -> None :
163+ path = os .path .join (tmpdir , "log.har" )
164+ context = browser .new_context (
165+ record_har_path = path ,
166+ record_har_mode = "minimal" ,
167+ )
168+ page = context .new_page ()
169+ page .goto (server .PREFIX + "/har.html" )
170+ context .close ()
171+ with open (path ) as f :
172+ data = json .load (f )
173+ assert "log" in data
174+ log = data ["log" ]
175+ assert log ["entries" ][0 ]["request" ]["bodySize" ] == - 1
176+
177+
67178def test_should_filter_by_glob (browser : Browser , server : Server , tmpdir : str ) -> None :
68179 path = os .path .join (tmpdir , "log.har" )
69180 context = browser .new_context (
0 commit comments