tez 0.0.3

A lightweight, ergonomic, and fast high-level HTTP client for Rust
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
//! Integration specifications for Tez's response compression policy.

use std::collections::BTreeSet;

#[cfg(feature = "gzip")]
use tez::BodyExt as _;
use tez::{Client, header};
#[cfg(feature = "gzip")]
use tez::{StatusCode, Timeouts};

#[path = "harness/httpmock.rs"]
mod httpmock;

const DECODED: &[u8] = b"Tez compression fixture.\n";

const GZIP: &[u8] = &[
    31, 139, 8, 0, 0, 0, 0, 0, 0, 19, 11, 73, 173, 82, 72, 206, 207, 45, 40, 74, 45, 46, 206, 204,
    207, 83, 72, 203, 172, 40, 41, 45, 74, 213, 227, 2, 0, 58, 171, 44, 106, 25, 0, 0, 0,
];

const DEFLATE: &[u8] = &[
    120, 156, 11, 73, 173, 82, 72, 206, 207, 45, 40, 74, 45, 46, 206, 204, 207, 83, 72, 203, 172,
    40, 41, 45, 74, 213, 227, 2, 0, 126, 45, 9, 101,
];

const BROTLI: &[u8] = &[
    11, 12, 128, 84, 101, 122, 32, 99, 111, 109, 112, 114, 101, 115, 115, 105, 111, 110, 32, 102,
    105, 120, 116, 117, 114, 101, 46, 10, 3,
];

const ZSTD: &[u8] = &[
    40, 181, 47, 253, 32, 25, 201, 0, 0, 84, 101, 122, 32, 99, 111, 109, 112, 114, 101, 115, 115,
    105, 111, 110, 32, 102, 105, 120, 116, 117, 114, 101, 46, 10,
];

#[derive(Clone, Copy)]
struct Codec {
    feature: &'static str,
    token: &'static str,
    fixture: &'static [u8],
    enabled: bool,
}

const CODECS: [Codec; 4] = [
    Codec {
        feature: "gzip",
        token: "gzip",
        fixture: GZIP,
        enabled: cfg!(feature = "gzip"),
    },
    Codec {
        feature: "brotli",
        token: "br",
        fixture: BROTLI,
        enabled: cfg!(feature = "brotli"),
    },
    Codec {
        feature: "deflate",
        token: "deflate",
        fixture: DEFLATE,
        enabled: cfg!(feature = "deflate"),
    },
    Codec {
        feature: "zstd",
        token: "zstd",
        fixture: ZSTD,
        enabled: cfg!(feature = "zstd"),
    },
];

#[tokio::test]
async fn enabled_features_are_advertised_and_decoded() {
    let server = httpmock::Server::start(|request| {
        let codec = codec_for_path(request.path_and_query());
        httpmock::MockResponse::ok(codec.fixture)
            .with_header(header::CONTENT_ENCODING.as_str(), codec.token)
    })
    .await;
    let client = Client::new().unwrap();

    for codec in CODECS {
        let response = client
            .get(format!("{}/{}", server.base_url(), codec.feature))
            .send()
            .await
            .unwrap();

        if codec.enabled {
            assert_eq!(
                response.headers().get(header::CONTENT_ENCODING),
                None,
                "{} Content-Encoding",
                codec.feature
            );
            assert_eq!(
                response.headers().get(header::CONTENT_LENGTH),
                None,
                "{} Content-Length",
                codec.feature
            );
            assert_eq!(response.content_length(), None, "{}", codec.feature);
            assert_eq!(
                response.bytes().await.unwrap(),
                DECODED,
                "{}",
                codec.feature
            );
        } else {
            assert_eq!(
                response.headers()[header::CONTENT_ENCODING],
                codec.token,
                "{} Content-Encoding",
                codec.feature
            );
            assert_eq!(
                response.content_length(),
                Some(codec.fixture.len() as u64),
                "{}",
                codec.feature
            );
            assert_eq!(
                response.bytes().await.unwrap(),
                codec.fixture,
                "{}",
                codec.feature
            );
        }
    }

    let expected = CODECS
        .iter()
        .filter(|codec| codec.enabled)
        .map(|codec| codec.token)
        .collect::<BTreeSet<_>>();
    for request in server.recorded_requests() {
        assert_eq!(
            encoding_tokens(request.header(header::ACCEPT_ENCODING.as_str())),
            expected
        );
    }

    server.shutdown().await;
}

