rerout 0.6.0

Official Rust SDK for the Rerout branded-link API.
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
//! Integration tests for the [`Rerout`] client: construction, transport,
//! auth headers, query params, error parsing, and synthetic codes.

use rerout::{CreateLinkInput, Environment, ListLinksParams, Rerout, ReroutError};
use wiremock::matchers::{body_json_string, header, header_exists, method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

/// A minimal `Link` JSON envelope the server returns for create/get/update.
fn link_json(code: &str) -> serde_json::Value {
    serde_json::json!({
        "code": code,
        "short_url": format!("https://rerout.co/{code}"),
        "domain_hostname": null,
        "target_url": "https://example.com/sale",
        "project_id": "prj_123",
        "expires_at": null,
        "is_active": true,
        "seo_title": null,
        "seo_description": null,
        "seo_image_url": null,
        "seo_canonical_url": null,
        "seo_noindex": false,
        "seo_updated_at": null,
        "created_at": 1_700_000_000,
        "updated_at": 1_700_000_000,
    })
}

// ─── Constructor ─────────────────────────────────────────────────────────────

#[test]
fn new_rejects_blank_api_key() {
    let err = Rerout::new("").expect_err("blank key must fail");
    assert!(matches!(err, ReroutError::Config { .. }));
    assert_eq!(err.code(), "missing_api_key");
    assert_eq!(err.status(), 0);
}

#[test]
fn new_rejects_whitespace_only_api_key() {
    let err = Rerout::new("   ").expect_err("whitespace key must fail");
    assert_eq!(err.code(), "missing_api_key");
}

#[test]
fn builder_accepts_a_valid_api_key() {
    let client = Rerout::new("rrk_live_abc").expect("valid key builds");
    assert_eq!(client.base_url(), "https://api.rerout.co");
}

#[test]
fn base_url_defaults_to_production() {
    let client = Rerout::builder("rrk_x").build().unwrap();
    assert_eq!(client.base_url(), rerout::DEFAULT_BASE_URL);
}

#[test]
fn sandbox_environment_is_explicit_and_fail_closed() {
    let key = format!("rrk_test_{}", "a".repeat(32));
    let client = Rerout::builder(&key).sandbox().build().unwrap();
    assert_eq!(client.base_url(), rerout::SANDBOX_BASE_URL);
    assert!(Rerout::builder(&key).build().is_err());
    assert!(
        Rerout::builder(format!("rrk_{}", "b".repeat(32)))
            .environment(Environment::Sandbox)
            .build()
            .is_err()
    );
}

#[test]
fn base_url_trims_trailing_slashes() {
    let client = Rerout::builder("rrk_x")
        .base_url("https://api.staging.rerout.co///")
        .unwrap()
        .build()
        .unwrap();
    assert_eq!(client.base_url(), "https://api.staging.rerout.co");
}

#[test]
fn base_url_rejects_empty_string() {
    let err = Rerout::builder("rrk_x")
        .base_url("   /// ")
        .expect_err("blank base_url must fail");
    assert_eq!(err.code(), "invalid_base_url");
}

#[test]
fn base_url_rejects_unparseable_url() {
    let err = Rerout::builder("rrk_x")
        .base_url("not a url")
        .expect_err("garbage base_url must fail");
    assert_eq!(err.code(), "invalid_base_url");
}

#[test]
fn namespaces_are_present() {
    let client = Rerout::new("rrk_x").unwrap();
    // The namespace accessors return; reaching here means they all exist.
    let _ = client.links();
    let _ = client.project();
    let _ = client.qr();
    let _ = client.webhooks();
    let _ = client.conversions();
    let _ = client.tags();
}

#[test]
fn client_is_cloneable() {
    let client = Rerout::new("rrk_x").unwrap();
    let cloned = client.clone();
    assert_eq!(client.base_url(), cloned.base_url());
}

// ─── Request transport ──────────────────────────────────────────────────────

#[tokio::test]
async fn sends_bearer_authorization_header() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/links/abc"))
        .and(header("authorization", "Bearer rrk_secret_token"))
        .respond_with(ResponseTemplate::new(200).set_body_json(link_json("abc")))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_secret_token")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();

    let link = client.links().get("abc").await.expect("call succeeds");
    assert_eq!(link.code, "abc");
}

