nym-http-api-client 1.22.1

Nym's HTTP API client, examples, and tests
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use super::*;
use serial_test::serial;
use std::time::{Duration, Instant};

#[test]
fn sanitizing_urls() {
    let base_url: Url = "http://api.test".parse().unwrap();

    // works with a full string
    assert_eq!(
        "http://api.test/foo/bar",
        sanitize_url(&base_url, "/foo//bar/", NO_PARAMS).as_str()
    );

    // (and leading slash doesn't matter)
    assert_eq!(
        "http://api.test/foo/bar",
        sanitize_url(&base_url, "foo//bar/", NO_PARAMS).as_str()
    );

    // works with 1 segment
    assert_eq!(
        "http://api.test/foo",
        sanitize_url(&base_url, &["foo"], NO_PARAMS).as_str()
    );

    // works with 2 segments
    assert_eq!(
        "http://api.test/foo/bar",
        sanitize_url(&base_url, &["foo", "bar"], NO_PARAMS).as_str()
    );

    // works with leading slash
    assert_eq!(
        "http://api.test/foo",
        sanitize_url(&base_url, &["/foo"], NO_PARAMS).as_str()
    );
    assert_eq!(
        "http://api.test/foo/bar",
        sanitize_url(&base_url, &["/foo", "bar"], NO_PARAMS).as_str()
    );
    assert_eq!(
        "http://api.test/foo/bar",
        sanitize_url(&base_url, &["foo", "/bar"], NO_PARAMS).as_str()
    );

    // works with trailing slash
    assert_eq!(
        "http://api.test/foo",
        sanitize_url(&base_url, &["foo/"], NO_PARAMS).as_str()
    );
    assert_eq!(
        "http://api.test/foo/bar",
        sanitize_url(&base_url, &["foo/", "bar"], NO_PARAMS).as_str()
    );
    assert_eq!(
        "http://api.test/foo/bar",
        sanitize_url(&base_url, &["foo", "bar/"], NO_PARAMS).as_str()
    );

    // works with both leading and trailing slash
    assert_eq!(
        "http://api.test/foo",
        sanitize_url(&base_url, &["/foo/"], NO_PARAMS).as_str()
    );
    assert_eq!(
        "http://api.test/foo/bar",
        sanitize_url(&base_url, &["/foo/", "/bar/"], NO_PARAMS).as_str()
    );

    // adds params
    assert_eq!(
        "http://api.test/foo/bar?foomp=baz",
        sanitize_url(&base_url, &["foo", "bar"], &[("foomp", "baz")]).as_str()
    );
    assert_eq!(
        "http://api.test/foo/bar?arg1=val1&arg2=val2",
        sanitize_url(
            &base_url,
            &["/foo/", "/bar/"],
            &[("arg1", "val1"), ("arg2", "val2")]
        )
        .as_str()
    );
}

// - Do the retries work
// - Do we use fallback urls on retry if multiple are provided
// - Do we use the next front on retry if multiple are provided
// - If we have more retries than urls, do we wrap back to the first one again
// - on error without retries is where we have multiple urls, is the url updated?

#[tokio::test]
#[cfg(any())] // #[ignore] we run ignore assuming it just means slow in Ci/CD -_-
// test relies on external services being available and behaving in a specific way.
async fn api_client_retry() -> Result<(), Box<dyn std::error::Error>> {
    let client = ClientBuilder::new_with_urls(vec![
        "http://broken.nym.test".parse()?, // This should fail because of DNS NXDomain (rotate)
        "http://127.0.0.1:9".parse()?,     // This will fail because of TCP refused (rotate)
        "https://httpbin.org/status/200".parse()?, // This should succeed
    ])?
    .with_retries(2)
    .build()?;

    let req = client.create_get_request(&[], NO_PARAMS).unwrap();
    let _resp = client.send(req).await?;

    // The main test is that we successfully retried and switched to the working URL
    // We accept any response from the working endpoint since external services can be unreliable
    assert_eq!(
        client.current_url().as_str(),
        "https://httpbin.org/status/200"
    );

    // // This assert can be unreliable due to factors beyond our control and beyond the scope of
    // // this test
    // assert_eq!(_resp.status(), StatusCode::OK);

    Ok(())
}

