File: compression.rs

package info (click to toggle)
rust-actix-web 4.11.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,748 kB
  • sloc: makefile: 2
file content (328 lines) | stat: -rw-r--r-- 9,906 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
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
use actix_http::ContentEncoding;
use actix_web::{
    http::{header, StatusCode},
    middleware::Compress,
    web, App, HttpResponse,
};
use bytes::Bytes;

mod utils;

static LOREM: &[u8] = include_bytes!("fixtures/lorem.txt");
static LOREM_GZIP: &[u8] = include_bytes!("fixtures/lorem.txt.gz");
static LOREM_BR: &[u8] = include_bytes!("fixtures/lorem.txt.br");
static LOREM_ZSTD: &[u8] = include_bytes!("fixtures/lorem.txt.zst");
static LOREM_XZ: &[u8] = include_bytes!("fixtures/lorem.txt.xz");

macro_rules! test_server {
    () => {
        actix_test::start(|| {
            App::new()
                .wrap(Compress::default())
                .route(
                    "/static",
                    web::to(|| async { HttpResponse::Ok().body(LOREM) }),
                )
                .route(
                    "/static-gzip",
                    web::to(|| async {
                        HttpResponse::Ok()
                            // signal to compressor that content should not be altered
                            // signal to client that content is encoded
                            .insert_header(ContentEncoding::Gzip)
                            .body(LOREM_GZIP)
                    }),
                )
                .route(
                    "/static-br",
                    web::to(|| async {
                        HttpResponse::Ok()
                            // signal to compressor that content should not be altered
                            // signal to client that content is encoded
                            .insert_header(ContentEncoding::Brotli)
                            .body(LOREM_BR)
                    }),
                )
                .route(
                    "/static-zstd",
                    web::to(|| async {
                        HttpResponse::Ok()
                            // signal to compressor that content should not be altered
                            // signal to client that content is encoded
                            .insert_header(ContentEncoding::Zstd)
                            .body(LOREM_ZSTD)
                    }),
                )
                .route(
                    "/static-xz",
                    web::to(|| async {
                        HttpResponse::Ok()
                            // signal to compressor that content should not be altered
                            // signal to client that content is encoded as 7zip
                            .insert_header((header::CONTENT_ENCODING, "xz"))
                            .body(LOREM_XZ)
                    }),
                )
                .route(
                    "/echo",
                    web::to(|body: Bytes| async move { HttpResponse::Ok().body(body) }),
                )
        })
    };
}

#[actix_rt::test]
async fn negotiate_encoding_identity() {
    let srv = test_server!();

    let req = srv
        .post("/static")
        .insert_header((header::ACCEPT_ENCODING, "identity"))
        .send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING), None);

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM));

    srv.stop().await;
}

#[actix_rt::test]
async fn negotiate_encoding_gzip() {
    let srv = test_server!();

    let req = srv
        .post("/static")
        .insert_header((header::ACCEPT_ENCODING, "gzip, br;q=0.8, zstd;q=0.5"))
        .send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "gzip");

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM));

    let mut res = srv
        .post("/static")
        .no_decompress()
        .insert_header((header::ACCEPT_ENCODING, "gzip, br;q=0.8, zstd;q=0.5"))
        .send()
        .await
        .unwrap();
    let bytes = res.body().await.unwrap();
    assert_eq!(utils::gzip::decode(bytes), LOREM);

    srv.stop().await;
}

#[actix_rt::test]
async fn negotiate_encoding_br() {
    let srv = test_server!();

    // check that brotli content-encoding header is returned

    let req = srv
        .post("/static")
        .insert_header((header::ACCEPT_ENCODING, "br, zstd, gzip"))
        .send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "br");

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM));

    // check that brotli is preferred even when later in (q-less) list

    let req = srv
        .post("/static")
        .insert_header((header::ACCEPT_ENCODING, "gzip, zstd, br"))
        .send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "br");

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM));

    // check that returned content is actually brotli encoded

    let mut res = srv
        .post("/static")
        .no_decompress()
        .insert_header((header::ACCEPT_ENCODING, "br, zstd, gzip"))
        .send()
        .await
        .unwrap();
    let bytes = res.body().await.unwrap();
    assert_eq!(utils::brotli::decode(bytes), LOREM);

    srv.stop().await;
}

#[actix_rt::test]
async fn negotiate_encoding_zstd() {
    let srv = test_server!();

    let req = srv
        .post("/static")
        .insert_header((header::ACCEPT_ENCODING, "zstd, gzip, br;q=0.8"))
        .send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "zstd");

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM));

    let mut res = srv
        .post("/static")
        .no_decompress()
        .insert_header((header::ACCEPT_ENCODING, "zstd, gzip, br;q=0.8"))
        .send()
        .await
        .unwrap();
    let bytes = res.body().await.unwrap();
    assert_eq!(utils::zstd::decode(bytes), LOREM);

    srv.stop().await;
}

#[cfg(all(
    feature = "compress-brotli",
    feature = "compress-gzip",
    feature = "compress-zstd",
))]
#[actix_rt::test]
async fn client_encoding_prefers_brotli() {
    let srv = test_server!();

    let req = srv.post("/static").send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "br");

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM));

    srv.stop().await;
}

#[actix_rt::test]
async fn gzip_no_decompress() {
    let srv = test_server!();

    let req = srv
        .post("/static-gzip")
        // don't decompress response body
        .no_decompress()
        // signal that we want a compressed body
        .insert_header((header::ACCEPT_ENCODING, "gzip, br;q=0.8, zstd;q=0.5"))
        .send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "gzip");

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM_GZIP));

    srv.stop().await;
}

#[actix_rt::test]
async fn manual_custom_coding() {
    let srv = test_server!();

    let req = srv
        .post("/static-xz")
        // don't decompress response body
        .no_decompress()
        // signal that we want a compressed body
        .insert_header((header::ACCEPT_ENCODING, "xz"))
        .send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "xz");

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM_XZ));

    srv.stop().await;
}

#[actix_rt::test]
async fn deny_identity_coding() {
    let srv = test_server!();

    let req = srv
        .post("/static")
        // signal that we want a compressed body
        .insert_header((header::ACCEPT_ENCODING, "br, identity;q=0"))
        .send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "br");

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM));

    srv.stop().await;
}

#[actix_rt::test]
async fn deny_identity_coding_no_decompress() {
    let srv = test_server!();

    let req = srv
        .post("/static-br")
        // don't decompress response body
        .no_decompress()
        // signal that we want a compressed body
        .insert_header((header::ACCEPT_ENCODING, "br, identity;q=0"))
        .send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "br");

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM_BR));

    srv.stop().await;
}

// TODO: fix test
// currently fails because negotiation doesn't consider unknown encoding types
#[ignore]
#[actix_rt::test]
async fn deny_identity_for_manual_coding() {
    let srv = test_server!();

    let req = srv
        .post("/static-xz")
        // don't decompress response body
        .no_decompress()
        // signal that we want a compressed body
        .insert_header((header::ACCEPT_ENCODING, "xz, identity;q=0"))
        .send();

    let mut res = req.await.unwrap();
    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.headers().get(header::CONTENT_ENCODING).unwrap(), "xz");

    let bytes = res.body().await.unwrap();
    assert_eq!(bytes, Bytes::from_static(LOREM_XZ));

    srv.stop().await;
}