taskcluster 43.1.0

API client for Taskcluster; typically used via `taskcluster`
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
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
use crate::retry::Backoff;
use crate::util::collect_scopes;
use crate::{Credentials, Retry};
use anyhow::{anyhow, bail, Context, Error, Result};
use reqwest::header::HeaderValue;
use serde_json::json;
use serde_json::Value;
use std::iter::IntoIterator;
use std::str::FromStr;
use std::time::Duration;

/// ClientBuilder implements the builder pattern for building a Client, allowing
/// optional configuration of features such as authorized scopes and retry.
#[derive(Default, Debug, Clone)]
pub struct ClientBuilder {
    root_url: String,
    retry: Retry,
    credentials: Option<Credentials>,
    path_prefix: Option<String>,
    authorized_scopes: Option<Vec<String>>,
    timeout: Duration,
}

impl ClientBuilder {
    /// Create a new ClientBuilder.  The Taskcluster root URL is required and so must always be
    /// specified.
    pub fn new<S: Into<String>>(root_url: S) -> Self {
        Self {
            root_url: root_url.into(),
            timeout: Duration::from_secs(30),
            ..Self::default()
        }
    }

    /// Add credentials to the client
    pub fn credentials(mut self, credentials: Credentials) -> Self {
        self.credentials = Some(credentials);
        self
    }

    /// Set the retry configuration for the client
    pub fn retry(mut self, retry: Retry) -> Self {
        self.retry = retry;
        self
    }

    /// Set the timeout for each HTTP request made by the client.  The default is
    /// 30 seconds.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// Set the path_prefix; this will be included between the root URL and the path given to
    /// `request`, `make_url`, and `make_signed_url`.  This is typically used when building a
    /// client that will address a single service, such as `api/queue/v1/`.  The path prefix
    /// must not start with `/` and must end with a `/` character.  This is only for internal
    /// use in constructing service-specific clients that will always use the same path prefix.
    pub(crate) fn path_prefix<S: Into<String>>(mut self, path_prefix: S) -> Self {
        let path_prefix = path_prefix.into();
        debug_assert!(path_prefix.ends_with('/'));
        self.path_prefix = Some(path_prefix);
        self
    }

    /// Set the authorized scopes for this client.  These will be passed along with request, and
    /// included in signed URLs, and will act as a limit on the scopes available for the operation
    /// beyond those afforded by the credentials themselves.
    pub fn authorized_scopes(
        mut self,
        authorized_scopes: impl IntoIterator<Item = impl AsRef<str>>,
    ) -> Self {
        let authorized_scopes = collect_scopes(authorized_scopes);
        self.authorized_scopes = Some(authorized_scopes);
        self
    }

    /// Build the resulting client, consuming the builder
    pub fn build(self) -> Result<Client> {
        Client::new(self)
    }
}

impl From<String> for ClientBuilder {
    fn from(root_url: String) -> Self {
        Self::new(root_url)
    }
}

impl From<&str> for ClientBuilder {
    fn from(root_url: &str) -> Self {
        Self::new(root_url)
    }
}

/// Client is the entry point into all the functionality in this package. It
/// contains authentication credentials, and a service endpoint, which are
/// required for all HTTP operations.
pub struct Client {
    /// The credentials associated with this client and used for requests.
    /// If None, then unauthenticated requests are made.
    credentials: Option<hawk::Credentials>,

    /// The `ext` string for any requests made by this client, if any
    ext: Option<String>,

    /// Retry information.
    retry: Retry,

    /// The base URL for requests to the selected service / api version
    base_url: reqwest::Url,

    /// The host for the given root URL
    host: String,

    /// The port for the given root URL
    port: u16,

    /// Reqwest client
    client: reqwest::Client,
}

