1111from crawlee ._types import HttpHeaders , HttpMethod
1212from crawlee .http_clients import HttpResponse , HttpxHttpClient
1313
14- from apify .storages ._actor_inputs import URL_NO_COMMAS_REGEX , ActorInputKeys , Input
14+ from apify .storages ._actor_inputs import URL_NO_COMMAS_REGEX , ActorInputKeys , create_request_list
1515
1616
1717@pytest .mark .parametrize ('request_method' , get_args (HttpMethod ))
@@ -36,24 +36,22 @@ async def test_actor_create_request_list_request_types(
3636 ActorInputKeys .startUrls .method : request_method ,
3737 }
3838 request_dict_input = {** minimal_request_dict_input , ** optional_input }
39- example_actor_input : dict [str , Any ] = {ActorInputKeys .startUrls : [request_dict_input ]}
4039
41- generated_input = await Input .read (example_actor_input )
40+ request_list = await create_request_list ([request_dict_input ])
41+ assert not await request_list .is_empty ()
42+ request = await request_list .fetch_next_request ()
43+ assert request is not None
44+ assert await request_list .is_empty ()
4245
43- assert not await generated_input .start_urls .is_empty ()
44- generated_request = await generated_input .start_urls .fetch_next_request ()
45- assert generated_request is not None
46- assert await generated_input .start_urls .is_empty ()
47-
48- assert generated_request .method == request_dict_input [ActorInputKeys .startUrls .method ]
49- assert generated_request .url == request_dict_input [ActorInputKeys .startUrls .url ]
50- assert generated_request .payload == request_dict_input .get (ActorInputKeys .startUrls .payload , '' ).encode ('utf-8' )
46+ assert request .method == request_dict_input [ActorInputKeys .startUrls .method ]
47+ assert request .url == request_dict_input [ActorInputKeys .startUrls .url ]
48+ assert request .payload == request_dict_input .get (ActorInputKeys .startUrls .payload , '' ).encode ('utf-8' )
5149 expected_user_data = UserData ()
5250 if ActorInputKeys .startUrls .userData in optional_input :
5351 for key , value in optional_input [ActorInputKeys .startUrls .userData ].items ():
5452 expected_user_data [key ] = value
55- assert generated_request .user_data == expected_user_data
56- assert generated_request .headers .root == optional_input .get (ActorInputKeys .startUrls .headers , {})
53+ assert request .user_data == expected_user_data
54+ assert request .headers .root == optional_input .get (ActorInputKeys .startUrls .headers , {})
5755
5856
5957def _create_dummy_response (read_output : Iterator [str ]) -> HttpResponse :
@@ -80,39 +78,37 @@ def read(self) -> bytes:
8078
8179async def test_actor_create_request_list_from_url_correctly_send_requests () -> None :
8280 """Test that injected HttpClient's method send_request is called with properly passed arguments."""
83- example_actor_input : dict [str , Any ] = {
84- ActorInputKeys .startUrls : [
85- {
86- ActorInputKeys .startUrls .requestsFromUrl : 'https://abc.dev/file.txt' ,
87- ActorInputKeys .startUrls .method : 'GET' ,
88- },
89- {
90- ActorInputKeys .startUrls .requestsFromUrl : 'https://www.abc.dev/file2' ,
91- ActorInputKeys .startUrls .method : 'PUT' ,
92- },
93- {
94- ActorInputKeys .startUrls .requestsFromUrl : 'https://www.something.som' ,
95- ActorInputKeys .startUrls .method : 'POST' ,
96- ActorInputKeys .startUrls .headers : {'key' : 'value' },
97- ActorInputKeys .startUrls .payload : 'some_payload' ,
98- ActorInputKeys .startUrls .userData : {'another_key' : 'another_value' },
99- },
100- ]
101- }
81+ actor_start_urls_input : list [dict [str , Any ]] = [
82+ {
83+ ActorInputKeys .startUrls .requestsFromUrl : 'https://abc.dev/file.txt' ,
84+ ActorInputKeys .startUrls .method : 'GET' ,
85+ },
86+ {
87+ ActorInputKeys .startUrls .requestsFromUrl : 'https://www.abc.dev/file2' ,
88+ ActorInputKeys .startUrls .method : 'PUT' ,
89+ },
90+ {
91+ ActorInputKeys .startUrls .requestsFromUrl : 'https://www.something.som' ,
92+ ActorInputKeys .startUrls .method : 'POST' ,
93+ ActorInputKeys .startUrls .headers : {'key' : 'value' },
94+ ActorInputKeys .startUrls .payload : 'some_payload' ,
95+ ActorInputKeys .startUrls .userData : {'another_key' : 'another_value' },
96+ },
97+ ]
10298
103- mocked_read_outputs = ('' for url in example_actor_input [ ActorInputKeys . startUrls ] )
99+ mocked_read_outputs = ('' for url in actor_start_urls_input )
104100 http_client = HttpxHttpClient ()
105101 with mock .patch .object (
106102 http_client , 'send_request' , return_value = _create_dummy_response (mocked_read_outputs )
107103 ) as mocked_send_request :
108- await Input . read ( example_actor_input , http_client = http_client )
104+ await create_request_list ( actor_start_urls_input , http_client = http_client )
109105
110106 expected_calls = [
111107 call (
112108 method = 'GET' ,
113109 url = example_input [ActorInputKeys .startUrls .requestsFromUrl ],
114110 )
115- for example_input in example_actor_input [ ActorInputKeys . startUrls ]
111+ for example_input in actor_start_urls_input
116112 ]
117113 mocked_send_request .assert_has_calls (expected_calls )
118114
@@ -130,25 +126,23 @@ async def test_actor_create_request_list_from_url() -> None:
130126 )
131127 )
132128
133- example_actor_input : dict [str , Any ] = {
134- ActorInputKeys .startUrls : [
135- {
136- ActorInputKeys .startUrls .requestsFromUrl : 'https://abc.dev/file.txt' ,
137- ActorInputKeys .startUrls .method : 'GET' ,
138- },
139- {ActorInputKeys .startUrls .url : expected_simple_url , ActorInputKeys .startUrls .method : 'GET' },
140- {
141- ActorInputKeys .startUrls .requestsFromUrl : 'https://www.abc.dev/file2' ,
142- ActorInputKeys .startUrls .method : 'GET' ,
143- },
144- ]
145- }
129+ actor_start_urls_input = [
130+ {
131+ ActorInputKeys .startUrls .requestsFromUrl : 'https://abc.dev/file.txt' ,
132+ ActorInputKeys .startUrls .method : 'GET' ,
133+ },
134+ {ActorInputKeys .startUrls .url : expected_simple_url , ActorInputKeys .startUrls .method : 'GET' },
135+ {
136+ ActorInputKeys .startUrls .requestsFromUrl : 'https://www.abc.dev/file2' ,
137+ ActorInputKeys .startUrls .method : 'GET' ,
138+ },
139+ ]
146140
147141 http_client = HttpxHttpClient ()
148142 with mock .patch .object (http_client , 'send_request' , return_value = _create_dummy_response (response_bodies )):
149- generated_input = await Input . read ( example_actor_input , http_client = http_client )
143+ request_list = await create_request_list ( actor_start_urls_input , http_client = http_client )
150144 generated_requests = []
151- while request := await generated_input . start_urls .fetch_next_request ():
145+ while request := await request_list .fetch_next_request ():
152146 generated_requests .append (request )
153147
154148 # Check correctly created requests' urls in request list
@@ -158,29 +152,28 @@ async def test_actor_create_request_list_from_url() -> None:
158152async def test_actor_create_request_list_from_url_additional_inputs () -> None :
159153 """Test that all generated request properties are correctly populated from input values."""
160154 expected_simple_url = 'https://www.someurl.com'
161- example_start_url_input = {
155+ example_start_url_input : dict [ str , Any ] = {
162156 ActorInputKeys .startUrls .requestsFromUrl : 'https://crawlee.dev/file.txt' ,
163157 ActorInputKeys .startUrls .method : 'POST' ,
164158 ActorInputKeys .startUrls .headers : {'key' : 'value' },
165159 ActorInputKeys .startUrls .payload : 'some_payload' ,
166160 ActorInputKeys .startUrls .userData : {'another_key' : 'another_value' },
167161 }
168- example_actor_input : dict [ str , Any ] = { ActorInputKeys . startUrls : [ example_start_url_input ]}
162+
169163 response_bodies = iter ((expected_simple_url ,))
170164 http_client = HttpxHttpClient ()
171165 with mock .patch .object (http_client , 'send_request' , return_value = _create_dummy_response (response_bodies )):
172- generated_input = await Input . read ( example_actor_input , http_client = http_client )
173- request = await generated_input . start_urls .fetch_next_request ()
166+ request_list = await create_request_list ([ example_start_url_input ] , http_client = http_client )
167+ request = await request_list .fetch_next_request ()
174168
175169 # Check all properties correctly created for request
176- example_start_url_input = example_actor_input [ActorInputKeys .startUrls ][0 ]
177170 assert request
178171 assert request .url == expected_simple_url
179172 assert request .method == example_start_url_input [ActorInputKeys .startUrls .method ]
180173 assert request .headers .root == example_start_url_input [ActorInputKeys .startUrls .headers ]
181174 assert request .payload == str (example_start_url_input [ActorInputKeys .startUrls .payload ]).encode ('utf-8' )
182175 expected_user_data = UserData ()
183- for key , value in example_actor_input [ ActorInputKeys . startUrls ][ 0 ] [ActorInputKeys .startUrls .userData ].items ():
176+ for key , value in example_start_url_input [ActorInputKeys .startUrls .userData ].items ():
184177 expected_user_data [key ] = value
185178 assert request .user_data == expected_user_data
186179
0 commit comments