@@ -20,19 +20,19 @@ use std::slice;
20
20
/// ```
21
21
/// # fn main() -> http_types::Result<()> {
22
22
/// #
23
- /// use http_types::content::{Accept, ContentEncoding, Encoding, Mime };
24
- /// use http_types::Response;
23
+ /// use http_types::content::{Accept, MediaTypeProposal };
24
+ /// use http_types::{mime, Response} ;
25
25
///
26
26
/// let mut accept = Accept::new();
27
- /// accept.push(Mime ::new(Encoding::Brotli , Some(0.8))?);
28
- /// accept.push(Mime ::new(Encoding::Gzip , Some(0.4))?);
29
- /// accept.push(Mime ::new(Encoding::Identity , None)?);
27
+ /// accept.push(MediaTypeProposal ::new(mime::HTML , Some(0.8))?);
28
+ /// accept.push(MediaTypeProposal ::new(mime::XML , Some(0.4))?);
29
+ /// accept.push(MediaTypeProposal ::new(mime::PLAIN , None)?);
30
30
///
31
31
/// let mut res = Response::new(200);
32
- /// let encoding = accept.negotiate(&[Encoding::Brotli, Encoding::Gzip ])?;
33
- /// encoding .apply(&mut res);
32
+ /// let media_type = accept.negotiate(&[mime::XML ])?;
33
+ /// media_type .apply(&mut res);
34
34
///
35
- /// assert_eq!(res["Content-Encoding"], "br ");
35
+ /// assert_eq!(res["Content-Encoding"], "text/html ");
36
36
/// #
37
37
/// # Ok(()) }
38
38
/// ```
@@ -111,7 +111,7 @@ impl Accept {
111
111
/// # Errors
112
112
///
113
113
/// If no suitable encoding is found, an error with the status of `406` will be returned.
114
- pub fn negotiate ( & mut self , available : & [ MediaTypeProposal ] ) -> crate :: Result < Mime > {
114
+ pub fn negotiate ( & mut self , available : & [ Mime ] ) -> crate :: Result < Mime > {
115
115
// Start by ordering the encodings.
116
116
self . sort ( ) ;
117
117
@@ -307,122 +307,119 @@ mod test {
307
307
Ok ( ( ) )
308
308
}
309
309
310
- // #[test]
311
- // fn wildcard() -> crate::Result<()> {
312
- // let mut accept = Accept::new();
313
- // accept.set_wildcard(true);
314
-
315
- // let mut headers = Response::new(200);
316
- // accept.apply(&mut headers);
317
-
318
- // let accept = Accept::from_headers(headers)?.unwrap();
319
- // assert!(accept.wildcard());
320
- // Ok(())
321
- // }
322
-
323
- // #[test]
324
- // fn wildcard_and_header() -> crate::Result<()> {
325
- // let mut accept = Accept::new();
326
- // accept.push(Encoding::Gzip);
327
- // accept.set_wildcard(true);
328
-
329
- // let mut headers = Response::new(200);
330
- // accept.apply(&mut headers);
331
-
332
- // let accept = Accept::from_headers(headers)?.unwrap();
333
- // assert!(accept.wildcard());
334
- // assert_eq!(accept.iter().next().unwrap(), Encoding::Gzip);
335
- // Ok(())
336
- // }
337
-
338
- // #[test]
339
- // fn iter() -> crate::Result<()> {
340
- // let mut accept = Accept::new();
341
- // accept.push(Encoding::Gzip);
342
- // accept.push(Encoding::Brotli);
343
-
344
- // let mut headers = Response::new(200);
345
- // accept.apply(&mut headers);
346
-
347
- // let accept = Accept::from_headers(headers)?.unwrap();
348
- // let mut accept = accept.iter();
349
- // assert_eq!(accept.next().unwrap(), Encoding::Gzip);
350
- // assert_eq!(accept.next().unwrap(), Encoding::Brotli);
351
- // Ok(())
352
- // }
353
-
354
- // #[test]
355
- // fn reorder_based_on_weight() -> crate::Result<()> {
356
- // let mut accept = Accept::new();
357
- // accept.push(Accept::new(Encoding::Gzip, Some(0.4))?);
358
- // accept.push(Accept::new(Encoding::Identity, None)?);
359
- // accept.push(Accept::new(Encoding::Brotli, Some(0.8))?);
360
-
361
- // let mut headers = Response::new(200);
362
- // accept.apply(&mut headers);
363
-
364
- // let mut accept = Accept::from_headers(headers)?.unwrap();
365
- // accept.sort();
366
- // let mut accept = accept.iter();
367
- // assert_eq!(accept.next().unwrap(), Encoding::Brotli);
368
- // assert_eq!(accept.next().unwrap(), Encoding::Gzip);
369
- // assert_eq!(accept.next().unwrap(), Encoding::Identity);
370
- // Ok(())
371
- // }
372
-
373
- // #[test]
374
- // fn reorder_based_on_weight_and_location() -> crate::Result<()> {
375
- // let mut accept = Accept::new();
376
- // accept.push(Accept::new(Encoding::Identity, None)?);
377
- // accept.push(Accept::new(Encoding::Gzip, None)?);
378
- // accept.push(Accept::new(Encoding::Brotli, Some(0.8))?);
379
-
380
- // let mut res = Response::new(200);
381
- // accept.apply(&mut res);
382
-
383
- // let mut accept = Accept::from_headers(res)?.unwrap();
384
- // accept.sort();
385
- // let mut accept = accept.iter();
386
- // assert_eq!(accept.next().unwrap(), Encoding::Brotli);
387
- // assert_eq!(accept.next().unwrap(), Encoding::Gzip);
388
- // assert_eq!(accept.next().unwrap(), Encoding::Identity);
389
- // Ok(())
390
- // }
391
-
392
- // #[test]
393
- // fn negotiate() -> crate::Result<()> {
394
- // let mut accept = Accept::new();
395
- // accept.push(Accept::new(Encoding::Brotli, Some(0.8))?);
396
- // accept.push(Accept::new(Encoding::Gzip, Some(0.4))?);
397
- // accept.push(Accept::new(Encoding::Identity, None)?);
398
-
399
- // assert_eq!(
400
- // accept.negotiate(&[Encoding::Brotli, Encoding::Gzip])?,
401
- // Encoding::Brotli,
402
- // );
403
- // Ok(())
404
- // }
405
-
406
- // #[test]
407
- // fn negotiate_not_acceptable() -> crate::Result<()> {
408
- // let mut accept = Accept::new();
409
- // let err = accept.negotiate(&[Mime::JSON]).unwrap_err();
410
- // assert_eq!(err.status(), 406);
411
-
412
- // let mut accept = Accept::new();
413
- // accept.push(MediaTypeProposal::new(Mime::JSON, Some(0.8))?);
414
- // let err = accept.negotiate(&[Encoding::Gzip]).unwrap_err();
415
- // assert_eq!(err.status(), 406);
416
- // Ok(())
417
- // }
418
-
419
- // #[test]
420
- // fn negotiate_wildcard() -> crate::Result<()> {
421
- // let mut accept = Accept::new();
422
- // accept.push(MediaTypeProposal::new(Mime::JSON, Some(0.8))?);
423
- // accept.set_wildcard(true);
424
-
425
- // assert_eq!(accept.negotiate(&[Encoding::Gzip])?, Encoding::Gzip);
426
- // Ok(())
427
- // }
310
+ #[ test]
311
+ fn wildcard ( ) -> crate :: Result < ( ) > {
312
+ let mut accept = Accept :: new ( ) ;
313
+ accept. set_wildcard ( true ) ;
314
+
315
+ let mut headers = Response :: new ( 200 ) ;
316
+ accept. apply ( & mut headers) ;
317
+
318
+ let accept = Accept :: from_headers ( headers) ?. unwrap ( ) ;
319
+ assert ! ( accept. wildcard( ) ) ;
320
+ Ok ( ( ) )
321
+ }
322
+
323
+ #[ test]
324
+ fn wildcard_and_header ( ) -> crate :: Result < ( ) > {
325
+ let mut accept = Accept :: new ( ) ;
326
+ accept. push ( mime:: HTML ) ;
327
+ accept. set_wildcard ( true ) ;
328
+
329
+ let mut headers = Response :: new ( 200 ) ;
330
+ accept. apply ( & mut headers) ;
331
+
332
+ let accept = Accept :: from_headers ( headers) ?. unwrap ( ) ;
333
+ assert ! ( accept. wildcard( ) ) ;
334
+ assert_eq ! ( accept. iter( ) . next( ) . unwrap( ) , mime:: HTML ) ;
335
+ Ok ( ( ) )
336
+ }
337
+
338
+ #[ test]
339
+ fn iter ( ) -> crate :: Result < ( ) > {
340
+ let mut accept = Accept :: new ( ) ;
341
+ accept. push ( mime:: HTML ) ;
342
+ accept. push ( mime:: XML ) ;
343
+
344
+ let mut headers = Response :: new ( 200 ) ;
345
+ accept. apply ( & mut headers) ;
346
+
347
+ let accept = Accept :: from_headers ( headers) ?. unwrap ( ) ;
348
+ let mut accept = accept. iter ( ) ;
349
+ assert_eq ! ( accept. next( ) . unwrap( ) , mime:: HTML ) ;
350
+ assert_eq ! ( accept. next( ) . unwrap( ) , mime:: XML ) ;
351
+ Ok ( ( ) )
352
+ }
353
+
354
+ #[ test]
355
+ fn reorder_based_on_weight ( ) -> crate :: Result < ( ) > {
356
+ let mut accept = Accept :: new ( ) ;
357
+ accept. push ( MediaTypeProposal :: new ( mime:: HTML , Some ( 0.4 ) ) ?) ;
358
+ accept. push ( MediaTypeProposal :: new ( mime:: XML , None ) ?) ;
359
+ accept. push ( MediaTypeProposal :: new ( mime:: PLAIN , Some ( 0.8 ) ) ?) ;
360
+
361
+ let mut headers = Response :: new ( 200 ) ;
362
+ accept. apply ( & mut headers) ;
363
+
364
+ let mut accept = Accept :: from_headers ( headers) ?. unwrap ( ) ;
365
+ accept. sort ( ) ;
366
+ let mut accept = accept. iter ( ) ;
367
+ assert_eq ! ( accept. next( ) . unwrap( ) , mime:: PLAIN ) ;
368
+ assert_eq ! ( accept. next( ) . unwrap( ) , mime:: HTML ) ;
369
+ assert_eq ! ( accept. next( ) . unwrap( ) , mime:: XML ) ;
370
+ Ok ( ( ) )
371
+ }
372
+
373
+ #[ test]
374
+ fn reorder_based_on_weight_and_location ( ) -> crate :: Result < ( ) > {
375
+ let mut accept = Accept :: new ( ) ;
376
+ accept. push ( MediaTypeProposal :: new ( mime:: HTML , None ) ?) ;
377
+ accept. push ( MediaTypeProposal :: new ( mime:: XML , None ) ?) ;
378
+ accept. push ( MediaTypeProposal :: new ( mime:: PLAIN , Some ( 0.8 ) ) ?) ;
379
+
380
+ let mut res = Response :: new ( 200 ) ;
381
+ accept. apply ( & mut res) ;
382
+
383
+ let mut accept = Accept :: from_headers ( res) ?. unwrap ( ) ;
384
+ accept. sort ( ) ;
385
+ let mut accept = accept. iter ( ) ;
386
+ assert_eq ! ( accept. next( ) . unwrap( ) , mime:: PLAIN ) ;
387
+ assert_eq ! ( accept. next( ) . unwrap( ) , mime:: XML ) ;
388
+ assert_eq ! ( accept. next( ) . unwrap( ) , mime:: HTML ) ;
389
+ Ok ( ( ) )
390
+ }
391
+
392
+ #[ test]
393
+ fn negotiate ( ) -> crate :: Result < ( ) > {
394
+ let mut accept = Accept :: new ( ) ;
395
+ accept. push ( MediaTypeProposal :: new ( mime:: HTML , Some ( 0.4 ) ) ?) ;
396
+ accept. push ( MediaTypeProposal :: new ( mime:: PLAIN , Some ( 0.8 ) ) ?) ;
397
+ accept. push ( MediaTypeProposal :: new ( mime:: XML , None ) ?) ;
398
+
399
+ assert_eq ! ( accept. negotiate( & [ mime:: HTML , mime:: XML ] ) ?, mime:: HTML ) ;
400
+ Ok ( ( ) )
401
+ }
402
+
403
+ #[ test]
404
+ fn negotiate_not_acceptable ( ) -> crate :: Result < ( ) > {
405
+ let mut accept = Accept :: new ( ) ;
406
+ let err = accept. negotiate ( & [ mime:: JSON ] ) . unwrap_err ( ) ;
407
+ assert_eq ! ( err. status( ) , 406 ) ;
408
+
409
+ let mut accept = Accept :: new ( ) ;
410
+ accept. push ( MediaTypeProposal :: new ( mime:: JSON , Some ( 0.8 ) ) ?) ;
411
+ let err = accept. negotiate ( & [ mime:: XML ] ) . unwrap_err ( ) ;
412
+ assert_eq ! ( err. status( ) , 406 ) ;
413
+ Ok ( ( ) )
414
+ }
415
+
416
+ #[ test]
417
+ fn negotiate_wildcard ( ) -> crate :: Result < ( ) > {
418
+ let mut accept = Accept :: new ( ) ;
419
+ accept. push ( MediaTypeProposal :: new ( mime:: JSON , Some ( 0.8 ) ) ?) ;
420
+ accept. set_wildcard ( true ) ;
421
+
422
+ assert_eq ! ( accept. negotiate( & [ mime:: XML ] ) ?, mime:: XML ) ;
423
+ Ok ( ( ) )
424
+ }
428
425
}
0 commit comments