#[test]
fn host_updating() {
    let url = Url::new("http://nym-api1.test", None).unwrap();
    let mut client = ClientBuilder::new(url).unwrap().build().unwrap();

    // check that the url is set correctly
    let current_url = client.current_url();
    assert_eq!(current_url.as_str(), "http://nym-api1.test/");
    assert_eq!(current_url.front_str(), None);

    // update the url
    client.update_host(None);

    // check that the url is still the same since there is one URL
    assert_eq!(client.current_url().as_str(), "http://nym-api1.test/");

    // =======================================
    // we rotate through urls when available

    let new_urls = vec![
        Url::new("http://nym-api1.test", None).unwrap(),
        Url::new("http://nym-api2.test", None).unwrap(),
    ];
    client.change_base_urls(new_urls);
    assert_eq!(client.current_url().as_str(), "http://nym-api1.test/");

    client.update_host(None);

    // check that the url got updated now that there are multiple URLs
    assert_eq!(client.current_url().as_str(), "http://nym-api2.test/");
    assert_eq!(client.current_url().front_str(), None);

    client.update_host(None);
    assert_eq!(client.current_url().as_str(), "http://nym-api1.test/");

    // =======================================
    // we rotate through urls when available if fronting is disabled

    let new_urls = vec![
        Url::new(
            "http://nym-api1.test",
            Some(vec!["http://cdn1.test", "http://cdn2.test"]),
        )
        .unwrap(),
        Url::new("http://nym-api2.test", None).unwrap(),
    ];
    client.change_base_urls(new_urls);

    assert_eq!(client.current_url().as_str(), "http://nym-api1.test/");

    client.update_host(None);

    // check that the url got updated now that there are multiple URLs
    assert_eq!(client.current_url().as_str(), "http://nym-api2.test/");
}

#[test]
fn host_updating_url_conditioned() {
    let url1 = Url::new("http://nym-api1.test", None).unwrap();
    let url2 = Url::new("http://nym-api2.test", None).unwrap();
    let urls = vec![url1.clone(), url2.clone()];
    let client = ClientBuilder::new_with_urls(urls).unwrap().build().unwrap();

    assert_eq!(client.current_url().as_str(), "http://nym-api1.test/");

    // Try to update with a URL that does NOT match current - should result in no change
    client.update_host(Some(Url::parse("http://example.com").unwrap()));

    // check that the url did NOT get updated
    assert_eq!(client.current_url().as_str(), "http://nym-api1.test/");
    assert_eq!(client.current_url().front_str(), None);

    // Try to update with a URL that DOES match current - should result in no change
    client.update_host(Some(url1));
    assert_eq!(client.current_url().as_str(), "http://nym-api2.test/");
}

#[test]
#[cfg(feature = "tunneling")]
fn fronted_host_updating() {
    let url = Url::new("http://nym-api.test", Some(vec!["http://cdn1.test"])).unwrap();
    let mut client = ClientBuilder::new(url)
        .unwrap()
        .with_fronting(Some(crate::fronted::FrontPolicy::Always))
        .build()
        .unwrap();

    // check that the url is set correctly
    let current_url = client.current_url();
    assert_eq!(current_url.as_str(), "http://nym-api.test/");
    assert_eq!(current_url.front_str(), Some("cdn1.test"));

    // update the url
    client.update_host(None);

    // check that the url is still the same since there is one URL and one front
    let current_url = client.current_url();
    assert_eq!(current_url.as_str(), "http://nym-api.test/");
    assert_eq!(current_url.front_str(), Some("cdn1.test"));

    // =======================================
    // we rotate through front urls when available if fronting is enabled

    let new_urls = vec![
        Url::new(
            "http://nym-api.test",
            Some(vec!["http://cdn1.test", "http://cdn2.test"]),
        )
        .unwrap(),
        Url::new("http://nym-api2.test", None).unwrap(),
    ];
    client.change_base_urls(new_urls);

    let current_url = client.current_url();
    assert_eq!(current_url.as_str(), "http://nym-api.test/");
    assert_eq!(current_url.front_str(), Some("cdn1.test"));

    // update the url - this should keep the same host but change the front
    client.update_host(None);

    let current_url = client.current_url();
    // check that the url is still the same since there is one URL
    assert_eq!(current_url.as_str(), "http://nym-api.test/");
    assert_eq!(current_url.front_str(), Some("cdn2.test"));

    // update the url - this should wrap around to the first front as the second url is not fronted
    client.update_host(None);

    let current_url = client.current_url();
    assert_eq!(current_url.as_str(), "http://nym-api.test/");
    assert_eq!(current_url.front_str(), Some("cdn1.test"));
}

