44
55import pytest
66
7+ from crawlee ._types import HttpHeaders
78from crawlee ._utils .requests import compute_unique_key , normalize_url , unique_key_to_request_id
89
910if TYPE_CHECKING :
@@ -78,35 +79,80 @@ def test_normalize_url(url: str, expected_output: str, *, keep_url_fragment: boo
7879
7980
8081@pytest .mark .parametrize (
81- ('url' , 'method' , 'payload' , 'keep_url_fragment' , 'use_extended_unique_key' , 'expected_output' ),
82+ ('url' , 'method' , 'headers' , ' payload' , 'keep_url_fragment' , 'use_extended_unique_key' , 'expected_output' ),
8283 [
83- ('http://example.com' , 'GET' , None , False , False , 'http://example.com' ),
84- ('http://example.com' , 'POST' , None , False , False , 'http://example.com' ),
85- ('http://example.com' , 'GET' , 'data' , False , False , 'http://example.com' ),
86- ('http://example.com' , 'GET' , 'data' , False , True , 'GET(3a6eb079):http://example.com' ),
87- ('http://example.com' , 'POST' , 'data' , False , True , 'POST(3a6eb079):http://example.com' ),
88- ('http://example.com#fragment' , 'GET' , None , True , False , 'http://example.com#fragment' ),
89- ('http://example.com#fragment' , 'GET' , None , False , False , 'http://example.com' ),
90- ('http://example.com' , 'DELETE' , 'test' , False , True , 'DELETE(9f86d081):http://example.com' ),
91- ('https://example.com?utm_content=test' , 'GET' , None , False , False , 'https://example.com' ),
92- ('https://example.com?utm_content=test' , 'GET' , None , True , False , 'https://example.com' ),
84+ ('http://example.com' , 'GET' , None , None , False , False , 'http://example.com' ),
85+ ('http://example.com' , 'POST' , None , None , False , False , 'http://example.com' ),
86+ ('http://example.com' , 'GET' , None , 'data' , False , False , 'http://example.com' ),
87+ (
88+ 'http://example.com' ,
89+ 'GET' ,
90+ None ,
91+ 'data' ,
92+ False ,
93+ True ,
94+ 'GET|e3b0c442|3a6eb079|http://example.com' ,
95+ ),
96+ (
97+ 'http://example.com' ,
98+ 'POST' ,
99+ HttpHeaders ({'Content-Type' : 'application/json' }),
100+ 'data' ,
101+ False ,
102+ True ,
103+ 'POST|60d83e70|3a6eb079|http://example.com' ,
104+ ),
105+ (
106+ 'http://example.com' ,
107+ 'POST' ,
108+ HttpHeaders ({'Content-Type' : 'application/json' , 'Custom-Header' : 'should be ignored' }),
109+ 'data' ,
110+ False ,
111+ True ,
112+ 'POST|60d83e70|3a6eb079|http://example.com' ,
113+ ),
114+ ('http://example.com#fragment' , 'GET' , None , None , True , False , 'http://example.com#fragment' ),
115+ ('http://example.com#fragment' , 'GET' , None , None , False , False , 'http://example.com' ),
116+ (
117+ 'http://example.com' ,
118+ 'DELETE' ,
119+ None ,
120+ 'test' ,
121+ False ,
122+ True ,
123+ 'DELETE|e3b0c442|9f86d081|http://example.com' ,
124+ ),
125+ ('https://example.com?utm_content=test' , 'GET' , None , None , False , False , 'https://example.com' ),
126+ ('https://example.com?utm_content=test' , 'GET' , None , None , True , False , 'https://example.com' ),
127+ (
128+ 'http://example.com' ,
129+ 'GET' ,
130+ HttpHeaders ({'Accept' : 'text/html' }),
131+ None ,
132+ False ,
133+ True ,
134+ 'GET|f1614162|e3b0c442|http://example.com' ,
135+ ),
93136 ],
94137 ids = [
95138 'simple_get' ,
96139 'simple_post' ,
97140 'get_with_payload' ,
98141 'get_with_payload_extended' ,
99142 'post_with_payload_extended' ,
143+ 'post_with_payload_and_headers' ,
100144 'get_with_fragment' ,
101145 'get_remove_fragment' ,
102146 'delete_with_payload_extended' ,
103147 'get_remove_utm' ,
104148 'get_keep_utm_fragment' ,
149+ 'get_with_headers_extended' ,
105150 ],
106151)
107152def test_compute_unique_key (
108153 url : str ,
109154 method : HttpMethod ,
155+ headers : HttpHeaders | None ,
110156 payload : HttpPayload | None ,
111157 * ,
112158 keep_url_fragment : bool ,
@@ -115,8 +161,9 @@ def test_compute_unique_key(
115161) -> None :
116162 output = compute_unique_key (
117163 url ,
118- method ,
119- payload ,
164+ method = method ,
165+ headers = headers ,
166+ payload = payload ,
120167 keep_url_fragment = keep_url_fragment ,
121168 use_extended_unique_key = use_extended_unique_key ,
122169 )
0 commit comments