@@ -157,230 +157,4 @@ def create_simple_host(access_token: str, inbound_tag: str, *, remark: str, prio
157157 json = payload ,
158158 )
159159 assert response .status_code == status .HTTP_201_CREATED
160- return response .json ()["id" ]
161-
162-
163- def test_get_hosts_simple_basic (access_token ):
164- """Test that hosts/simple returns correct minimal data structure."""
165- core = create_core (access_token )
166- inbounds = get_inbounds (access_token )
167- assert inbounds , "No inbounds available for host creation"
168- created_ids = []
169- created_remarks = []
170- try :
171- for i in range (3 ):
172- remark = unique_name (f"host_simple_{ i } " )
173- host_id = create_simple_host (access_token , inbounds [i % len (inbounds )], remark = remark , priority = i + 1 )
174- created_ids .append (host_id )
175- created_remarks .append (remark )
176-
177- response = client .get (
178- "/api/hosts/simple" ,
179- headers = {"Authorization" : f"Bearer { access_token } " },
180- )
181- assert response .status_code == status .HTTP_200_OK
182- data = response .json ()
183- assert "hosts" in data
184- assert "total" in data
185-
186- for host in data ["hosts" ]:
187- assert set (host .keys ()) == {"id" , "remark" , "address" , "port" , "inbound_tag" , "priority" }
188-
189- response_remarks = [h ["remark" ] for h in data ["hosts" ]]
190- for remark in created_remarks :
191- assert remark in response_remarks
192- finally :
193- for host_id in created_ids :
194- client .delete (f"/api/host/{ host_id } " , headers = {"Authorization" : f"Bearer { access_token } " })
195- delete_core (access_token , core ["id" ])
196-
197-
198- def test_get_hosts_simple_search (access_token ):
199- """Test case-insensitive search by remark."""
200- core = create_core (access_token )
201- inbounds = get_inbounds (access_token )
202- assert inbounds , "No inbounds available for host creation"
203- created_ids = []
204- try :
205- remark_alpha = unique_name ("host_alpha_search" )
206- remark_beta = unique_name ("host_beta_search" )
207- remark_other = unique_name ("host_other_search" )
208- created_ids .append (create_simple_host (access_token , inbounds [0 ], remark = remark_alpha , priority = 1 ))
209- created_ids .append (create_simple_host (access_token , inbounds [0 ], remark = remark_beta , priority = 2 ))
210- created_ids .append (create_simple_host (access_token , inbounds [0 ], remark = remark_other , priority = 3 ))
211-
212- response = client .get (
213- "/api/hosts/simple" ,
214- headers = {"Authorization" : f"Bearer { access_token } " },
215- params = {"search" : "alpha_search" },
216- )
217- assert response .status_code == status .HTTP_200_OK
218- data = response .json ()
219- assert len (data ["hosts" ]) >= 1
220- assert any (h ["remark" ] == remark_alpha for h in data ["hosts" ])
221- finally :
222- for host_id in created_ids :
223- client .delete (f"/api/host/{ host_id } " , headers = {"Authorization" : f"Bearer { access_token } " })
224- delete_core (access_token , core ["id" ])
225-
226-
227- def test_get_hosts_simple_sort_ascending (access_token ):
228- """Test ascending sort by remark."""
229- core = create_core (access_token )
230- inbounds = get_inbounds (access_token )
231- assert inbounds , "No inbounds available for host creation"
232- created_ids = []
233- remarks = []
234- try :
235- for remark in ["host_c_sort" , "host_a_sort" , "host_b_sort" ]:
236- unique_remark = unique_name (remark )
237- remarks .append (unique_remark )
238- created_ids .append (create_simple_host (access_token , inbounds [0 ], remark = unique_remark , priority = 1 ))
239-
240- response = client .get (
241- "/api/hosts/simple" ,
242- headers = {"Authorization" : f"Bearer { access_token } " },
243- params = {"sort" : "remark" },
244- )
245- assert response .status_code == status .HTTP_200_OK
246- data = response .json ()
247- our_hosts = [h for h in data ["hosts" ] if h ["remark" ] in remarks ]
248- our_remarks = [h ["remark" ] for h in our_hosts ]
249- assert our_remarks == sorted (remarks )
250- finally :
251- for host_id in created_ids :
252- client .delete (f"/api/host/{ host_id } " , headers = {"Authorization" : f"Bearer { access_token } " })
253- delete_core (access_token , core ["id" ])
254-
255-
256- def test_get_hosts_simple_sort_descending (access_token ):
257- """Test descending sort by remark."""
258- core = create_core (access_token )
259- inbounds = get_inbounds (access_token )
260- assert inbounds , "No inbounds available for host creation"
261- created_ids = []
262- remarks = []
263- try :
264- for remark in ["host_a_desc" , "host_b_desc" , "host_c_desc" ]:
265- unique_remark = unique_name (remark )
266- remarks .append (unique_remark )
267- created_ids .append (create_simple_host (access_token , inbounds [0 ], remark = unique_remark , priority = 1 ))
268-
269- response = client .get (
270- "/api/hosts/simple" ,
271- headers = {"Authorization" : f"Bearer { access_token } " },
272- params = {"sort" : "-remark" },
273- )
274- assert response .status_code == status .HTTP_200_OK
275- data = response .json ()
276- our_hosts = [h for h in data ["hosts" ] if h ["remark" ] in remarks ]
277- our_remarks = [h ["remark" ] for h in our_hosts ]
278- assert our_remarks == sorted (remarks , reverse = True )
279- finally :
280- for host_id in created_ids :
281- client .delete (f"/api/host/{ host_id } " , headers = {"Authorization" : f"Bearer { access_token } " })
282- delete_core (access_token , core ["id" ])
283-
284-
285- def test_get_hosts_simple_pagination (access_token ):
286- """Test pagination with offset and limit."""
287- core = create_core (access_token )
288- inbounds = get_inbounds (access_token )
289- assert inbounds , "No inbounds available for host creation"
290- created_ids = []
291- try :
292- for i in range (5 ):
293- remark = unique_name (f"host_pag_{ i } " )
294- created_ids .append (create_simple_host (access_token , inbounds [0 ], remark = remark , priority = i + 1 ))
295-
296- response1 = client .get (
297- "/api/hosts/simple" ,
298- headers = {"Authorization" : f"Bearer { access_token } " },
299- params = {"offset" : 0 , "limit" : 2 },
300- )
301- response2 = client .get (
302- "/api/hosts/simple" ,
303- headers = {"Authorization" : f"Bearer { access_token } " },
304- params = {"offset" : 2 , "limit" : 2 },
305- )
306-
307- assert response1 .status_code == status .HTTP_200_OK
308- assert response2 .status_code == status .HTTP_200_OK
309- data1 = response1 .json ()
310- data2 = response2 .json ()
311- assert len (data1 ["hosts" ]) == 2
312- assert len (data2 ["hosts" ]) == 2
313-
314- ids1 = {h ["id" ] for h in data1 ["hosts" ]}
315- ids2 = {h ["id" ] for h in data2 ["hosts" ]}
316- assert len (ids1 & ids2 ) == 0
317- finally :
318- for host_id in created_ids :
319- client .delete (f"/api/host/{ host_id } " , headers = {"Authorization" : f"Bearer { access_token } " })
320- delete_core (access_token , core ["id" ])
321-
322-
323- def test_get_hosts_simple_skip_pagination (access_token ):
324- """Test all=true parameter returns all records."""
325- core = create_core (access_token )
326- inbounds = get_inbounds (access_token )
327- assert inbounds , "No inbounds available for host creation"
328- created_ids = []
329- try :
330- for i in range (10 ):
331- remark = unique_name (f"host_all_{ i } " )
332- created_ids .append (create_simple_host (access_token , inbounds [0 ], remark = remark , priority = i + 1 ))
333-
334- response = client .get (
335- "/api/hosts/simple" ,
336- headers = {"Authorization" : f"Bearer { access_token } " },
337- params = {"all" : "true" },
338- )
339- assert response .status_code == status .HTTP_200_OK
340- data = response .json ()
341- assert "hosts" in data
342- assert "total" in data
343- assert data ["total" ] >= 10
344- finally :
345- for host_id in created_ids :
346- client .delete (f"/api/host/{ host_id } " , headers = {"Authorization" : f"Bearer { access_token } " })
347- delete_core (access_token , core ["id" ])
348-
349-
350- def test_get_hosts_simple_empty_search (access_token ):
351- """Test search with no matching results."""
352- core = create_core (access_token )
353- inbounds = get_inbounds (access_token )
354- assert inbounds , "No inbounds available for host creation"
355- created_ids = []
356- try :
357- remark1 = unique_name ("known_host_search_1" )
358- remark2 = unique_name ("known_host_search_2" )
359- created_ids .append (create_simple_host (access_token , inbounds [0 ], remark = remark1 , priority = 1 ))
360- created_ids .append (create_simple_host (access_token , inbounds [0 ], remark = remark2 , priority = 2 ))
361-
362- response = client .get (
363- "/api/hosts/simple" ,
364- headers = {"Authorization" : f"Bearer { access_token } " },
365- params = {"search" : "nonexistent_host_xyz_12345" },
366- )
367- assert response .status_code == status .HTTP_200_OK
368- data = response .json ()
369- assert data ["total" ] == 0
370- assert len (data ["hosts" ]) == 0
371- finally :
372- for host_id in created_ids :
373- client .delete (f"/api/host/{ host_id } " , headers = {"Authorization" : f"Bearer { access_token } " })
374- delete_core (access_token , core ["id" ])
375-
376-
377- def test_get_hosts_simple_invalid_sort (access_token ):
378- """Test error handling for invalid sort parameter."""
379- response = client .get (
380- "/api/hosts/simple" ,
381- headers = {"Authorization" : f"Bearer { access_token } " },
382- params = {"sort" : "invalid_field_xyz" },
383- )
384- assert response .status_code == status .HTTP_400_BAD_REQUEST
385-
386-
160+ return response .json ()["id" ]
0 commit comments