// Reproduces the exact url-list shape used for the nymvpn-api config:
//   [{ "url": "https://nymvpn.com/api/" },
//    { "url": "https://nymvpn-frontdoor.global.ssl.fastly.net/api",
//      "fronts": ["yelp.global.ssl.fastly.net"] }]
//
// When fronting is enabled and the CURRENT url is the one with no `fronts`
// configured, `matches_current_host` must consider the fact that the domain
// doesn't have fronts and compare properly. This test ensures the correct
// behavior, rotating to the next (fronted) entry in the list.
#[test]
#[cfg(feature = "tunneling")]
fn fronting_enabled_stuck_on_unfronted_first_url() {
    let plain_url = Url::new("https://nymvpn.com/api/", None).unwrap();
    let fronted_url = Url::new(
        "https://nymvpn-frontdoor.global.ssl.fastly.net/api",
        Some(vec!["https://yelp.global.ssl.fastly.net"]),
    )
    .unwrap();

    let client = ClientBuilder::new_with_urls(vec![plain_url, fronted_url])
        .unwrap()
        .with_fronting(Some(crate::fronted::FrontPolicy::Always))
        .build()
        .unwrap();

    // client starts on the first, unfronted url.
    assert_eq!(client.current_url().as_str(), "https://nymvpn.com/api/");

    // offending url matches the plain url
    let offending = Url::parse("https://nymvpn.com/api/").unwrap();
    client.maybe_rotate_hosts(Some(offending));

    // should rotate urls.
    assert_eq!(
        client.current_url().as_str(),
        "https://nymvpn-frontdoor.global.ssl.fastly.net/api",
        "client failed to rotate away from the unfronted url after an error"
    );
}

#[test]
#[cfg(feature = "network-defaults")]
fn from_network_configures_multiple_urls_and_retries() {
    use nym_network_defaults::{ApiUrl, NymNetworkDetails};

    // Create network details with multiple URLs and fronting
    let mut network_details = NymNetworkDetails::new_empty();
    network_details.set_nym_api_urls(vec![
        ApiUrl {
            url: "https://validator.nymtech.net/api/".to_string(),
            front_hosts: None,
        },
        ApiUrl {
            url: "https://nym-frontdoor.vercel.app/api/".to_string(),
            front_hosts: Some(vec!["vercel.app".to_string(), "vercel.com".to_string()]),
        },
        ApiUrl {
            url: "https://nym-frontdoor.global.ssl.fastly.net/api/".to_string(),
            front_hosts: Some(vec!["yelp.global.ssl.fastly.net".to_string()]),
        },
    ]);

    // Build client from network details
    let client = ClientBuilder::new_with_fronted_urls(network_details.nym_api_urls())
        .expect("Failed to create client from network")
        .build()
        .expect("Failed to build client");

    // Verify all URLs were configured
    assert_eq!(
        client.base_urls().len(),
        3,
        "Expected 3 URLs to be configured from network details"
    );

    // Verify the URLs have fronting configured where appropriate
    assert_eq!(
        client.base_urls()[0].as_str(),
        "https://validator.nymtech.net/api/"
    );
    assert!(client.base_urls()[0].front_str().is_none());

    assert_eq!(
        client.base_urls()[1].as_str(),
        "https://nym-frontdoor.vercel.app/api/"
    );
    assert!(client.base_urls()[1].front_str().is_some());

    assert_eq!(
        client.base_urls()[2].as_str(),
        "https://nym-frontdoor.global.ssl.fastly.net/api/"
    );
    assert!(client.base_urls()[2].front_str().is_some());
}