#[tokio::test]
async fn disable_compression_preserves_encoded_responses() {
    let server = httpmock::Server::start(|request| {
        let codec = codec_for_path(request.path_and_query());
        httpmock::MockResponse::ok(codec.fixture)
            .with_header(header::CONTENT_ENCODING.as_str(), codec.token)
    })
    .await;
    let client = Client::builder().disable_compression().build().unwrap();

    for codec in CODECS {
        let response = client
            .get(format!("{}/{}", server.base_url(), codec.feature))
            .send()
            .await
            .unwrap();
        assert_eq!(
            response.headers()[header::CONTENT_ENCODING],
            codec.token,
            "{} Content-Encoding",
            codec.feature
        );
        assert_eq!(
            response.headers()[header::CONTENT_LENGTH],
            codec.fixture.len().to_string(),
            "{} Content-Length",
            codec.feature
        );
        assert_eq!(
            response.content_length(),
            Some(codec.fixture.len() as u64),
            "{}",
            codec.feature
        );
        assert_eq!(
            response.bytes().await.unwrap(),
            codec.fixture,
            "{}",
            codec.feature
        );
    }

    let response = client
        .get(format!("{}/gzip", server.base_url()))
        .header(header::ACCEPT_ENCODING, "gzip")
        .send()
        .await
        .unwrap();
    assert_eq!(response.headers()[header::CONTENT_ENCODING], "gzip");
    assert_eq!(response.bytes().await.unwrap(), GZIP);

    let requests = server.recorded_requests();
    for request in &requests[..CODECS.len()] {
        assert_eq!(request.header(header::ACCEPT_ENCODING.as_str()), None);
    }
    assert_eq!(
        requests[CODECS.len()].header(header::ACCEPT_ENCODING.as_str()),
        Some("gzip")
    );

    let debug = format!("{client:?}");
    assert!(debug.contains("decompresses_responses: false"));
    assert!(!debug.contains("Accept-Encoding"));
    server.shutdown().await;
}

#[tokio::test]
async fn range_requests_do_not_implicitly_advertise_compression() {
    let server = httpmock::Server::start(|_| httpmock::MockResponse::ok("partial")).await;
    Client::new()
        .unwrap()
        .get(server.base_url())
        .header(header::RANGE, "bytes=0-6")
        .send()
        .await
        .unwrap()
        .discard()
        .await
        .unwrap();

    let request = &server.recorded_requests()[0];
    assert_eq!(request.header(header::ACCEPT_ENCODING.as_str()), None);
    server.shutdown().await;
}

#[cfg(feature = "gzip")]
#[tokio::test]
async fn range_responses_are_not_transparently_decoded() {
    let server = httpmock::Server::start(|_| {
        httpmock::MockResponse::with_status(StatusCode::PARTIAL_CONTENT, GZIP)
            .with_header(header::CONTENT_ENCODING.as_str(), "gzip")
    })
    .await;
    let response = Client::new()
        .unwrap()
        .get(server.base_url())
        .header(header::RANGE, "bytes=0-6")
        .header(header::ACCEPT_ENCODING, "gzip")
        .send()
        .await
        .unwrap();

    assert_eq!(response.headers()[header::CONTENT_ENCODING], "gzip");
    assert_eq!(response.bytes().await.unwrap(), GZIP);
    server.shutdown().await;
}

#[cfg(feature = "gzip")]
#[tokio::test]
async fn bodyless_responses_are_not_transparently_decoded() {
    let server = httpmock::Server::start(|request| {
        let status = match request.path_and_query() {
            "/not-modified" => StatusCode::NOT_MODIFIED,
            "/reset-content" => StatusCode::RESET_CONTENT,
            _ => StatusCode::NO_CONTENT,
        };
        httpmock::MockResponse::with_status(status, "")
            .with_header(header::CONTENT_ENCODING.as_str(), "gzip")
    })
    .await;
    let client = Client::new().unwrap();

    for path in ["no-content", "reset-content", "not-modified"] {
        let response = client
            .get(format!("{}/{path}", server.base_url()))
            .send()
            .await
            .unwrap();
        assert_eq!(response.headers()[header::CONTENT_ENCODING], "gzip");
        assert!(response.bytes().await.unwrap().is_empty());
    }

    let response = client
        .head(format!("{}/head", server.base_url()))
        .send()
        .await
        .unwrap();
    assert_eq!(response.headers()[header::CONTENT_ENCODING], "gzip");
    assert!(response.bytes().await.unwrap().is_empty());

    server.shutdown().await;
}

