Skip to main content

nautilus_binance/common/
urls.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Environment-aware Binance API endpoint URLs.
17
18use super::{
19    consts::{
20        BINANCE_FUTURES_COIN_DEMO_HTTP_URL, BINANCE_FUTURES_COIN_DEMO_WS_URL,
21        BINANCE_FUTURES_COIN_HTTP_URL, BINANCE_FUTURES_COIN_TESTNET_HTTP_URL,
22        BINANCE_FUTURES_COIN_TESTNET_WS_URL, BINANCE_FUTURES_COIN_WS_URL,
23        BINANCE_FUTURES_USD_DEMO_HTTP_URL, BINANCE_FUTURES_USD_DEMO_WS_URL,
24        BINANCE_FUTURES_USD_HTTP_URL, BINANCE_FUTURES_USD_TESTNET_HTTP_URL,
25        BINANCE_FUTURES_USD_TESTNET_WS_URL, BINANCE_FUTURES_USD_WS_PRIVATE_URL,
26        BINANCE_FUTURES_USD_WS_PUBLIC_URL, BINANCE_FUTURES_USD_WS_URL, BINANCE_OPTIONS_HTTP_URL,
27        BINANCE_OPTIONS_TESTNET_HTTP_URL, BINANCE_OPTIONS_TESTNET_WS_PRIVATE_URL,
28        BINANCE_OPTIONS_TESTNET_WS_PUBLIC_URL, BINANCE_OPTIONS_TESTNET_WS_URL,
29        BINANCE_OPTIONS_WS_URL, BINANCE_PAPI_HTTP_URL, BINANCE_SPOT_DEMO_HTTP_URL,
30        BINANCE_SPOT_DEMO_WS_URL, BINANCE_SPOT_HTTP_URL, BINANCE_SPOT_TESTNET_HTTP_URL,
31        BINANCE_SPOT_TESTNET_WS_URL, BINANCE_SPOT_WS_URL, BINANCE_US_SPOT_HTTP_URL,
32        BINANCE_US_SPOT_USER_WS_URL, BINANCE_US_SPOT_WS_URL,
33    },
34    enums::{BinanceEnvironment, BinanceProductType},
35};
36
37/// Returns the HTTP base URL for the given product type and environment.
38#[must_use]
39pub fn get_http_base_url(
40    product_type: BinanceProductType,
41    environment: BinanceEnvironment,
42) -> &'static str {
43    match (product_type, environment) {
44        // Live
45        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Live) => {
46            BINANCE_SPOT_HTTP_URL
47        }
48        (BinanceProductType::UsdM, BinanceEnvironment::Live) => BINANCE_FUTURES_USD_HTTP_URL,
49        (BinanceProductType::CoinM, BinanceEnvironment::Live) => BINANCE_FUTURES_COIN_HTTP_URL,
50        (BinanceProductType::Options, BinanceEnvironment::Live) => BINANCE_OPTIONS_HTTP_URL,
51
52        // Testnet
53        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Testnet) => {
54            BINANCE_SPOT_TESTNET_HTTP_URL
55        }
56        (BinanceProductType::UsdM, BinanceEnvironment::Testnet) => {
57            BINANCE_FUTURES_USD_TESTNET_HTTP_URL
58        }
59        (BinanceProductType::CoinM, BinanceEnvironment::Testnet) => {
60            BINANCE_FUTURES_COIN_TESTNET_HTTP_URL
61        }
62        (BinanceProductType::Options, BinanceEnvironment::Testnet) => {
63            BINANCE_OPTIONS_TESTNET_HTTP_URL
64        }
65
66        // Demo
67        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Demo) => {
68            BINANCE_SPOT_DEMO_HTTP_URL
69        }
70        (BinanceProductType::UsdM, BinanceEnvironment::Demo) => BINANCE_FUTURES_USD_DEMO_HTTP_URL,
71        (BinanceProductType::CoinM, BinanceEnvironment::Demo) => BINANCE_FUTURES_COIN_DEMO_HTTP_URL,
72        (BinanceProductType::Options, BinanceEnvironment::Demo) => BINANCE_OPTIONS_TESTNET_HTTP_URL,
73    }
74}
75
76/// Returns the HTTP base URL, including first-class Binance US routing.
77#[must_use]
78pub fn get_http_base_url_with_us(
79    product_type: BinanceProductType,
80    environment: BinanceEnvironment,
81    us: bool,
82) -> &'static str {
83    if us {
84        BINANCE_US_SPOT_HTTP_URL
85    } else {
86        get_http_base_url(product_type, environment)
87    }
88}
89
90/// Returns the SAPI base URL for the given environment, or `None` where SAPI is unavailable.
91///
92/// SAPI endpoints (`/sapi/v1/...`) are served from the Spot host on the live exchange. The
93/// testnet and demo hosts do not route `/sapi/v1`, so callers must treat `None` as unavailable
94/// rather than falling back to live, which would route account management against real funds.
95#[must_use]
96pub fn get_sapi_base_url(environment: BinanceEnvironment) -> Option<&'static str> {
97    match environment {
98        BinanceEnvironment::Live => Some(BINANCE_SPOT_HTTP_URL),
99        BinanceEnvironment::Testnet | BinanceEnvironment::Demo => None,
100    }
101}
102
103/// Returns the Portfolio Margin (PAPI) base URL for the given environment, or `None` where PAPI
104/// is unavailable.
105///
106/// Binance documents only a production PAPI host; no testnet or demo environment exists. Callers
107/// must treat `None` as unavailable rather than redirecting to a standard Futures testnet, which
108/// does not serve `/papi/v1`.
109///
110/// References:
111/// - <https://developers.binance.com/docs/derivatives/portfolio-margin/general-info>
112#[must_use]
113pub fn get_papi_base_url(environment: BinanceEnvironment) -> Option<&'static str> {
114    match environment {
115        BinanceEnvironment::Live => Some(BINANCE_PAPI_HTTP_URL),
116        BinanceEnvironment::Testnet | BinanceEnvironment::Demo => None,
117    }
118}
119
120/// Returns the WebSocket base URL for the given product type and environment.
121#[must_use]
122pub fn get_ws_base_url(
123    product_type: BinanceProductType,
124    environment: BinanceEnvironment,
125) -> &'static str {
126    match (product_type, environment) {
127        // Live
128        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Live) => {
129            BINANCE_SPOT_WS_URL
130        }
131        (BinanceProductType::UsdM, BinanceEnvironment::Live) => BINANCE_FUTURES_USD_WS_URL,
132        (BinanceProductType::CoinM, BinanceEnvironment::Live) => BINANCE_FUTURES_COIN_WS_URL,
133        (BinanceProductType::Options, BinanceEnvironment::Live) => BINANCE_OPTIONS_WS_URL,
134
135        // Testnet
136        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Testnet) => {
137            BINANCE_SPOT_TESTNET_WS_URL
138        }
139        (BinanceProductType::UsdM, BinanceEnvironment::Testnet) => {
140            BINANCE_FUTURES_USD_TESTNET_WS_URL
141        }
142        (BinanceProductType::CoinM, BinanceEnvironment::Testnet) => {
143            BINANCE_FUTURES_COIN_TESTNET_WS_URL
144        }
145        (BinanceProductType::Options, BinanceEnvironment::Testnet) => {
146            BINANCE_OPTIONS_TESTNET_WS_URL
147        }
148
149        // Demo
150        (BinanceProductType::Spot | BinanceProductType::Margin, BinanceEnvironment::Demo) => {
151            BINANCE_SPOT_DEMO_WS_URL
152        }
153        (BinanceProductType::UsdM, BinanceEnvironment::Demo) => BINANCE_FUTURES_USD_DEMO_WS_URL,
154        (BinanceProductType::CoinM, BinanceEnvironment::Demo) => BINANCE_FUTURES_COIN_DEMO_WS_URL,
155        (BinanceProductType::Options, BinanceEnvironment::Demo) => BINANCE_OPTIONS_TESTNET_WS_URL,
156    }
157}
158
159/// Returns the WebSocket base URL, including first-class Binance US routing.
160#[must_use]
161pub fn get_ws_base_url_with_us(
162    product_type: BinanceProductType,
163    environment: BinanceEnvironment,
164    us: bool,
165) -> &'static str {
166    if us {
167        BINANCE_US_SPOT_WS_URL
168    } else {
169        get_ws_base_url(product_type, environment)
170    }
171}
172
173/// Returns a Spot user stream URL bound to the supplied listen key.
174#[must_use]
175pub(crate) fn get_spot_user_stream_url(base_url: Option<&str>, listen_key: &str) -> String {
176    let base_url = base_url.unwrap_or(BINANCE_US_SPOT_USER_WS_URL);
177    let normalized = base_url.trim_end_matches('/');
178    if normalized.ends_with("/ws") {
179        format!("{normalized}/{listen_key}")
180    } else {
181        format!("{normalized}/ws/{listen_key}")
182    }
183}
184
185/// Returns the WebSocket public stream base URL for high-frequency book data.
186///
187/// USD-M live exchange uses the dedicated public endpoint for `@bookTicker` and
188/// `@depth` streams. All other product types and environments fall back to
189/// [`get_ws_base_url`].
190#[must_use]
191pub fn get_ws_public_base_url(
192    product_type: BinanceProductType,
193    environment: BinanceEnvironment,
194) -> &'static str {
195    match (product_type, environment) {
196        (BinanceProductType::UsdM, BinanceEnvironment::Live) => BINANCE_FUTURES_USD_WS_PUBLIC_URL,
197        (BinanceProductType::Options, BinanceEnvironment::Testnet | BinanceEnvironment::Demo) => {
198            BINANCE_OPTIONS_TESTNET_WS_PUBLIC_URL
199        }
200        _ => get_ws_base_url(product_type, environment),
201    }
202}
203
204/// Returns the WebSocket private stream base URL for user data.
205///
206/// USD-M live exchange uses the dedicated private endpoint. All other
207/// product types and environments fall back to [`get_ws_base_url`].
208#[must_use]
209pub fn get_ws_private_base_url(
210    product_type: BinanceProductType,
211    environment: BinanceEnvironment,
212) -> &'static str {
213    match (product_type, environment) {
214        (BinanceProductType::UsdM, BinanceEnvironment::Live) => BINANCE_FUTURES_USD_WS_PRIVATE_URL,
215        (BinanceProductType::Options, BinanceEnvironment::Testnet | BinanceEnvironment::Demo) => {
216            BINANCE_OPTIONS_TESTNET_WS_PRIVATE_URL
217        }
218        _ => get_ws_base_url(product_type, environment),
219    }
220}
221
222/// Returns a Futures user stream URL bound to the supplied listen key.
223#[must_use]
224pub(crate) fn get_futures_user_stream_url(
225    product_type: BinanceProductType,
226    base_url: &str,
227    listen_key: &str,
228) -> String {
229    assert!(
230        matches!(
231            product_type,
232            BinanceProductType::UsdM | BinanceProductType::CoinM
233        ),
234        "Futures user stream requires UsdM or CoinM product type, was {product_type:?}"
235    );
236
237    let mut normalized = base_url.trim_end_matches('/').to_string();
238    let path = normalized
239        .split_once("://")
240        .map_or(normalized.as_str(), |(_, rest)| rest)
241        .split_once('/')
242        .map(|(_, path)| path);
243    if matches!(path, None | Some("" | "private")) {
244        normalized.push_str("/ws");
245    }
246
247    match product_type {
248        BinanceProductType::UsdM => format!("{normalized}?listenKey={listen_key}"),
249        BinanceProductType::CoinM => format!("{normalized}/{listen_key}"),
250        _ => unreachable!(),
251    }
252}
253
254fn is_usdm_ws_host(base_url: &str) -> bool {
255    // Strip scheme (e.g. `wss://`) and trailing path/port, then match the hostname.
256    // Accepts fstream.binance.com, fstream-mm.binance.com, fstream-auth.binance.com,
257    // and their .us counterparts, without admitting arbitrary substrings.
258    let without_scheme = base_url
259        .split_once("://")
260        .map_or(base_url, |(_, rest)| rest);
261    let host = without_scheme
262        .split(['/', ':'])
263        .next()
264        .unwrap_or(without_scheme);
265    host.starts_with("fstream") && (host.ends_with(".binance.com") || host.ends_with(".binance.us"))
266}
267
268/// Returns a routed USD-M Futures WebSocket URL derived from an override.
269///
270/// Binance now routes USD-M Futures live traffic by category. This function
271/// accepts either a root override (for example `wss://fstream.binance.com`) or
272/// a routed/transport-specific override such as `/market`, `/public/ws`, or
273/// `/private/stream`, then rebuilds the URL for the requested route.
274///
275/// URLs that do not point at `fstream.binance.com` (for example local test
276/// endpoints) are returned unchanged.
277#[must_use]
278pub(crate) fn get_usdm_ws_route_base_url(base_url: &str, route: &str) -> String {
279    const SUFFIXES: [&str; 11] = [
280        "/market/ws",
281        "/market/stream",
282        "/public/ws",
283        "/public/stream",
284        "/private/ws",
285        "/private/stream",
286        "/market",
287        "/public",
288        "/private",
289        "/ws",
290        "/stream",
291    ];
292
293    assert!(
294        matches!(route, "market" | "public" | "private"),
295        "invalid USD-M WebSocket route: {route}"
296    );
297
298    if !is_usdm_ws_host(base_url) {
299        return base_url.to_string();
300    }
301
302    let mut normalized = base_url.trim_end_matches('/').to_string();
303
304    for suffix in SUFFIXES {
305        if normalized.ends_with(suffix) {
306            normalized.truncate(normalized.len() - suffix.len());
307            break;
308        }
309    }
310
311    format!("{normalized}/{route}/ws")
312}
313
314#[cfg(test)]
315mod tests {
316    use rstest::rstest;
317
318    use super::*;
319
320    #[rstest]
321    fn test_http_url_spot_live() {
322        let url = get_http_base_url(BinanceProductType::Spot, BinanceEnvironment::Live);
323        assert_eq!(url, "https://api.binance.com");
324    }
325
326    #[rstest]
327    fn test_binance_us_spot_urls() {
328        let http =
329            get_http_base_url_with_us(BinanceProductType::Spot, BinanceEnvironment::Live, true);
330        let public_ws =
331            get_ws_base_url_with_us(BinanceProductType::Spot, BinanceEnvironment::Live, true);
332        let user_ws = get_spot_user_stream_url(None, "listen-key");
333
334        assert_eq!(http, "https://api.binance.us");
335        assert_eq!(public_ws, "wss://stream.binance.us:9443/ws");
336        assert_eq!(user_ws, "wss://stream.binance.us:443/ws/listen-key");
337    }
338
339    #[rstest]
340    #[case("wss://custom.example/ws", "wss://custom.example/ws/listen-key")]
341    #[case("wss://custom.example", "wss://custom.example/ws/listen-key")]
342    #[case("wss://custom.example/ws/", "wss://custom.example/ws/listen-key")]
343    fn test_spot_user_stream_url_override(#[case] base: &str, #[case] expected: &str) {
344        assert_eq!(get_spot_user_stream_url(Some(base), "listen-key"), expected);
345    }
346
347    #[rstest]
348    fn test_http_url_spot_testnet() {
349        let url = get_http_base_url(BinanceProductType::Spot, BinanceEnvironment::Testnet);
350        assert_eq!(url, "https://testnet.binance.vision");
351    }
352
353    #[rstest]
354    fn test_http_url_spot_demo() {
355        let url = get_http_base_url(BinanceProductType::Spot, BinanceEnvironment::Demo);
356        assert_eq!(url, "https://demo-api.binance.com");
357    }
358
359    #[rstest]
360    fn test_http_url_usdm_live() {
361        let url = get_http_base_url(BinanceProductType::UsdM, BinanceEnvironment::Live);
362        assert_eq!(url, "https://fapi.binance.com");
363    }
364
365    #[rstest]
366    fn test_urls_usdm_testnet() {
367        let http_url = get_http_base_url(BinanceProductType::UsdM, BinanceEnvironment::Testnet);
368        let ws_url = get_ws_base_url(BinanceProductType::UsdM, BinanceEnvironment::Testnet);
369
370        assert_eq!(http_url, "https://testnet.binancefuture.com");
371        assert_eq!(ws_url, "wss://fstream.binancefuture.com/ws");
372    }
373
374    #[rstest]
375    fn test_http_url_coinm_live() {
376        let url = get_http_base_url(BinanceProductType::CoinM, BinanceEnvironment::Live);
377        assert_eq!(url, "https://dapi.binance.com");
378    }
379
380    #[rstest]
381    fn test_urls_usdm_demo() {
382        let http_url = get_http_base_url(BinanceProductType::UsdM, BinanceEnvironment::Demo);
383        let ws_url = get_ws_base_url(BinanceProductType::UsdM, BinanceEnvironment::Demo);
384
385        assert_eq!(http_url, "https://demo-fapi.binance.com");
386        assert_eq!(ws_url, "wss://demo-fstream.binance.com/ws");
387    }
388
389    #[rstest]
390    fn test_http_url_coinm_demo() {
391        let url = get_http_base_url(BinanceProductType::CoinM, BinanceEnvironment::Demo);
392        assert_eq!(url, "https://demo-dapi.binance.com");
393    }
394
395    #[rstest]
396    fn test_http_url_options_testnet() {
397        let url = get_http_base_url(BinanceProductType::Options, BinanceEnvironment::Testnet);
398        assert_eq!(url, "https://testnet.binancefuture.com");
399    }
400
401    #[rstest]
402    fn test_http_url_options_demo() {
403        let url = get_http_base_url(BinanceProductType::Options, BinanceEnvironment::Demo);
404        assert_eq!(url, "https://testnet.binancefuture.com");
405    }
406
407    #[rstest]
408    fn test_ws_url_spot_live() {
409        let url = get_ws_base_url(BinanceProductType::Spot, BinanceEnvironment::Live);
410        assert_eq!(url, "wss://stream.binance.com:9443/ws");
411    }
412
413    #[rstest]
414    fn test_ws_url_spot_demo() {
415        let url = get_ws_base_url(BinanceProductType::Spot, BinanceEnvironment::Demo);
416        assert_eq!(url, "wss://demo-stream.binance.com/ws");
417    }
418
419    #[rstest]
420    fn test_ws_url_usdm_live() {
421        let url = get_ws_base_url(BinanceProductType::UsdM, BinanceEnvironment::Live);
422        assert_eq!(url, "wss://fstream.binance.com/market/ws");
423    }
424
425    #[rstest]
426    fn test_ws_url_coinm_demo() {
427        let url = get_ws_base_url(BinanceProductType::CoinM, BinanceEnvironment::Demo);
428        assert_eq!(url, "wss://demo-dstream.binance.com/ws");
429    }
430
431    #[rstest]
432    fn test_ws_url_options_testnet() {
433        let url = get_ws_base_url(BinanceProductType::Options, BinanceEnvironment::Testnet);
434        assert_eq!(url, "wss://fstream.binancefuture.com/market/ws");
435    }
436
437    #[rstest]
438    fn test_ws_url_options_demo() {
439        let url = get_ws_base_url(BinanceProductType::Options, BinanceEnvironment::Demo);
440        assert_eq!(url, "wss://fstream.binancefuture.com/market/ws");
441    }
442
443    #[rstest]
444    fn test_ws_private_url_usdm_live() {
445        let url = get_ws_private_base_url(BinanceProductType::UsdM, BinanceEnvironment::Live);
446        assert_eq!(url, "wss://fstream.binance.com/private/ws");
447    }
448
449    #[rstest]
450    fn test_ws_private_url_fallback_to_market() {
451        let url = get_ws_private_base_url(BinanceProductType::Spot, BinanceEnvironment::Live);
452        assert_eq!(
453            url,
454            get_ws_base_url(BinanceProductType::Spot, BinanceEnvironment::Live)
455        );
456    }
457
458    #[rstest]
459    fn test_ws_public_url_usdm_live() {
460        let url = get_ws_public_base_url(BinanceProductType::UsdM, BinanceEnvironment::Live);
461        assert_eq!(url, "wss://fstream.binance.com/public/ws");
462    }
463
464    #[rstest]
465    fn test_ws_public_url_options_demo() {
466        let url = get_ws_public_base_url(BinanceProductType::Options, BinanceEnvironment::Demo);
467        assert_eq!(url, "wss://fstream.binancefuture.com/public/ws");
468    }
469
470    #[rstest]
471    fn test_ws_private_url_options_demo() {
472        let url = get_ws_private_base_url(BinanceProductType::Options, BinanceEnvironment::Demo);
473        assert_eq!(url, "wss://fstream.binancefuture.com/private/ws");
474    }
475
476    #[rstest]
477    #[case(
478        BinanceProductType::UsdM,
479        "wss://fstream.binance.com/private/ws",
480        "wss://fstream.binance.com/private/ws?listenKey=redacted"
481    )]
482    #[case(
483        BinanceProductType::CoinM,
484        "wss://dstream.binance.com/ws",
485        "wss://dstream.binance.com/ws/redacted"
486    )]
487    #[case(
488        BinanceProductType::CoinM,
489        "wss://dstream.binancefuture.com/ws",
490        "wss://dstream.binancefuture.com/ws/redacted"
491    )]
492    #[case(
493        BinanceProductType::CoinM,
494        "wss://demo-dstream.binance.com/ws",
495        "wss://demo-dstream.binance.com/ws/redacted"
496    )]
497    #[case(
498        BinanceProductType::CoinM,
499        "wss://custom.example/ws/",
500        "wss://custom.example/ws/redacted"
501    )]
502    #[case(
503        BinanceProductType::CoinM,
504        "wss://custom.example",
505        "wss://custom.example/ws/redacted"
506    )]
507    #[case(
508        BinanceProductType::UsdM,
509        "ws://127.0.0.1:1234/ws-inject",
510        "ws://127.0.0.1:1234/ws-inject?listenKey=redacted"
511    )]
512    #[case(
513        BinanceProductType::CoinM,
514        "ws://127.0.0.1:1234/ws-inject",
515        "ws://127.0.0.1:1234/ws-inject/redacted"
516    )]
517    fn test_futures_user_stream_url(
518        #[case] product_type: BinanceProductType,
519        #[case] base_url: &str,
520        #[case] expected: &str,
521    ) {
522        let url = get_futures_user_stream_url(product_type, base_url, "redacted");
523        assert_eq!(url, expected);
524    }
525
526    #[rstest]
527    #[should_panic(expected = "Futures user stream requires UsdM or CoinM product type")]
528    fn test_futures_user_stream_url_rejects_non_futures_product() {
529        let _ = get_futures_user_stream_url(
530            BinanceProductType::Spot,
531            "wss://stream.binance.com/ws",
532            "redacted",
533        );
534    }
535
536    #[rstest]
537    fn test_ws_public_url_fallback_to_market() {
538        let url = get_ws_public_base_url(BinanceProductType::Spot, BinanceEnvironment::Live);
539        assert_eq!(
540            url,
541            get_ws_base_url(BinanceProductType::Spot, BinanceEnvironment::Live)
542        );
543    }
544
545    #[rstest]
546    #[case(
547        "wss://fstream.binance.com",
548        "market",
549        "wss://fstream.binance.com/market/ws"
550    )]
551    #[case(
552        "wss://fstream.binance.com/ws",
553        "public",
554        "wss://fstream.binance.com/public/ws"
555    )]
556    #[case(
557        "wss://fstream.binance.com/market/ws",
558        "private",
559        "wss://fstream.binance.com/private/ws"
560    )]
561    #[case(
562        "wss://fstream-mm.binance.com",
563        "market",
564        "wss://fstream-mm.binance.com/market/ws"
565    )]
566    #[case(
567        "wss://fstream-mm.binance.com/ws",
568        "public",
569        "wss://fstream-mm.binance.com/public/ws"
570    )]
571    #[case(
572        "wss://fstream-auth.binance.com/market/ws",
573        "private",
574        "wss://fstream-auth.binance.com/private/ws"
575    )]
576    #[case(
577        "wss://fstream.binance.us",
578        "market",
579        "wss://fstream.binance.us/market/ws"
580    )]
581    fn test_usdm_ws_route_base_url_normalizes_override(
582        #[case] base_url: &str,
583        #[case] route: &str,
584        #[case] expected: &str,
585    ) {
586        let url = get_usdm_ws_route_base_url(base_url, route);
587        assert_eq!(url, expected);
588    }
589
590    #[rstest]
591    #[case("ws://127.0.0.1:9999/ws", "market")]
592    #[case("wss://other.example.com/private/ws", "private")]
593    #[case("ws://localhost:8080", "public")]
594    #[case("wss://other-fstream.binance.com.example.org/ws", "market")]
595    #[case("wss://fstream.binance.com.example.org/ws", "market")]
596    fn test_usdm_ws_route_base_url_passes_through_non_binance_host(
597        #[case] base_url: &str,
598        #[case] route: &str,
599    ) {
600        let url = get_usdm_ws_route_base_url(base_url, route);
601        assert_eq!(url, base_url);
602    }
603
604    #[rstest]
605    #[case(BinanceEnvironment::Live, Some("https://api.binance.com"))]
606    #[case(BinanceEnvironment::Testnet, None)]
607    #[case(BinanceEnvironment::Demo, None)]
608    fn test_sapi_base_url(#[case] environment: BinanceEnvironment, #[case] expected: Option<&str>) {
609        let url = get_sapi_base_url(environment);
610        assert_eq!(url, expected);
611    }
612
613    #[rstest]
614    #[case(BinanceEnvironment::Live, Some("https://papi.binance.com"))]
615    #[case(BinanceEnvironment::Testnet, None)]
616    #[case(BinanceEnvironment::Demo, None)]
617    fn test_papi_base_url(#[case] environment: BinanceEnvironment, #[case] expected: Option<&str>) {
618        let url = get_papi_base_url(environment);
619        assert_eq!(url, expected);
620    }
621}