impl Client {
    /// Create a new client (public interface is via
    /// [`ClientBuilder::build`](crate::ClientBuilder::build))
    fn new(b: ClientBuilder) -> Result<Client> {
        // In general, try to pre-compute as much as possible here, so that later requests and
        // URL-generation operations are as fast as possible.  Once created, a Client is immutable.

        // build a base_url containing both the root URL and any path_prefix.  This allows
        // service-specific clients to provide only the portion of the path specific to
        // the API method being invoked.
        let mut base_url = reqwest::Url::parse(b.root_url.as_ref())
            .context(format!("while parsing {}", b.root_url))?;

        let host = base_url
            .host_str()
            .ok_or_else(|| anyhow!("The root URL {} doesn't contain a host", b.root_url))?
            .to_owned();

        let port = base_url
            .port_or_known_default()
            .ok_or_else(|| anyhow!("Unkown port for protocol {}", base_url.scheme()))?;

        if let Some(path_prefix) = b.path_prefix {
            base_url = base_url.join(path_prefix.as_ref()).context(format!(
                "while adding path_prefix to root_url {}",
                b.root_url
            ))?;
        }

        let retry = b.retry;
        let timeout = b.timeout;

        // build a reqwest client with the timeout configuration; this will also handle
        // connection re-use.
        let client = reqwest::Client::builder()
            .redirect(reqwest::redirect::Policy::none())
            .timeout(timeout)
            .build()?;

        // figure out the `certificate` and `authorizedScopes` parts of the ext property
        let mut certificate: Option<Value> = None;
        if let Some(Credentials {
            certificate: Some(ref cert_str),
            ..
        }) = b.credentials
        {
            certificate = Some(
                serde_json::from_str(cert_str)
                    .context("while parsing given certificate as JSON")?,
            );
        }

        let mut authorized_scopes: Option<Value> = None;
        if let Some(scopes) = b.authorized_scopes {
            authorized_scopes = Some(scopes.into());
        }

        let ext_json = match (certificate, authorized_scopes) {
            (Some(c), None) => Some(json!({ "certificate": c })),
            (None, Some(s)) => Some(json!({ "authorizedScopes": s })),
            (Some(c), Some(s)) => Some(json!({ "certificate": c, "authorizedScopes": s })),
            (None, None) => None,
        };

        let ext = if let Some(ext) = ext_json {
            let ext_str = serde_json::to_string(&ext)?;
            Some(base64::encode_config(ext_str, base64::URL_SAFE_NO_PAD))
        } else {
            None
        };

        // pre-generate the hawk::Credentials struct we will use to sign requests
        let credentials = match b.credentials {
            None => None,
            Some(c) => Some(hawk::Credentials {
                id: c.client_id.clone(),
                key: hawk::Key::new(&c.access_token, hawk::SHA256).context(c.client_id)?,
            }),
        };

        Ok(Client {
            credentials,
            ext,
            retry,
            base_url,
            host,
            port,
            client,
        })
    }

    /// Make a request to a Taskcluster deployment.  While the per-service methods are generally
    /// more convenient, this method can be used to call a path on the deployment directly.
    ///
    /// The request URI is `<root_url>/<path_prefix>/<path>`.  The `path` parameter must not start
    /// with `/`.
    ///
    /// This will automatically retry on server-side errors and return an error for client errors.
    /// Success and redirection responses are treated as OK.
    pub async fn request(
        &self,
        method: &str,
        path: &str,
        query: Option<Vec<(&str, &str)>>,
        body: Option<&Value>,
    ) -> Result<reqwest::Response, Error> {
        let mut backoff = Backoff::new(&self.retry);

        let req = self.build_request(method, path, query, body)?;
        let url = req.url().as_str();

        let mut retries = self.retry.retries;
        loop {
            let req = req
                .try_clone()
                .ok_or_else(|| anyhow!("Cannot clone the request {}", url))?;

            let retry_for;
            match self.client.execute(req).await {
                // From the request docs for Client::execute:
                // > This method fails if there was an error while sending request, redirect loop
                // > was detected or redirect limit was exhausted.
                // All cases where there's a successful HTTP response are Ok(..).
                Err(e) => {
                    retry_for = e;
                }

                // Retry for server errors
                Ok(resp) if resp.status().is_server_error() => {
                    retry_for = resp.error_for_status().err().unwrap();
                }

                // client errors do not get retried
                Ok(resp) if resp.status().is_client_error() => {
                    let err = resp.error_for_status_ref().err().unwrap();

                    // try to add context based on the message from the JSON body, falling back
                    // to just returning the reqwest::Error
                    if let Ok(json) = resp.json::<Value>().await {
                        if let Some(message) = json.get("message") {
                            if let Some(s) = message.as_str() {
                                return Err(Error::from(err).context(s.to_owned()));
                            }
                        }
                    }
                    return Err(err.into());
                }

                Ok(resp) => {
                    return Ok(resp);
                }
            };

            // if we got here, we are going to retry, or return the error if we are done
            // retrying.

            if retries == 0 {
                return Err(retry_for.into());
            }
            retries -= 1;

            match backoff.next_backoff() {
                Some(duration) => tokio::time::sleep(duration).await,
                None => return Err(retry_for.into()),
            }
        }
    }