#[cfg(feature = "gzip")]
#[tokio::test]
async fn compressed_body_timeouts_remain_timeouts() {
    let server = httpmock::Server::start(|_| {
        httpmock::MockResponse::ok(GZIP)
            .with_header(header::CONTENT_ENCODING.as_str(), "gzip")
            .with_body_delay(std::time::Duration::from_millis(100))
    })
    .await;
    let client = Client::builder()
        .timeouts(
            Timeouts::default()
                .total(std::time::Duration::from_secs(1))
                .read(std::time::Duration::from_millis(20)),
        )
        .build()
        .unwrap();

    let error = client
        .get(server.base_url())
        .send()
        .await
        .unwrap()
        .bytes()
        .await
        .unwrap_err();
    assert!(error.is_timeout());
    server.shutdown().await;
}

#[cfg(feature = "gzip")]
#[tokio::test]
async fn explicit_accept_encoding_is_decoded_when_compression_is_enabled() {
    let server = httpmock::Server::start(|_| {
        httpmock::MockResponse::ok(GZIP).with_header(header::CONTENT_ENCODING.as_str(), "gzip")
    })
    .await;
    let response = Client::new()
        .unwrap()
        .get(server.base_url())
        .header(header::ACCEPT_ENCODING, "gzip")
        .send()
        .await
        .unwrap();

    assert_eq!(response.headers().get(header::CONTENT_ENCODING), None);
    assert_eq!(response.headers().get(header::CONTENT_LENGTH), None);
    assert_eq!(response.content_length(), None);
    assert_eq!(response.bytes().await.unwrap(), DECODED);

    let request = server
        .recorded_requests()
        .into_iter()
        .next()
        .expect("the request must be recorded");
    assert_eq!(
        request.header(header::ACCEPT_ENCODING.as_str()),
        Some("gzip")
    );
    server.shutdown().await;
}

#[cfg(feature = "gzip")]
#[tokio::test]
async fn repeated_content_encoding_fields_are_decoded_in_wire_order() {
    let server = httpmock::Server::start(|_| {
        httpmock::MockResponse::ok(GZIP)
            .with_header(header::CONTENT_ENCODING.as_str(), "identity")
            .with_header(header::CONTENT_ENCODING.as_str(), "gzip")
    })
    .await;
    let response = Client::new()
        .unwrap()
        .get(server.base_url())
        .send()
        .await
        .unwrap();

    assert_eq!(response.headers().get(header::CONTENT_ENCODING), None);
    assert_eq!(response.bytes().await.unwrap(), DECODED);
    server.shutdown().await;
}

#[cfg(feature = "gzip")]
#[tokio::test]
async fn decoded_responses_preserve_http_trailers_after_the_data() {
    let server = httpmock::Server::start(|_| {
        httpmock::MockResponse::ok(GZIP)
            .with_header(header::CONTENT_ENCODING.as_str(), "gzip")
            .with_trailer("x-checksum", "complete")
    })
    .await;
    let mut body = Client::new()
        .unwrap()
        .get(server.base_url())
        .header(header::TE, "trailers")
        .send()
        .await
        .unwrap()
        .into_body();
    let mut decoded = Vec::new();
    let mut checksum = None;

    while let Some(frame) = body.frame().await {
        let frame = frame.unwrap();
        match frame.into_data() {
            Ok(data) => decoded.extend_from_slice(&data),
            Err(frame) => {
                checksum = frame
                    .into_trailers()
                    .ok()
                    .and_then(|mut trailers| trailers.remove("x-checksum"));
            }
        }
    }

    assert_eq!(decoded, DECODED);
    assert_eq!(checksum.unwrap(), "complete");
    server.shutdown().await;
}

fn codec_for_path(path: &str) -> Codec {
    CODECS
        .iter()
        .copied()
        .find(|codec| path == format!("/{}", codec.feature))
        .expect("the request path must select a codec fixture")
}

fn encoding_tokens(value: Option<&str>) -> BTreeSet<&str> {
    value
        .into_iter()
        .flat_map(|value| value.split(','))
        .map(str::trim)
        .filter(|token| !token.is_empty())
        .collect()
}