/// Tests that network reconfiguration timestamp tempers host rotation / fronting activation.
///
/// If a network reconfiguration happened after request start we avoid rotating and avoid enabling
/// fronting. Otherwise, a network error should rotate host and enable fronting (for `OnRetry`).
#[tokio::test]
#[serial]
#[cfg(feature = "tunneling")]
async fn host_rotation_tempered_by_net_reconfigure() {
    // mutates the process-wide SHARED_NETWORK_RECONFIGURATION marker and sends real requests
    // sensitive to it - must not run concurrently with tests that touch the same state.

    let url1 = Url::new("http://nym-api.test", Some(vec!["http://cdn1.test"])).unwrap();
    let url2 = Url::new("http://nym-api2.test", Some(vec!["http://cdn2.test"])).unwrap();
    let urls = vec![url1.clone(), url2.clone()];

    let client = ClientBuilder::new_with_urls(urls)
        .unwrap()
        .with_fronting(Some(crate::fronted::FrontPolicy::OnRetry))
        .build()
        .unwrap();

    let request_host = |client: &Client| {
        client
            .create_get_request(&["health"], NO_PARAMS)
            .unwrap()
            .build()
            .unwrap()
            .url()
            .host_str()
            .unwrap()
            .to_string()
    };

    // fronting starts disabled for OnRetry policy.
    assert_eq!(request_host(&client), "nym-api.test");
    assert_eq!(client.current_url().as_str(), "http://nym-api.test/");

    // Simulate a network reconfiguration happening during the request. This should suppress both
    // host rotation and fronting activation.
    *crate::SHARED_NETWORK_RECONFIGURATION.lock().unwrap() =
        Some(Instant::now() + Duration::from_secs(60));
    let req = client.create_get_request(&["health"], NO_PARAMS).unwrap();
    let _ = client.send(req).await;

    assert_eq!(client.current_url().as_str(), "http://nym-api.test/");
    assert_eq!(request_host(&client), "nym-api.test");

    // Simulate no recent network reconfiguration. Now the same network error should rotate to the
    // next host and enable fronting for OnRetry.
    *crate::SHARED_NETWORK_RECONFIGURATION.lock().unwrap() =
        Some(Instant::now() - Duration::from_secs(60));
    let req = client.create_get_request(&["health"], NO_PARAMS).unwrap();
    let _ = client.send(req).await;

    assert_eq!(client.current_url().as_str(), "http://nym-api2.test/");
    assert_eq!(request_host(&client), "cdn2.test");
}

#[test]
fn rate_limit_detection_on_plain_429() {
    // a bare 429 with no special headers should be treated as a rate limit response
    assert!(is_rate_limit_response(
        StatusCode::TOO_MANY_REQUESTS,
        &HeaderMap::new()
    ));

    // the status code alone is sufficient - unrelated headers shouldn't change that
    let mut headers = HeaderMap::new();
    headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
    assert!(is_rate_limit_response(
        StatusCode::TOO_MANY_REQUESTS,
        &headers
    ));
}

#[test]
fn rate_limit_detection_on_throttled_503() {
    // a 503 with Retry-After is treated as throttling, not just an outage
    let mut headers = HeaderMap::new();
    headers.insert(RETRY_AFTER, HeaderValue::from_static("120"));
    assert!(is_rate_limit_response(
        StatusCode::SERVICE_UNAVAILABLE,
        &headers
    ));

    // a plain 503 without Retry-After is NOT treated as rate limiting - it may just be down
    assert!(!is_rate_limit_response(
        StatusCode::SERVICE_UNAVAILABLE,
        &HeaderMap::new()
    ));
}

#[test]
fn rate_limit_detection_on_vercel_challenge() {
    let mut headers = HeaderMap::new();
    headers.insert(
        VERCEL_CHALLENGE_HEADER,
        HeaderValue::from_static("challenge"),
    );
    headers.insert(CONTENT_TYPE, HeaderValue::from_static("text/html"));

    assert!(is_rate_limit_response(StatusCode::FORBIDDEN, &headers));
}

#[test]
fn rate_limit_detection_ignores_unrelated_responses() {
    // plain 403 without the vercel challenge markers is not a rate limit
    assert!(!is_rate_limit_response(
        StatusCode::FORBIDDEN,
        &HeaderMap::new()
    ));

    // the vercel challenge header alone, without the matching content-type, is not enough
    let mut headers = HeaderMap::new();
    headers.insert(
        VERCEL_CHALLENGE_HEADER,
        HeaderValue::from_static("challenge"),
    );
    assert!(!is_rate_limit_response(StatusCode::FORBIDDEN, &headers));

    // a normal successful response is never a rate limit
    assert!(!is_rate_limit_response(StatusCode::OK, &HeaderMap::new()));
}