    fn build_request(
        &self,
        method: &str,
        path: &str,
        query: Option<Vec<(&str, &str)>>,
        body: Option<&Value>,
    ) -> Result<reqwest::Request, Error> {
        if path.starts_with('/') {
            bail!("Request path must not begin with `/`");
        }

        let mut url = self.base_url.join(path)?;

        if let Some(q) = query {
            url.query_pairs_mut().extend_pairs(q);
        }

        let meth = reqwest::Method::from_str(method)?;

        let req = self.client.request(meth, url);

        let req = match body {
            Some(b) => req.json(&b),
            None => req,
        };

        let req = req.build()?;

        match self.credentials {
            Some(ref creds) => self.sign_request(creds, req),
            None => Ok(req),
        }
    }

    fn sign_request(
        &self,
        creds: &hawk::Credentials,
        req: reqwest::Request,
    ) -> Result<reqwest::Request, Error> {
        let mut signed_req_builder = hawk::RequestBuilder::new(
            req.method().as_str(),
            &self.host,
            self.port,
            req.url().path(),
        );

        // hash the payload, if there is one
        let payload_hash;
        if let Some(ref b) = req.body() {
            let b = b
                .as_bytes()
                .ok_or_else(|| anyhow!("stream request bodies are not supported"))?;
            payload_hash = hawk::PayloadHasher::hash("application/json", hawk::SHA256, b)?;
            signed_req_builder = signed_req_builder.hash(&payload_hash[..])
        }

        signed_req_builder = signed_req_builder.ext(self.ext.as_ref().map(|s| s.as_ref()));

        let header = signed_req_builder.request().make_header(&creds)?;

        let token = HeaderValue::from_str(format!("Hawk {}", header).as_str()).context(header)?;

        let mut req = req;
        req.headers_mut().insert("Authorization", token);
        Ok(req)
    }

    /// Make a URL for the given path, constructed as for [`request`](crate::Client::request).  The
    /// path should not begin with a `/`.
    pub fn make_url(&self, path: &str, query: Option<Vec<(&str, &str)>>) -> Result<String> {
        if path.starts_with('/') {
            bail!("Request path must not begin with `/`");
        }

        let mut url = self.base_url.join(path)?;

        if let Some(q) = query {
            url.query_pairs_mut().extend_pairs(q);
        }
        Ok(url.as_ref().to_owned())
    }