#[tokio::test]
async fn sends_accept_application_json_header() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/links/abc"))
        .and(header("accept", "application/json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(link_json("abc")))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    client.links().get("abc").await.expect("call succeeds");
}

#[tokio::test]
async fn sends_content_type_only_when_a_body_is_present() {
    let server = MockServer::start().await;
    // POST carries a body — content-type must be present.
    Mock::given(method("POST"))
        .and(path("/v1/links"))
        .and(header("content-type", "application/json"))
        .respond_with(ResponseTemplate::new(200).set_body_json(link_json("new1")))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    client
        .links()
        .create(&CreateLinkInput::new("https://example.com/sale"))
        .await
        .expect("create succeeds");
}

#[tokio::test]
async fn omits_content_type_on_bodyless_get() {
    let server = MockServer::start().await;
    // A GET has no body; assert content-type is absent by failing the mock if present.
    Mock::given(method("GET"))
        .and(path("/v1/links/abc"))
        .respond_with(ResponseTemplate::new(200).set_body_json(link_json("abc")))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    client.links().get("abc").await.expect("get succeeds");

    let requests = server.received_requests().await.unwrap();
    let get = requests
        .iter()
        .find(|r| r.url.path() == "/v1/links/abc")
        .unwrap();
    assert!(
        get.headers.get("content-type").is_none(),
        "GET request must not carry a content-type header",
    );
}

#[tokio::test]
async fn serializes_the_json_request_body() {
    let server = MockServer::start().await;
    let expected = serde_json::json!({
        "target_url": "https://example.com/promo",
        "code": "promo",
    })
    .to_string();

    Mock::given(method("POST"))
        .and(path("/v1/links"))
        .and(body_json_string(expected))
        .respond_with(ResponseTemplate::new(200).set_body_json(link_json("promo")))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    let input = CreateLinkInput::new("https://example.com/promo").with_code("promo");
    let link = client
        .links()
        .create(&input)
        .await
        .expect("create succeeds");
    assert_eq!(link.code, "promo");
}

#[tokio::test]
async fn omits_optional_fields_from_the_create_body() {
    let server = MockServer::start().await;
    // Only `target_url` should appear — no nulls for the unset SEO fields.
    let expected = serde_json::json!({ "target_url": "https://example.com/x" }).to_string();
    Mock::given(method("POST"))
        .and(path("/v1/links"))
        .and(body_json_string(expected))
        .respond_with(ResponseTemplate::new(200).set_body_json(link_json("x1")))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    client
        .links()
        .create(&CreateLinkInput::new("https://example.com/x"))
        .await
        .expect("create succeeds");
}

#[tokio::test]
async fn forwards_cursor_and_limit_query_params() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/links"))
        .and(query_param("cursor", "42"))
        .and(query_param("limit", "10"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(serde_json::json!({ "links": [], "next_cursor": null })),
        )
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    let params = ListLinksParams {
        cursor: Some(42),
        limit: Some(10),
    };
    client.links().list(params).await.expect("list succeeds");
}

#[tokio::test]
async fn omits_query_params_when_list_params_are_empty() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/links"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(serde_json::json!({ "links": [], "next_cursor": null })),
        )
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    client
        .links()
        .list(ListLinksParams::default())
        .await
        .expect("list succeeds");

    let requests = server.received_requests().await.unwrap();
    let listing = requests
        .iter()
        .find(|r| r.url.path() == "/v1/links")
        .unwrap();
    assert_eq!(listing.url.query(), None, "no query string expected");
}

#[tokio::test]
async fn forwards_days_query_param_on_stats() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/projects/me/stats"))
        .and(query_param("days", "7"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "days": 7,
            "total_clicks": 0,
            "qr_scans": 0,
            "daily": [],
            "countries": [],
            "referrers": [],
            "devices": [],
            "browsers": [],
            "top_codes": [],
        })))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    let stats = client.project().stats(7).await.expect("stats succeeds");
    assert_eq!(stats.days, 7);
}

// ─── Error parsing ──────────────────────────────────────────────────────────

#[tokio::test]
async fn preserves_server_error_code_and_message() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/v1/links"))
        .respond_with(ResponseTemplate::new(400).set_body_json(serde_json::json!({
            "code": "bad_target_url",
            "message": "target_url must use https.",
        })))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    let err = client
        .links()
        .create(&CreateLinkInput::new("http://insecure.example"))
        .await
        .expect_err("400 must surface as an error");

    assert_eq!(err.code(), "bad_target_url");
    assert_eq!(err.status(), 400);
    assert_eq!(err.message(), "target_url must use https.");
    assert!(matches!(err, ReroutError::Api { .. }));
}

#[tokio::test]
async fn preserves_error_path_and_timestamp_details() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/links/missing"))
        .respond_with(ResponseTemplate::new(404).set_body_json(serde_json::json!({
            "code": "not_found",
            "message": "no such link",
            "path": "/v1/links/missing",
            "timestamp": "2026-05-20T12:00:00Z",
            "details": { "field": "code" },
        })))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    let err = client
        .links()
        .get("missing")
        .await
        .expect_err("404 must surface as an error");

    assert_eq!(err.path(), Some("/v1/links/missing"));
    assert_eq!(err.timestamp(), Some("2026-05-20T12:00:00Z"));
    assert_eq!(
        err.details()
            .and_then(|d| d.get("field"))
            .and_then(|v| v.as_str()),
        Some("code"),
    );
}

