File: encoding.rs

package info (click to toggle)
rust-isahc 1.7.2%2Bds-20
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,044 kB
  • sloc: perl: 258; python: 148; makefile: 24; sh: 1
file content (148 lines) | stat: -rw-r--r-- 3,838 bytes parent folder | download | duplicates (2)
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
use flate2::{
    read::{DeflateEncoder, GzEncoder},
    Compression,
};
use isahc::{prelude::*, Request};
use std::io::Read;
use testserver::mock;

#[test]
fn gzip_encoded_response_is_decoded_automatically() {
    let body = "hello world";
    let mut body_encoded = Vec::new();

    GzEncoder::new(body.as_bytes(), Compression::default())
        .read_to_end(&mut body_encoded)
        .unwrap();

    let m = mock! {
        headers {
            "Content-Encoding": "gzip",
        }
        body: body_encoded.clone(),
    };

    let mut response = isahc::get(m.url()).unwrap();

    //assert_eq!(response.text().unwrap(), body);
    m.request()
        .expect_header("Accept-Encoding", "deflate, gzip");

    // Response body size should be unknown, because the actual content is
    // gzipped.
    assert_eq!(response.body().len(), None);
}

#[test]
fn request_gzip_without_automatic_decompression() {
    let body = "hello world";
    let mut body_encoded = Vec::new();

    GzEncoder::new(body.as_bytes(), Compression::default())
        .read_to_end(&mut body_encoded)
        .unwrap();

    let m = {
        let body_encoded = body_encoded.clone();
        mock! {
            headers {
                "Content-Encoding": "gzip",
            }
            body: body_encoded.clone(),
        }
    };

    let mut response = Request::get(m.url())
        .header("Accept-Encoding", "gzip")
        .automatic_decompression(false)
        .body(())
        .unwrap()
        .send()
        .unwrap();
    let mut body_received = Vec::new();
    response.body_mut().read_to_end(&mut body_received).unwrap();

    assert_eq!(body_received, body_encoded);
    m.request().expect_header("Accept-Encoding", "gzip");

    // Response body size should be known.
    assert_eq!(response.body().len(), Some(body_encoded.len() as u64));
}

#[test]
fn deflate_encoded_response_is_decoded_automatically() {
    let body = "hello world";
    let mut body_encoded = Vec::new();

    DeflateEncoder::new(body.as_bytes(), Compression::default())
        .read_to_end(&mut body_encoded)
        .unwrap();

    let m = mock! {
        headers {
            "Content-Encoding": "deflate",
        }
        body: body_encoded.clone(),
    };

    let mut response = isahc::get(m.url()).unwrap();

    //assert_eq!(response.text().unwrap(), body);
    m.request()
        .expect_header("Accept-Encoding", "deflate, gzip");

    // Response body size should be unknown, because the actual content is
    // compressed.
    assert_eq!(response.body().len(), None);
}

#[test]
fn content_is_decoded_even_if_not_listed_as_accepted() {
    let body = "hello world";
    let mut body_encoded = Vec::new();

    GzEncoder::new(body.as_bytes(), Compression::default())
        .read_to_end(&mut body_encoded)
        .unwrap();

    let m = mock! {
        headers {
            "Content-Encoding": "gzip",
        }
        body: body_encoded.clone(),
    };

    let mut response = Request::get(m.url())
        .header("Accept-Encoding", "deflate")
        .body(())
        .unwrap()
        .send()
        .unwrap();

    //assert_eq!(response.text().unwrap(), body);
    m.request().expect_header("Accept-Encoding", "deflate");
}

#[cfg(not(target_arch = "s390x"))]
#[test]
fn unknown_content_encoding_returns_error() {
    let m = mock! {
        headers {
            "Content-Encoding": "foo",
        }
        body: "hello world",
    };

    let result = Request::get(m.url())
        .header("Accept-Encoding", "deflate")
        .body(())
        .unwrap()
        .send();

    match result {
        Err(e) if e.kind() == isahc::error::ErrorKind::InvalidContentEncoding => {}
        _ => panic!("expected unknown encoding error, instead got {:?}", result),
    };

    m.request().expect_header("Accept-Encoding", "deflate");
}