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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
use std::{
net::SocketAddr,
pin::Pin,
task::{Context, Poll},
};
use bytes::Bytes;
#[cfg(feature = "charset")]
use encoding_rs::{Encoding, UTF_8};
#[cfg(feature = "stream")]
use futures_util::{Stream, TryStreamExt};
use http::{HeaderMap, StatusCode, Uri, Version};
use http_body::{Body as HttpBody, Frame};
use http_body_util::{BodyExt, Collected};
#[cfg(feature = "charset")]
use mime::Mime;
#[cfg(any(feature = "json", feature = "form"))]
use serde::de::DeserializeOwned;
use super::{
ClientResponseBody,
conn::HttpInfo,
core::{ext::ReasonPhrase, upgrade},
};
#[cfg(feature = "cookies")]
use crate::cookie;
use crate::{Body, Error, Upgraded, error::BoxError, ext::RequestUri};
#[cfg(feature = "simd-json")]
fn simd_json_input(bytes: Bytes) -> bytes::BytesMut {
match bytes.try_into_mut() {
Ok(bytes_mut) => bytes_mut,
Err(bytes) => bytes::BytesMut::from(bytes.as_ref()),
}
}
/// A Response to a submitted [`crate::Request`].
#[derive(Debug)]
pub struct Response {
uri: Uri,
res: http::Response<Body>,
}
impl Response {
pub(super) fn new<B>(res: http::Response<B>, uri: Uri) -> Response
where
B: HttpBody + Send + Sync + 'static,
B::Data: Into<Bytes>,
B::Error: Into<BoxError>,
{
Response {
uri,
res: res.map(Body::wrap),
}
}
/// Construct a [`Response`] from a standard `http::Response` and the final request URI.
///
/// This is the reverse bridge for the tower-native integration: when using
/// [`TowerServiceExt::into_tower_service()`](crate::tower_compat::TowerServiceExt), the
/// returned service yields `http::Response<ClientResponseBody>`. Use this method to
/// convert it back to `hpx::Response` for the convenience API.
pub fn from_http<B>(uri: Uri, res: http::Response<B>) -> Response
where
B: HttpBody + Send + Sync + 'static,
B::Data: Into<Bytes>,
B::Error: Into<BoxError>,
{
Response::new(res, uri)
}
pub(crate) fn from_client_response(uri: Uri, res: http::Response<ClientResponseBody>) -> Self {
Response {
uri,
res: res.map(Body::from),
}
}
/// Get the final [`Uri`] of this [`Response`].
#[inline]
pub fn uri(&self) -> &Uri {
&self.uri
}
/// Get the [`StatusCode`] of this [`Response`].
#[inline]
pub fn status(&self) -> StatusCode {
self.res.status()
}
/// Get the HTTP [`Version`] of this [`Response`].
#[inline]
pub fn version(&self) -> Version {
self.res.version()
}
/// Get the [`HeaderMap`] of this [`Response`].
#[inline]
pub fn headers(&self) -> &HeaderMap {
self.res.headers()
}
/// Get a mutable reference to the [`HeaderMap`] of this [`Response`].
#[inline]
pub fn headers_mut(&mut self) -> &mut HeaderMap {
self.res.headers_mut()
}
/// Get the content length of the [`Response`], if it is known.
///
/// This value does not directly represents the value of the `Content-Length`
/// header, but rather the size of the response's body. To read the header's
/// value, please use the [`Response::headers`] method instead.
///
/// Reasons it may not be known:
///
/// - The response does not include a body (e.g. it responds to a `HEAD` request).
/// - The response is gzipped and automatically decoded (thus changing the actual decoded
/// length).
#[inline]
pub fn content_length(&self) -> Option<u64> {
HttpBody::size_hint(self.res.body()).exact()
}
/// Retrieve the cookies contained in the [`Response`].
///
/// Note that invalid 'Set-Cookie' headers will be ignored.
///
/// # Optional
///
/// This requires the optional `cookies` feature to be enabled.
#[cfg(feature = "cookies")]
pub fn cookies(&self) -> impl Iterator<Item = cookie::Cookie<'_>> {
self.res
.headers()
.get_all(crate::header::SET_COOKIE)
.iter()
.map(cookie::Cookie::parse)
.filter_map(Result::ok)
}
/// Get the local address used to get this [`Response`].
pub fn local_addr(&self) -> Option<SocketAddr> {
self.res
.extensions()
.get::<HttpInfo>()
.map(HttpInfo::local_addr)
}
/// Get the remote address used to get this [`Response`].
pub fn remote_addr(&self) -> Option<SocketAddr> {
self.res
.extensions()
.get::<HttpInfo>()
.map(HttpInfo::remote_addr)
}
// body methods
/// Get the full response text.
///
/// This method decodes the response body with BOM sniffing
/// and with malformed sequences replaced with the [`char::REPLACEMENT_CHARACTER`].
/// Encoding is determined from the `charset` parameter of `Content-Type` header,
/// and defaults to `utf-8` if not presented.
///
/// Note that the BOM is stripped from the returned String.
///
/// # Note
///
/// If the `charset` feature is disabled the method will only attempt to decode the
/// response as UTF-8, regardless of the given `Content-Type`
///
/// # Example
///
/// ```
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let content = hpx::Client::new()
/// .get("http://httpbin.org/range/26")
/// .send()
/// .await?
/// .text()
/// .await?;
///
/// println!("text: {content:?}");
/// # Ok(())
/// # }
/// ```
pub async fn text(self) -> crate::Result<String> {
#[cfg(feature = "charset")]
{
self.text_with_charset("utf-8").await
}
#[cfg(not(feature = "charset"))]
{
let full = self.bytes().await?;
let text = String::from_utf8_lossy(&full);
Ok(text.into_owned())
}
}
/// Get the full response text given a specific encoding.
///
/// This method decodes the response body with BOM sniffing
/// and with malformed sequences replaced with the
/// [`char::REPLACEMENT_CHARACTER`].
/// You can provide a default encoding for decoding the raw message, while the
/// `charset` parameter of `Content-Type` header is still prioritized. For more information
/// about the possible encoding name, please go to [`encoding_rs`] docs.
///
/// Note that the BOM is stripped from the returned String.
///
/// [`encoding_rs`]: https://docs.rs/encoding_rs/0.8/encoding_rs/#relationship-with-windows-code-pages
///
/// # Optional
///
/// This requires the optional `encoding_rs` feature enabled.
///
/// # Example
///
/// ```
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let content = hpx::Client::new()
/// .get("http://httpbin.org/range/26")
/// .send()
/// .await?
/// .text_with_charset("utf-8")
/// .await?;
///
/// println!("text: {content:?}");
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "charset")]
#[cfg_attr(docsrs, doc(cfg(feature = "charset")))]
pub async fn text_with_charset(
self,
default_encoding: impl AsRef<str>,
) -> crate::Result<String> {
let content_type = self
.headers()
.get(crate::header::CONTENT_TYPE)
.and_then(|value| value.to_str().ok())
.and_then(|value| value.parse::<Mime>().ok());
let encoding_name = content_type
.as_ref()
.and_then(|mime| mime.get_param("charset").map(|charset| charset.as_str()))
.unwrap_or(default_encoding.as_ref());
let encoding = Encoding::for_label(encoding_name.as_bytes()).unwrap_or(UTF_8);
let full = self.bytes().await?;
let (text, _, _) = encoding.decode(&full);
Ok(text.into_owned())
}
/// Try to deserialize the response body as `application/x-www-form-urlencoded`.
///
/// # Optional
///
/// This requires the optional `form` feature enabled.
///
/// # Examples
///
/// ```
/// # extern crate hpx;
/// # extern crate serde;
/// #
/// # use hpx::Error;
/// # use serde::Deserialize;
/// #
/// #[derive(Deserialize)]
/// struct TokenResponse {
/// access_token: String,
/// expires_in: u64,
/// }
///
/// # async fn run() -> Result<(), Error> {
/// let token = hpx::Client::new()
/// .get("https://example.com/oauth/token")
/// .send()
/// .await?
/// .form::<TokenResponse>()
/// .await?;
///
/// println!(
/// "token: {} expires in {}",
/// token.access_token, token.expires_in
/// );
/// # Ok(())
/// # }
/// #
/// # fn main() { }
/// ```
#[cfg(feature = "form")]
#[cfg_attr(docsrs, doc(cfg(feature = "form")))]
pub async fn form<T: DeserializeOwned>(self) -> crate::Result<T> {
let full = self.bytes().await?;
serde_urlencoded::from_bytes(&full).map_err(Error::decode)
}
/// Try to deserialize the response body as JSON.
///
/// # Optional
///
/// This requires the optional `json` feature enabled.
///
/// # Examples
///
/// ```
/// # extern crate hpx;
/// # extern crate serde;
/// #
/// # use hpx::Error;
/// # use serde::Deserialize;
/// #
/// // This `derive` requires the `serde` dependency.
/// #[derive(Deserialize)]
/// struct Ip {
/// origin: String,
/// }
///
/// # async fn run() -> Result<(), Error> {
/// let ip = hpx::Client::new()
/// .get("http://httpbin.org/ip")
/// .send()
/// .await?
/// .json::<Ip>()
/// .await?;
///
/// println!("ip: {}", ip.origin);
/// # Ok(())
/// # }
/// #
/// # fn main() { }
/// ```
///
/// # Errors
///
/// This method fails whenever the response body is not in JSON format
/// or it cannot be properly deserialized to target type `T`. For more
/// details please see [`serde_json::from_reader`].
///
/// [`serde_json::from_reader`]: https://docs.serde.rs/serde_json/fn.from_reader.html
#[cfg(all(feature = "json", not(feature = "simd-json")))]
#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
pub async fn json<T: DeserializeOwned>(self) -> crate::Result<T> {
let full = self.bytes().await?;
serde_json::from_slice(&full).map_err(Error::decode)
}
/// Try to deserialize the response body as JSON using SIMD-accelerated parsing.
///
/// This method uses `simd-json` for faster JSON parsing on supported platforms.
/// It requires a mutable buffer, so the response bytes are copied to a Vec.
///
/// # Optional
///
/// This requires the optional `simd-json` feature enabled.
///
/// # Examples
///
/// ```
/// # extern crate hpx;
/// # extern crate serde;
/// #
/// # use hpx::Error;
/// # use serde::Deserialize;
/// #
/// #[derive(Deserialize)]
/// struct Ip {
/// origin: String,
/// }
///
/// # async fn run() -> Result<(), Error> {
/// let ip = hpx::Client::new()
/// .get("http://httpbin.org/ip")
/// .send()
/// .await?
/// .json::<Ip>()
/// .await?;
///
/// println!("ip: {}", ip.origin);
/// # Ok(())
/// # }
/// ```
#[cfg(feature = "simd-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "simd-json")))]
pub async fn json<T: DeserializeOwned>(self) -> crate::Result<T> {
let full = self.bytes().await?;
let mut bytes_mut = simd_json_input(full);
simd_json::from_slice(&mut bytes_mut).map_err(Error::decode)
}
/// Get the full response body as [`Bytes`].
///
/// # Example
///
/// ```
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let bytes = hpx::Client::new()
/// .get("http://httpbin.org/ip")
/// .send()
/// .await?
/// .bytes()
/// .await?;
///
/// println!("bytes: {bytes:?}");
/// # Ok(())
/// # }
/// ```
pub async fn bytes(self) -> crate::Result<Bytes> {
BodyExt::collect(self.res.into_body())
.await
.map(Collected::<Bytes>::to_bytes)
}
/// Stream a chunk of the response body.
///
/// When the response body has been exhausted, this will return `None`.
///
/// # Example
///
/// ```
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut res = hpx::get("https://hyper.rs").send().await?;
///
/// while let Some(chunk) = res.chunk().await? {
/// println!("Chunk: {chunk:?}");
/// }
/// # Ok(())
/// # }
/// ```
pub async fn chunk(&mut self) -> crate::Result<Option<Bytes>> {
loop {
if let Some(res) = self.res.body_mut().frame().await {
if let Ok(buf) = res?.into_data() {
return Ok(Some(buf));
}
} else {
return Ok(None);
}
}
}
/// Convert the response into a [`Stream`] of [`Bytes`] from the body.
///
/// # Example
///
/// ```
/// use futures_util::StreamExt;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let mut stream = hpx::Client::new()
/// .get("http://httpbin.org/ip")
/// .send()
/// .await?
/// .bytes_stream();
///
/// while let Some(item) = stream.next().await {
/// println!("Chunk: {:?}", item?);
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Optional
///
/// This requires the optional `stream` feature to be enabled.
#[cfg(feature = "stream")]
#[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
pub fn bytes_stream(self) -> impl Stream<Item = crate::Result<Bytes>> {
http_body_util::BodyDataStream::new(self.res.into_body())
}
/// Consume the response and return the raw [`Body`].
///
/// This is useful for zero-copy streaming scenarios where you want
/// direct access to the body without additional processing.
///
/// # Example
///
/// ```
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let response = hpx::get("https://httpbin.org/bytes/1024").send().await?;
/// let body = response.into_body();
/// // Use body directly for zero-copy streaming
/// # Ok(())
/// # }
/// ```
pub fn into_body(self) -> Body {
self.res.into_body()
}
/// Get an [`tokio::io::AsyncRead`] adapter for the response body.
///
/// This enables efficient streaming of the response body using
/// standard async I/O traits.
///
/// # Example
///
/// ```
/// use tokio::io::AsyncReadExt;
///
/// # async fn run() -> Result<(), Box<dyn std::error::Error>> {
/// let response = hpx::get("https://httpbin.org/bytes/1024").send().await?;
/// let mut reader = response.reader();
///
/// let mut buffer = Vec::new();
/// reader.read_to_end(&mut buffer).await?;
/// println!("Read {} bytes", buffer.len());
/// # Ok(())
/// # }
/// ```
///
/// # Optional
///
/// This requires the optional `stream` feature to be enabled.
#[cfg(feature = "stream")]
#[cfg_attr(docsrs, doc(cfg(feature = "stream")))]
pub fn reader(self) -> impl tokio::io::AsyncRead {
tokio_util::io::StreamReader::new(
http_body_util::BodyDataStream::new(self.res.into_body())
.map_err(std::io::Error::other),
)
}
// extension methods
/// Returns a reference to the associated extensions.
///
/// # Example
///
/// ```
/// # use hpx::{Client, tls::TlsInfo};
/// # async fn run() -> hpx::Result<()> {
/// // Build a client that records TLS information.
/// let client = Client::builder().tls_info(true).build()?;
///
/// // Make a request.
/// let resp = client.get("https://www.google.com").send().await?;
///
/// // Take the TlsInfo extension to inspect it.
/// if let Some(tls_info) = resp.extensions().get::<TlsInfo>() {
/// // Now you own the TlsInfo and can process it.
/// println!("Peer certificate: {:?}", tls_info.peer_certificate());
/// }
///
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn extensions(&self) -> &http::Extensions {
self.res.extensions()
}
/// Returns a mutable reference to the associated extensions.
///
/// # Example
///
/// ```
/// # use hpx::{Client, tls::TlsInfo};
/// # async fn run() -> hpx::Result<()> {
/// // Build a client that records TLS information.
/// let client = Client::builder().tls_info(true).build()?;
///
/// // Make a request.
/// let mut resp = client.get("https://www.google.com").send().await?;
///
/// // Take the TlsInfo extension to inspect it.
/// if let Some(tls_info) = resp.extensions_mut().remove::<TlsInfo>() {
/// // Now you own the TlsInfo and can process it.
/// println!("Peer certificate: {:?}", tls_info.peer_certificate());
/// }
///
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn extensions_mut(&mut self) -> &mut http::Extensions {
self.res.extensions_mut()
}
// util methods
/// Turn a response into an error if the server returned an error.
///
/// # Example
///
/// ```
/// # use hpx::Response;
/// fn on_response(res: Response) {
/// match res.error_for_status() {
/// Ok(_res) => (),
/// Err(err) => {
/// // asserting a 400 as an example
/// // it could be any status between 400...599
/// assert_eq!(err.status(), Some(hpx::StatusCode::BAD_REQUEST));
/// }
/// }
/// }
/// # fn main() {}
/// ```
pub fn error_for_status(mut self) -> crate::Result<Self> {
let status = self.status();
if status.is_client_error() || status.is_server_error() {
let reason = self.res.extensions_mut().remove::<ReasonPhrase>();
Err(Error::status_code(self.uri, status, reason))
} else {
Ok(self)
}
}
/// Turn a reference to a response into an error if the server returned an error.
///
/// # Example
///
/// ```
/// # use hpx::Response;
/// fn on_response(res: &Response) {
/// match res.error_for_status_ref() {
/// Ok(_res) => (),
/// Err(err) => {
/// // asserting a 400 as an example
/// // it could be any status between 400...599
/// assert_eq!(err.status(), Some(hpx::StatusCode::BAD_REQUEST));
/// }
/// }
/// }
/// # fn main() {}
/// ```
pub fn error_for_status_ref(&self) -> crate::Result<&Self> {
let status = self.status();
if status.is_client_error() || status.is_server_error() {
let reason = self.res.extensions().get::<ReasonPhrase>().cloned();
Err(Error::status_code(self.uri.clone(), status, reason))
} else {
Ok(self)
}
}
/// Consumes the [`Response`] and returns a future for a possible HTTP upgrade.
pub async fn upgrade(self) -> crate::Result<Upgraded> {
upgrade::on(self.res).await.map_err(Error::upgrade)
}
}
/// I'm not sure this conversion is that useful... People should be encouraged
/// to use [`http::Response`], not `hpx::Response`.
impl<T: Into<Body>> From<http::Response<T>> for Response {
fn from(r: http::Response<T>) -> Response {
let mut res = r.map(Into::into);
let uri = res
.extensions_mut()
.remove::<RequestUri>()
.unwrap_or_else(|| RequestUri(Uri::from_static("http://no.url.provided.local")));
Response { res, uri: uri.0 }
}
}
/// A [`Response`] can be converted into a [`http::Response`].
// It's supposed to be the inverse of the conversion above.
impl From<Response> for http::Response<Body> {
fn from(r: Response) -> http::Response<Body> {
let mut res = r.res;
res.extensions_mut().insert(RequestUri(r.uri));
res
}
}
/// A [`Response`] can be piped as the [`Body`] of another request.
impl From<Response> for Body {
fn from(r: Response) -> Body {
r.res.into_body()
}
}
/// A [`Response`] implements [`HttpBody`] to allow streaming the body.
impl HttpBody for Response {
type Data = Bytes;
type Error = Error;
#[inline]
fn poll_frame(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
Pin::new(self.res.body_mut()).poll_frame(cx)
}
#[inline]
fn is_end_stream(&self) -> bool {
self.res.body().is_end_stream()
}
#[inline]
fn size_hint(&self) -> http_body::SizeHint {
self.res.body().size_hint()
}
}
#[cfg(test)]
mod tests {
use bytes::Bytes;
use http::Uri;
#[cfg(feature = "form")]
use serde::Deserialize;
use super::Response;
#[cfg(feature = "simd-json")]
use super::simd_json_input;
use crate::{Body, ClientResponseBody, ext::RequestUri};
fn response_with_body<T: Into<Body>>(body: T) -> Response {
let mut res = http::Response::new(body);
res.extensions_mut()
.insert(RequestUri(Uri::from_static("https://example.com/resource")));
res.into()
}
#[cfg(feature = "form")]
#[tokio::test]
async fn response_form_decodes_urlencoded_body() {
#[derive(Debug, Deserialize, PartialEq, Eq)]
struct TokenResponse {
access_token: String,
expires_in: u64,
}
let response = response_with_body("access_token=abc123&expires_in=60");
let token = response.form::<TokenResponse>().await.unwrap();
assert_eq!(
token,
TokenResponse {
access_token: "abc123".to_owned(),
expires_in: 60,
}
);
}
#[test]
fn response_into_body_preserves_reusable_bytes() {
let response = response_with_body(Bytes::from_static(b"hello"));
let body: Body = response.into();
assert_eq!(body.as_bytes(), Some(&b"hello"[..]));
}
#[test]
fn response_into_http_preserves_reusable_bytes_and_uri() {
let response = response_with_body(Bytes::from_static(b"hello"));
let http_response: http::Response<Body> = response.into();
assert_eq!(http_response.body().as_bytes(), Some(&b"hello"[..]));
assert_eq!(
http_response
.extensions()
.get::<RequestUri>()
.map(|uri| &uri.0),
Some(&Uri::from_static("https://example.com/resource"))
);
}
#[test]
fn response_from_client_response_preserves_direct_body_access() {
let response = Response::from_client_response(
Uri::from_static("https://example.com/resource"),
http::Response::new(ClientResponseBody::from(Bytes::from_static(b"hello"))),
);
let body: Body = response.into();
assert_eq!(body.as_bytes(), Some(&b"hello"[..]));
}
#[cfg(feature = "simd-json")]
#[test]
fn simd_json_input_reuses_unique_bytes_storage() {
let bytes = Bytes::from(Vec::from(&b"{\"ok\":true}"[..]));
let ptr = bytes.as_ptr();
let buffer = simd_json_input(bytes);
assert_eq!(buffer.as_ptr(), ptr);
}
#[cfg(feature = "simd-json")]
#[test]
fn simd_json_input_copies_when_bytes_are_shared() {
let bytes = Bytes::from(Vec::from(&b"{\"ok\":true}"[..]));
let shared = bytes.clone();
let ptr = shared.as_ptr();
let buffer = simd_json_input(shared);
assert_eq!(buffer.as_ref(), bytes.as_ref());
assert_ne!(buffer.as_ptr(), ptr);
}
}