#[tokio::test]
async fn synthetic_code_for_401_with_no_body() {
    assert_synthetic_code(401, "unauthorized").await;
}

#[tokio::test]
async fn synthetic_code_for_403_with_no_body() {
    assert_synthetic_code(403, "forbidden").await;
}

#[tokio::test]
async fn synthetic_code_for_404_with_no_body() {
    assert_synthetic_code(404, "not_found").await;
}

#[tokio::test]
async fn synthetic_code_for_429_with_no_body() {
    let err = error_for_status(429).await;
    assert_eq!(err.code(), "rate_limited");
    assert!(err.is_rate_limited());
    assert!(!err.is_server_error());
}

#[tokio::test]
async fn synthetic_code_for_500_with_no_body() {
    let err = error_for_status(500).await;
    assert_eq!(err.code(), "server_error");
    assert!(err.is_server_error());
    assert!(!err.is_rate_limited());
}

#[tokio::test]
async fn synthetic_code_for_503_with_no_body() {
    let err = error_for_status(503).await;
    assert_eq!(err.code(), "server_error");
    assert!(err.is_server_error());
}

#[tokio::test]
async fn synthetic_code_for_generic_4xx_with_no_body() {
    assert_synthetic_code(418, "client_error").await;
}

#[tokio::test]
async fn synthetic_code_for_non_json_error_body() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/links/x"))
        .respond_with(ResponseTemplate::new(500).set_body_string("<html>down</html>"))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    let err = client.links().get("x").await.expect_err("500 errors");
    assert_eq!(err.code(), "server_error");
    assert_eq!(err.status(), 500);
}

#[tokio::test]
async fn network_failure_maps_to_network_error() {
    // Point at a port nothing listens on; the connection is refused.
    let client = Rerout::builder("rrk_x")
        .base_url("http://127.0.0.1:1")
        .unwrap()
        .build()
        .unwrap();
    let err = client
        .links()
        .get("x")
        .await
        .expect_err("connection refused");
    assert_eq!(err.code(), "network_error");
    assert_eq!(err.status(), 0);
    assert!(matches!(err, ReroutError::Network { .. }));
}

#[tokio::test]
async fn timeout_maps_to_timeout_error() {
    use std::time::Duration;

    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/links/slow"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_delay(Duration::from_secs(5))
                .set_body_json(link_json("slow")),
        )
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .timeout(Duration::from_millis(80))
        .build()
        .unwrap();
    let err = client.links().get("slow").await.expect_err("must time out");
    assert_eq!(err.code(), "timeout");
    assert_eq!(err.status(), 0);
    assert!(matches!(err, ReroutError::Timeout { .. }));
}

#[tokio::test]
async fn non_json_2xx_body_maps_to_decode_error() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/links/weird"))
        .respond_with(ResponseTemplate::new(200).set_body_string("definitely not json"))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    let err = client
        .links()
        .get("weird")
        .await
        .expect_err("non-JSON 2xx body must fail");
    assert!(matches!(err, ReroutError::Decode { .. }));
    assert_eq!(err.code(), "decode_error");
}

#[tokio::test]
async fn empty_2xx_body_maps_to_unexpected_response() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/links/empty"))
        .respond_with(ResponseTemplate::new(200).set_body_string(""))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    let err = client
        .links()
        .get("empty")
        .await
        .expect_err("empty 2xx body must fail");
    assert!(matches!(err, ReroutError::Unexpected { .. }));
    assert_eq!(err.code(), "unexpected_response");
}

#[tokio::test]
async fn user_agent_override_is_sent() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/projects/me"))
        .and(header_exists("user-agent"))
        .and(header("user-agent", "my-app/9.9"))
        .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
            "id": "prj_1",
            "name": "Acme",
            "slug": "acme",
        })))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .user_agent("my-app/9.9")
        .build()
        .unwrap();
    client.project().me().await.expect("me succeeds");
}

// ─── Helpers ────────────────────────────────────────────────────────────────

async fn error_for_status(status: u16) -> ReroutError {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/v1/links/x"))
        .respond_with(ResponseTemplate::new(status))
        .mount(&server)
        .await;

    let client = Rerout::builder("rrk_x")
        .base_url(server.uri())
        .unwrap()
        .build()
        .unwrap();
    client
        .links()
        .get("x")
        .await
        .expect_err("non-2xx status must surface as an error")
}

async fn assert_synthetic_code(status: u16, expected_code: &str) {
    let err = error_for_status(status).await;
    assert_eq!(err.code(), expected_code);
    assert_eq!(err.status(), status);
}