    /// Make a signed URL for the given path, constructed as for
    /// [`request`](crate::Client::request).  The path should not begin with a `/`.  The URL will
    /// be valid for the given duration, and carries the client's scopes (including any
    /// authorized_scopes setting).
    pub fn make_signed_url(
        &self,
        path: &str,
        query: Option<Vec<(&str, &str)>>,
        ttl: Duration,
    ) -> Result<String> {
        if path.starts_with('/') {
            bail!("Request path must not begin with `/`");
        }

        let creds = if let Some(ref creds) = self.credentials {
            creds
        } else {
            return Err(anyhow!("Cannot sign a URL without credentials"));
        };

        let mut url = self.base_url.join(path)?;
        if let Some(q) = query {
            url.query_pairs_mut().extend_pairs(q);
        }

        // generate a full path containing the query
        let path_with_query = match url.query() {
            Some(q) => format!("{}?{}", url.path(), q),
            None => url.path().to_owned(),
        };

        let req = hawk::RequestBuilder::new("GET", &self.host, self.port, &path_with_query)
            .ext(self.ext.as_ref().map(|s| s.as_ref()))
            .request();

        let bewit = req.make_bewit_with_ttl(creds, ttl)?;

        url.query_pairs_mut().append_pair("bewit", &bewit.to_str());
        Ok(url.as_ref().to_owned())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::err_status_code;
    use anyhow::bail;
    use httptest::{matchers::*, responders::*, Expectation, Server};
    use serde_json::json;
    use std::fmt;
    use std::net::SocketAddr;
    use std::time::Duration;
    use tokio;

    /// An httptest matcher that will check Hawk authentication with the given cedentials.
    pub fn signed_with(creds: Credentials, addr: SocketAddr) -> SignedWith {
        SignedWith(creds, addr)
    }

    #[derive(Debug)]
    pub struct SignedWith(Credentials, SocketAddr);

    impl<B> Matcher<httptest::http::Request<B>> for SignedWith {
        fn matches(
            &mut self,
            input: &httptest::http::Request<B>,
            _ctx: &mut ExecutionContext,
        ) -> bool {
            let auth_header = input
                .headers()
                .get(httptest::http::header::AUTHORIZATION)
                .unwrap();
            let auth_header = auth_header.to_str().unwrap();
            if !auth_header.starts_with("Hawk ") {
                println!("Authorization header does not start with Hawk");
                return false;
            }
            let auth_header: hawk::Header = auth_header[5..].parse().unwrap();

            let host = format!("{}", self.1.ip());
            let hawk_req = hawk::RequestBuilder::new(
                input.method().as_str(),
                &host,
                self.1.port(),
                input.uri().path(),
            )
            .request();

            let key = hawk::Key::new(&self.0.access_token, hawk::SHA256).unwrap();

            // this ts_skew duration needs to be large -- in CI, somehow 1s can elapse between
            // a request and the invocation of a matcher.
            if !hawk_req.validate_header(&auth_header, &key, Duration::from_secs(60)) {
                println!("Validation failed");
                return false;
            }

            true
        }

        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            <Self as fmt::Debug>::fmt(self, f)
        }
    }

    fn get_authorized_scopes(client: &Client) -> Result<Vec<String>> {
        let ext = if let Some(ref ext) = client.ext {
            ext
        } else {
            bail!("client has no ext")
        };

        let ext = base64::decode(ext)?;

        #[derive(serde::Deserialize)]
        #[serde(rename_all = "camelCase")]
        struct Certificate {
            authorized_scopes: Vec<String>,
        }

        let ext = serde_json::from_slice::<Certificate>(&ext)?;
        Ok(ext.authorized_scopes)
    }

    #[test]
    fn test_authorized_scopes_vec() {
        let client = ClientBuilder::new("https://tc-tests.example.com")
            .authorized_scopes(vec!["a-scope"])
            .build()
            .unwrap();
        assert_eq!(get_authorized_scopes(&client).unwrap(), vec!["a-scope"]);
    }

    #[test]
    fn test_authorized_scopes_iter() {
        let nums = vec![1, 2, 3];
        let client = ClientBuilder::new("https://tc-tests.example.com")
            .authorized_scopes(nums.iter().map(|n| format!("scope:{}", n)))
            .build()
            .unwrap();
        assert_eq!(
            get_authorized_scopes(&client).unwrap(),
            vec!["scope:1", "scope:2", "scope:3"]
        );
    }

    #[tokio::test]
    async fn test_simple_request() -> Result<(), Error> {
        let server = Server::run();
        server.expect(
            Expectation::matching(request::method_path("GET", "/api/queue/v1/ping"))
                .respond_with(status_code(200)),
        );
        let root_url = format!("http://{}", server.addr());

        let client = ClientBuilder::new(&root_url)
            .path_prefix("api/queue/v1/")
            .build()?;
        let resp = client.request("GET", "ping", None, None).await?;
        assert!(resp.status().is_success());
        Ok(())
    }

