Skip to content

Commit 809b85b

Browse files
committed
Merge #1335: Refactor: improve REST API client
89b0bfd refactor: [#689] improve REST API client (Jose Celano) Pull request description: Refactor: improve REST API client: - Add a timeout to the requests. - Return an error in the constructor if it can't build the HTTP client. - Extract constants. ACKs for top commit: josecelano: ACK 89b0bfd Tree-SHA512: 2b552e2ed6ce56587f5a364ed1b3fcc4b20d16f2ea5229e33594f2f35c5c25835ae84f8864a1d1114e906fc9af2b4a6f82d287c30d4798795b2a590d13909046
2 parents 1f69e02 + 89b0bfd commit 809b85b

File tree

6 files changed

+93
-12
lines changed

6 files changed

+93
-12
lines changed

packages/axum-rest-tracker-api-server/tests/server/v1/contract/authentication.rs

+6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ async fn should_authenticate_requests_by_using_a_token_query_param() {
1616
let token = env.get_connection_info().api_token.unwrap();
1717

1818
let response = Client::new(env.get_connection_info())
19+
.unwrap()
1920
.get_request_with_query("stats", Query::params([QueryParam::new("token", &token)].to_vec()), None)
2021
.await;
2122

@@ -33,6 +34,7 @@ async fn should_not_authenticate_requests_when_the_token_is_missing() {
3334
let request_id = Uuid::new_v4();
3435

3536
let response = Client::new(env.get_connection_info())
37+
.unwrap()
3638
.get_request_with_query("stats", Query::default(), Some(headers_with_request_id(request_id)))
3739
.await;
3840

@@ -55,6 +57,7 @@ async fn should_not_authenticate_requests_when_the_token_is_empty() {
5557
let request_id = Uuid::new_v4();
5658

5759
let response = Client::new(env.get_connection_info())
60+
.unwrap()
5861
.get_request_with_query(
5962
"stats",
6063
Query::params([QueryParam::new("token", "")].to_vec()),
@@ -81,6 +84,7 @@ async fn should_not_authenticate_requests_when_the_token_is_invalid() {
8184
let request_id = Uuid::new_v4();
8285

8386
let response = Client::new(env.get_connection_info())
87+
.unwrap()
8488
.get_request_with_query(
8589
"stats",
8690
Query::params([QueryParam::new("token", "INVALID TOKEN")].to_vec()),
@@ -108,13 +112,15 @@ async fn should_allow_the_token_query_param_to_be_at_any_position_in_the_url_que
108112

109113
// At the beginning of the query component
110114
let response = Client::new(env.get_connection_info())
115+
.unwrap()
111116
.get_request(&format!("torrents?token={token}&limit=1"))
112117
.await;
113118

114119
assert_eq!(response.status(), 200);
115120

116121
// At the end of the query component
117122
let response = Client::new(env.get_connection_info())
123+
.unwrap()
118124
.get_request(&format!("torrents?limit=1&token={token}"))
119125
.await;
120126

packages/axum-rest-tracker-api-server/tests/server/v1/contract/context/auth_key.rs

+21
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ async fn should_allow_generating_a_new_random_auth_key() {
2525
let request_id = Uuid::new_v4();
2626

2727
let response = Client::new(env.get_connection_info())
28+
.unwrap()
2829
.add_auth_key(
2930
AddKeyForm {
3031
opt_key: None,
@@ -56,6 +57,7 @@ async fn should_allow_uploading_a_preexisting_auth_key() {
5657
let request_id = Uuid::new_v4();
5758

5859
let response = Client::new(env.get_connection_info())
60+
.unwrap()
5961
.add_auth_key(
6062
AddKeyForm {
6163
opt_key: Some("Xc1L4PbQJSFGlrgSRZl8wxSFAuMa21z5".to_string()),
@@ -87,6 +89,7 @@ async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users()
8789
let request_id = Uuid::new_v4();
8890

8991
let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
92+
.unwrap()
9093
.add_auth_key(
9194
AddKeyForm {
9295
opt_key: None,
@@ -106,6 +109,7 @@ async fn should_not_allow_generating_a_new_auth_key_for_unauthenticated_users()
106109
let request_id = Uuid::new_v4();
107110

108111
let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
112+
.unwrap()
109113
.add_auth_key(
110114
AddKeyForm {
111115
opt_key: None,
@@ -136,6 +140,7 @@ async fn should_fail_when_the_auth_key_cannot_be_generated() {
136140
let request_id = Uuid::new_v4();
137141

138142
let response = Client::new(env.get_connection_info())
143+
.unwrap()
139144
.add_auth_key(
140145
AddKeyForm {
141146
opt_key: None,
@@ -173,6 +178,7 @@ async fn should_allow_deleting_an_auth_key() {
173178
let request_id = Uuid::new_v4();
174179

175180
let response = Client::new(env.get_connection_info())
181+
.unwrap()
176182
.delete_auth_key(&auth_key.key.to_string(), Some(headers_with_request_id(request_id)))
177183
.await;
178184

@@ -207,6 +213,7 @@ async fn should_fail_generating_a_new_auth_key_when_the_provided_key_is_invalid(
207213
let request_id = Uuid::new_v4();
208214

209215
let response = Client::new(env.get_connection_info())
216+
.unwrap()
210217
.post_form(
211218
"keys",
212219
&InvalidAddKeyForm {
@@ -246,6 +253,7 @@ async fn should_fail_generating_a_new_auth_key_when_the_key_duration_is_invalid(
246253
let request_id = Uuid::new_v4();
247254

248255
let response = Client::new(env.get_connection_info())
256+
.unwrap()
249257
.post_form(
250258
"keys",
251259
&InvalidAddKeyForm {
@@ -282,6 +290,7 @@ async fn should_fail_deleting_an_auth_key_when_the_key_id_is_invalid() {
282290
let request_id = Uuid::new_v4();
283291

284292
let response = Client::new(env.get_connection_info())
293+
.unwrap()
285294
.delete_auth_key(invalid_auth_key, Some(headers_with_request_id(request_id)))
286295
.await;
287296

@@ -311,6 +320,7 @@ async fn should_fail_when_the_auth_key_cannot_be_deleted() {
311320
let request_id = Uuid::new_v4();
312321

313322
let response = Client::new(env.get_connection_info())
323+
.unwrap()
314324
.delete_auth_key(&auth_key.key.to_string(), Some(headers_with_request_id(request_id)))
315325
.await;
316326

@@ -344,6 +354,7 @@ async fn should_not_allow_deleting_an_auth_key_for_unauthenticated_users() {
344354
let request_id = Uuid::new_v4();
345355

346356
let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
357+
.unwrap()
347358
.delete_auth_key(&auth_key.key.to_string(), Some(headers_with_request_id(request_id)))
348359
.await;
349360

@@ -366,6 +377,7 @@ async fn should_not_allow_deleting_an_auth_key_for_unauthenticated_users() {
366377
let request_id = Uuid::new_v4();
367378

368379
let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
380+
.unwrap()
369381
.delete_auth_key(&auth_key.key.to_string(), Some(headers_with_request_id(request_id)))
370382
.await;
371383

@@ -396,6 +408,7 @@ async fn should_allow_reloading_keys() {
396408
let request_id = Uuid::new_v4();
397409

398410
let response = Client::new(env.get_connection_info())
411+
.unwrap()
399412
.reload_keys(Some(headers_with_request_id(request_id)))
400413
.await;
401414

@@ -423,6 +436,7 @@ async fn should_fail_when_keys_cannot_be_reloaded() {
423436
force_database_error(&env.container.tracker_core_container.database);
424437

425438
let response = Client::new(env.get_connection_info())
439+
.unwrap()
426440
.reload_keys(Some(headers_with_request_id(request_id)))
427441
.await;
428442

@@ -453,6 +467,7 @@ async fn should_not_allow_reloading_keys_for_unauthenticated_users() {
453467
let request_id = Uuid::new_v4();
454468

455469
let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
470+
.unwrap()
456471
.reload_keys(Some(headers_with_request_id(request_id)))
457472
.await;
458473

@@ -466,6 +481,7 @@ async fn should_not_allow_reloading_keys_for_unauthenticated_users() {
466481
let request_id = Uuid::new_v4();
467482

468483
let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
484+
.unwrap()
469485
.reload_keys(Some(headers_with_request_id(request_id)))
470486
.await;
471487

@@ -504,6 +520,7 @@ mod deprecated_generate_key_endpoint {
504520
let seconds_valid = 60;
505521

506522
let response = Client::new(env.get_connection_info())
523+
.unwrap()
507524
.generate_auth_key(seconds_valid, None)
508525
.await;
509526

@@ -530,12 +547,14 @@ mod deprecated_generate_key_endpoint {
530547
let seconds_valid = 60;
531548

532549
let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
550+
.unwrap()
533551
.generate_auth_key(seconds_valid, Some(headers_with_request_id(request_id)))
534552
.await;
535553

536554
assert_token_not_valid(response).await;
537555

538556
let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
557+
.unwrap()
539558
.generate_auth_key(seconds_valid, None)
540559
.await;
541560

@@ -563,6 +582,7 @@ mod deprecated_generate_key_endpoint {
563582

564583
for invalid_key_duration in invalid_key_durations {
565584
let response = Client::new(env.get_connection_info())
585+
.unwrap()
566586
.post_empty(&format!("key/{invalid_key_duration}"), None)
567587
.await;
568588

@@ -583,6 +603,7 @@ mod deprecated_generate_key_endpoint {
583603
let request_id = Uuid::new_v4();
584604
let seconds_valid = 60;
585605
let response = Client::new(env.get_connection_info())
606+
.unwrap()
586607
.generate_auth_key(seconds_valid, Some(headers_with_request_id(request_id)))
587608
.await;
588609

packages/axum-rest-tracker-api-server/tests/server/v1/contract/context/stats.rs

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ async fn should_allow_getting_tracker_statistics() {
2626
let request_id = Uuid::new_v4();
2727

2828
let response = Client::new(env.get_connection_info())
29+
.unwrap()
2930
.get_tracker_statistics(Some(headers_with_request_id(request_id)))
3031
.await;
3132

@@ -80,6 +81,7 @@ async fn should_not_allow_getting_tracker_statistics_for_unauthenticated_users()
8081
let request_id = Uuid::new_v4();
8182

8283
let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
84+
.unwrap()
8385
.get_tracker_statistics(Some(headers_with_request_id(request_id)))
8486
.await;
8587

@@ -93,6 +95,7 @@ async fn should_not_allow_getting_tracker_statistics_for_unauthenticated_users()
9395
let request_id = Uuid::new_v4();
9496

9597
let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
98+
.unwrap()
9699
.get_tracker_statistics(Some(headers_with_request_id(request_id)))
97100
.await;
98101

packages/axum-rest-tracker-api-server/tests/server/v1/contract/context/torrent.rs

+15
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ async fn should_allow_getting_all_torrents() {
3131
let request_id = Uuid::new_v4();
3232

3333
let response = Client::new(env.get_connection_info())
34+
.unwrap()
3435
.get_torrents(Query::empty(), Some(headers_with_request_id(request_id)))
3536
.await;
3637

@@ -64,6 +65,7 @@ async fn should_allow_limiting_the_torrents_in_the_result() {
6465
let request_id = Uuid::new_v4();
6566

6667
let response = Client::new(env.get_connection_info())
68+
.unwrap()
6769
.get_torrents(
6870
Query::params([QueryParam::new("limit", "1")].to_vec()),
6971
Some(headers_with_request_id(request_id)),
@@ -100,6 +102,7 @@ async fn should_allow_the_torrents_result_pagination() {
100102
let request_id = Uuid::new_v4();
101103

102104
let response = Client::new(env.get_connection_info())
105+
.unwrap()
103106
.get_torrents(
104107
Query::params([QueryParam::new("offset", "1")].to_vec()),
105108
Some(headers_with_request_id(request_id)),
@@ -135,6 +138,7 @@ async fn should_allow_getting_a_list_of_torrents_providing_infohashes() {
135138
let request_id = Uuid::new_v4();
136139

137140
let response = Client::new(env.get_connection_info())
141+
.unwrap()
138142
.get_torrents(
139143
Query::params(
140144
[
@@ -181,6 +185,7 @@ async fn should_fail_getting_torrents_when_the_offset_query_parameter_cannot_be_
181185
let request_id = Uuid::new_v4();
182186

183187
let response = Client::new(env.get_connection_info())
188+
.unwrap()
184189
.get_torrents(
185190
Query::params([QueryParam::new("offset", invalid_offset)].to_vec()),
186191
Some(headers_with_request_id(request_id)),
@@ -209,6 +214,7 @@ async fn should_fail_getting_torrents_when_the_limit_query_parameter_cannot_be_p
209214
let request_id = Uuid::new_v4();
210215

211216
let response = Client::new(env.get_connection_info())
217+
.unwrap()
212218
.get_torrents(
213219
Query::params([QueryParam::new("limit", invalid_limit)].to_vec()),
214220
Some(headers_with_request_id(request_id)),
@@ -237,6 +243,7 @@ async fn should_fail_getting_torrents_when_the_info_hash_parameter_is_invalid()
237243
let request_id = Uuid::new_v4();
238244

239245
let response = Client::new(env.get_connection_info())
246+
.unwrap()
240247
.get_torrents(
241248
Query::params([QueryParam::new("info_hash", invalid_info_hash)].to_vec()),
242249
Some(headers_with_request_id(request_id)),
@@ -262,6 +269,7 @@ async fn should_not_allow_getting_torrents_for_unauthenticated_users() {
262269
let request_id = Uuid::new_v4();
263270

264271
let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
272+
.unwrap()
265273
.get_torrents(Query::empty(), Some(headers_with_request_id(request_id)))
266274
.await;
267275

@@ -275,6 +283,7 @@ async fn should_not_allow_getting_torrents_for_unauthenticated_users() {
275283
let request_id = Uuid::new_v4();
276284

277285
let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
286+
.unwrap()
278287
.get_torrents(Query::default(), Some(headers_with_request_id(request_id)))
279288
.await;
280289

@@ -303,6 +312,7 @@ async fn should_allow_getting_a_torrent_info() {
303312
let request_id = Uuid::new_v4();
304313

305314
let response = Client::new(env.get_connection_info())
315+
.unwrap()
306316
.get_torrent(&info_hash.to_string(), Some(headers_with_request_id(request_id)))
307317
.await;
308318

@@ -331,6 +341,7 @@ async fn should_fail_while_getting_a_torrent_info_when_the_torrent_does_not_exis
331341
let info_hash = InfoHash::from_str("9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d").unwrap(); // DevSkim: ignore DS173237
332342

333343
let response = Client::new(env.get_connection_info())
344+
.unwrap()
334345
.get_torrent(&info_hash.to_string(), Some(headers_with_request_id(request_id)))
335346
.await;
336347

@@ -349,6 +360,7 @@ async fn should_fail_getting_a_torrent_info_when_the_provided_infohash_is_invali
349360
let request_id = Uuid::new_v4();
350361

351362
let response = Client::new(env.get_connection_info())
363+
.unwrap()
352364
.get_torrent(invalid_infohash, Some(headers_with_request_id(request_id)))
353365
.await;
354366

@@ -359,6 +371,7 @@ async fn should_fail_getting_a_torrent_info_when_the_provided_infohash_is_invali
359371
let request_id = Uuid::new_v4();
360372

361373
let response = Client::new(env.get_connection_info())
374+
.unwrap()
362375
.get_torrent(invalid_infohash, Some(headers_with_request_id(request_id)))
363376
.await;
364377

@@ -381,6 +394,7 @@ async fn should_not_allow_getting_a_torrent_info_for_unauthenticated_users() {
381394
let request_id = Uuid::new_v4();
382395

383396
let response = Client::new(connection_with_invalid_token(env.get_connection_info().origin))
397+
.unwrap()
384398
.get_torrent(&info_hash.to_string(), Some(headers_with_request_id(request_id)))
385399
.await;
386400

@@ -394,6 +408,7 @@ async fn should_not_allow_getting_a_torrent_info_for_unauthenticated_users() {
394408
let request_id = Uuid::new_v4();
395409

396410
let response = Client::new(connection_with_no_token(env.get_connection_info().origin))
411+
.unwrap()
397412
.get_torrent(&info_hash.to_string(), Some(headers_with_request_id(request_id)))
398413
.await;
399414

0 commit comments

Comments
 (0)