    #[tokio::test]
    async fn test_timeout() -> Result<(), Error> {
        let server = Server::run();
        server.expect(
            Expectation::matching(request::method_path("GET", "/api/queue/v1/ping")).respond_with(
                // note that the tests do not wait for this to actually time out,
                // so this is a very long delay to avoid any test intermittency
                delay_and_then(Duration::from_secs(30), status_code(200)),
            ),
        );
        let root_url = format!("http://{}", server.addr());

        let client = ClientBuilder::new(&root_url)
            .path_prefix("api/queue/v1/")
            .timeout(Duration::from_millis(5))
            .retry(Retry {
                retries: 0,
                ..Default::default()
            })
            .build()?;
        let err = client.request("GET", "ping", None, None).await.unwrap_err();
        let reqerr = err.downcast::<reqwest::Error>().unwrap();
        assert!(reqerr.is_timeout());
        Ok(())
    }

    #[tokio::test]
    async fn test_simple_request_with_perm_creds() -> Result<(), Error> {
        let creds = Credentials::new("clientId", "accessToken");

        let server = Server::run();
        server.expect(
            Expectation::matching(all_of![
                request::method_path("GET", "/api/queue/v1/ping"),
                signed_with(creds.clone(), server.addr()),
            ])
            .respond_with(status_code(200)),
        );
        let root_url = format!("http://{}", server.addr());

        let client = ClientBuilder::new(&root_url)
            .path_prefix("api/queue/v1/")
            .credentials(creds)
            .build()?;
        let resp = client.request("GET", "ping", None, None).await?;
        assert!(resp.status().is_success());
        Ok(())
    }

    #[tokio::test]
    async fn test_query() -> Result<(), Error> {
        let server = Server::run();
        server.expect(
            Expectation::matching(all_of![
                request::method_path("GET", "/api/queue/v1/test"),
                request::query(url_decoded(contains(("taskcluster", "test")))),
                request::query(url_decoded(contains(("client", "rust")))),
            ])
            .respond_with(status_code(200)),
        );
        let root_url = format!("http://{}", server.addr());

        let client = ClientBuilder::new(&root_url)
            .path_prefix("api/queue/v1/")
            .build()?;
        let resp = client
            .request(
                "GET",
                "test",
                Some(vec![("taskcluster", "test"), ("client", "rust")]),
                None,
            )
            .await?;
        assert!(resp.status().is_success());
        Ok(())
    }

    #[tokio::test]
    async fn test_body() -> Result<(), Error> {
        let body = json!({"hello": "world"});

        let server = Server::run();
        server.expect(
            Expectation::matching(all_of![
                request::method_path("POST", "/api/queue/v1/test"),
                request::body(json_decoded(eq(body.clone()))),
            ])
            .respond_with(status_code(200)),
        );
        let root_url = format!("http://{}", server.addr());

        let client = ClientBuilder::new(&root_url)
            .path_prefix("api/queue/v1/")
            .build()?;
        let resp = client.request("POST", "test", None, Some(&body)).await?;
        assert!(resp.status().is_success());
        Ok(())
    }

    #[test]
    fn make_url_simple() -> Result<(), Error> {
        let client = ClientBuilder::new("https://tc-test.example.com")
            .path_prefix("api/queue/v1/")
            .build()?;
        let url = client.make_url("ping", None)?;
        assert_eq!(url, "https://tc-test.example.com/api/queue/v1/ping");
        Ok(())
    }

    #[test]
    fn make_url_escapable_characters() -> Result<(), Error> {
        let client = ClientBuilder::new("https://tc-test.example.com")
            .path_prefix("api/queue/v1/")
            .build()?;
        let url = client.make_url("escape%2Fthis!", None)?;
        assert_eq!(
            url,
            "https://tc-test.example.com/api/queue/v1/escape%2Fthis!"
        );
        Ok(())
    }

    #[test]
    fn make_url_query() -> Result<(), Error> {
        let client = ClientBuilder::new("https://tc-test.example.com")
            .path_prefix("api/queue/v1/")
            .build()?;
        let url = client.make_url("a/b/c", Some(vec![("abc", "def"), ("x!z", "1/3")]))?;
        assert_eq!(
            url,
            "https://tc-test.example.com/api/queue/v1/a/b/c?abc=def&x%21z=1%2F3"
        );
        Ok(())
    }

    #[test]
    fn make_signed_url_simple() -> Result<(), Error> {
        let creds = Credentials::new("clientId", "accessToken");
        let client = ClientBuilder::new("https://tc-test.example.com")
            .path_prefix("api/queue/v1/")
            .credentials(creds)
            .build()?;
        let url = client.make_signed_url("a/b", None, Duration::from_secs(10))?;
        assert!(url.starts_with("https://tc-test.example.com/api/queue/v1/a/b?bewit="));
        Ok(())
    }

    #[test]
    fn make_signed_url_query() -> Result<(), Error> {
        let creds = Credentials::new("clientId", "accessToken");
        let client = ClientBuilder::new("https://tc-test.example.com")
            .path_prefix("api/queue/v1/")
            .credentials(creds)
            .build()?;
        let url = client.make_signed_url(
            "a/b/c",
            Some(vec![("abc", "def"), ("xyz", "1/3")]),
            Duration::from_secs(10),
        )?;
        assert!(url.starts_with(
            "https://tc-test.example.com/api/queue/v1/a/b/c?abc=def&xyz=1%2F3&bewit="
        ));
        Ok(())
    }

    fn retry_fast() -> Retry {
        Retry {
            retries: 6,
            max_delay: Duration::from_millis(1),
            ..Default::default()
        }
    }

    #[tokio::test]
    async fn test_500_retry() -> Result<(), Error> {
        let server = Server::run();
        server.expect(
            Expectation::matching(request::method_path("GET", "/api/queue/v1/test"))
                .times(7) // 1 try, 6 retries
                .respond_with(status_code(500)),
        );
        let root_url = format!("http://{}", server.addr());
        let client = ClientBuilder::new(root_url)
            .path_prefix("api/queue/v1/")
            .retry(retry_fast())
            .build()?;

        let result = client.request("GET", "test", None, None).await;
        assert!(result.is_err());
        let reqw_err: reqwest::Error = result.err().unwrap().downcast()?;
        assert_eq!(reqw_err.status().unwrap(), 500);
        Ok(())
    }

    #[tokio::test]
    async fn test_400_no_retry() -> Result<(), Error> {
        let server = Server::run();
        server.expect(
            Expectation::matching(request::method_path("GET", "/api/queue/v1/test"))
                .times(1)
                .respond_with(status_code(400)),
        );
        let root_url = format!("http://{}", server.addr());
        let client = ClientBuilder::new(root_url)
            .path_prefix("api/queue/v1/")
            .retry(retry_fast())
            .build()?;

        let result = client.request("GET", "test", None, None).await;
        assert!(result.is_err());
        assert_eq!(
            err_status_code(&result.err().unwrap()),
            Some(reqwest::StatusCode::BAD_REQUEST)
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_303_no_follow() -> Result<(), Error> {
        let server = Server::run();
        server.expect(
            Expectation::matching(request::method_path("GET", "/api/queue/v1/test"))
                .times(1)
                // should not follow this redirect..
                .respond_with(
                    status_code(303)
                        .insert_header("location", "http://httpstat.us/404")
                        .insert_header("content-type", "application/json")
                        .body("{\"url\":\"http://httpstat.us/404\"}"),
                ),
        );
        let root_url = format!("http://{}", server.addr());
        let client = ClientBuilder::new(root_url)
            .path_prefix("api/queue/v1/")
            .retry(retry_fast())
            .build()?;

        let resp = client.request("GET", "test", None, None).await?;
        assert_eq!(resp.status(), 303);
        assert_eq!(
            resp.json::<serde_json::Value>().await?,
            json!({"url": "http://httpstat.us/404"})
        );
        Ok(())
    }
}