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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
use std::borrow::Cow;
use bytesbuf::BytesView;
use futures::Stream;
use http::header::CONTENT_TYPE;
use http::{HeaderName, HeaderValue, Version};
use crate::http_utils::{CONTENT_TYPE_TEXT, try_content_length_header, try_header};
use crate::{HttpBody, HttpBodyBuilder, HttpBodyOptions, HttpError, HttpResponse, Result};
/// A fluent builder for creating HTTP responses.
///
/// `HttpResponseBuilder` simplifies the process of building HTTP responses by providing a chainable API.
/// It handles setting headers and different body types, and offers convenient methods for common
/// response handling patterns.
///
/// > **Note**: While useful in application code, `HttpResponseBuilder` is primarily designed for testing HTTP handlers
/// > and middleware. The `HttpResponseBuilder::new_fake` method makes it particularly easy to create test responses
/// > without needing a real HTTP context.
///
/// # Examples
///
/// ```
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponse, HttpResponseBuilder};
/// # let builder = HttpBodyBuilder::new_fake();
/// // Create a response builder
/// let response_builder = HttpResponseBuilder::new(&builder);
///
/// // Customize the response builder
/// let response_builder = response_builder
/// .text("Hello world")
/// .status(200)
/// .header("X-Custom-Header", "value");
///
/// // Build the response
/// let response: HttpResponse = response_builder.build()?;
///
/// # Ok::<(), HttpError>(())
/// ```
#[derive(Debug)]
#[must_use]
pub struct HttpResponseBuilder<'a> {
body_builder: Cow<'a, HttpBodyBuilder>,
builder: http::response::Builder,
body: Option<Result<HttpBody>>,
content_type: Option<HeaderValue>,
}
impl HttpResponseBuilder<'static> {
/// Creates a new response builder instance for testing.
///
/// This method provides a convenient way to create a `HttpResponseBuilder` for tests
/// without needing an existing body builder. The response builder is ready to be
/// configured with headers, status, and body.
///
/// The `test-util` feature must be enabled to use this method.
///
/// # Examples
///
/// ```
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// let response = HttpResponseBuilder::new_fake()
/// .status(200)
/// .text("Test response")
/// .build()?;
/// # Ok::<(), HttpError>(())
/// ```
#[cfg(any(feature = "test-util", test))]
pub fn new_fake() -> Self {
Self {
body_builder: Cow::Owned(HttpBodyBuilder::new_fake()),
builder: http::response::Builder::new(),
body: None,
content_type: None,
}
}
}
impl<'a> HttpResponseBuilder<'a> {
/// Creates a new response builder instance with the given body builder.
pub fn new(builder: &'a HttpBodyBuilder) -> Self {
Self {
body_builder: Cow::Borrowed(builder),
builder: http::response::Builder::new(),
body: None,
content_type: None,
}
}
}
impl HttpResponseBuilder<'_> {
/// Sets a plain text body for the response.
///
/// Automatically sets the `Content-Type` header to `text/plain`.
/// If the `Content-Type` header is already set, it will not override it.
///
/// This method always encodes the provided string as UTF-8.
///
/// # Examples
///
/// ```
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// # let builder = HttpBodyBuilder::new_fake();
/// let response_builder = HttpResponseBuilder::new(&builder)
/// .status(200)
/// .text("Hello world");
///
/// # Ok::<(), HttpError>(())
/// ```
pub fn text(mut self, data: impl AsRef<str>) -> Self {
let body = self.body_builder.text(data);
self.content_type = Some(CONTENT_TYPE_TEXT);
self.body(body)
}
/// Sets a byte sequence as the response body.
///
/// Use this when you need to send raw binary data.
/// Unlike [`text`](Self::text), this doesn't set a `Content-Type` header.
///
/// # Examples
///
/// ```
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// # use bytesbuf::BytesView;
/// # let builder = HttpBodyBuilder::new_fake();
/// // Create a BytesView over some bytes
/// let payload = BytesView::copied_from_slice(b"hello world", &builder);
///
/// let response_builder = HttpResponseBuilder::new(&builder)
/// .status(200)
/// .bytes(payload);
///
/// # Ok::<(), HttpError>(())
/// ```
pub fn bytes(self, b: impl Into<BytesView>) -> Self {
let body = self.body_builder.bytes(b);
self.body(body)
}
/// Sets a JSON-serialized body for the response.
///
/// Takes any type that implements `serde::Serialize` and converts it to JSON with the following rules:
///
/// - The `Content-Type` header is set to `application/json` if not already set.
/// - The data is always encoded as UTF-8.
///
/// This method requires the `json` feature to be enabled.
///
/// # Examples
///
/// ```
/// # use serde_json::json;
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// # let builder = HttpBodyBuilder::new_fake();
/// let json_value = json!({ "id": 42, "name": "Alice" });
/// let response_builder = HttpResponseBuilder::new(&builder)
/// .status(200)
/// .json(&json_value);
///
/// # Ok::<(), HttpError>(())
/// ```
///
/// # Errors
///
/// Returns an error if JSON serialization fails.
#[cfg(any(feature = "json", test))]
pub fn json<T: serde_core::ser::Serialize>(mut self, data: &T) -> Self {
let body = self.body_builder.json(data).map_err(HttpError::from);
self.content_type = Some(crate::http_utils::CONTENT_TYPE_JSON);
self.body_result(body)
}
/// Sets the HTTP status code for the response.
///
/// # Examples
///
/// ```
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// # use http::StatusCode;
/// # let builder = HttpBodyBuilder::new_fake();
/// let response_builder = HttpResponseBuilder::new(&builder)
/// .status(StatusCode::OK)
/// .status(200); // You can also use integers
///
/// # Ok::<(), HttpError>(())
/// ```
pub fn status(mut self, status: impl TryInto<http::StatusCode, Error: Into<http::Error>>) -> Self {
self.builder = self.builder.status(status);
self
}
/// Sets the HTTP protocol version for the response.
///
/// # Examples
///
/// ```
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// # use http::Version;
/// # let builder = HttpBodyBuilder::new_fake();
/// let response_builder = HttpResponseBuilder::new(&builder).version(Version::HTTP_2);
///
/// # Ok::<(), HttpError>(())
/// ```
pub fn version(mut self, version: Version) -> Self {
self.builder = self.builder.version(version);
self
}
/// Adds an extension to the response.
///
/// Extensions are type-mapped data that can be attached to responses for use by
/// middleware, handlers, or other parts of your application.
///
/// # Examples
///
/// ```
/// # use http_extensions::HttpResponseBuilder;
/// #[derive(Clone)]
/// struct RequestId(String);
///
/// let response = HttpResponseBuilder::new_fake()
/// .status(200)
/// .extension(RequestId("req-123".to_string()))
/// .build()
/// .unwrap();
/// ```
pub fn extension<T>(mut self, extension: T) -> Self
where
T: Clone + Send + Sync + 'static,
{
self.builder = self.builder.extension(extension);
self
}
/// Adds a header to the response.
///
/// This method accepts any type that can be converted to a [`HeaderName`] and [`HeaderValue`].
/// It returns `self` to enable method chaining.
///
/// # Performance
///
/// It's better to use pre-created `HeaderName` and `HeaderValue` instances to avoid
/// parsing overhead. This applies for values that are fixed and used multiple times.
///
/// # Examples
///
/// ```
/// # use http::header::CACHE_CONTROL;
/// # use http::HeaderValue;
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// # let builder = HttpBodyBuilder::new_fake();
/// // Pre-create a HeaderValue to avoid parsing overhead
/// let header_value = HeaderValue::from_static("no-cache");
///
/// let response_builder = HttpResponseBuilder::new(&builder)
/// .header(CACHE_CONTROL, header_value.clone()) // Using a pre-created HeaderValue
/// .header("X-Custom-Header", "value");
///
/// # Ok::<(), HttpError>(())
/// ```
pub fn header(
mut self,
key: impl TryInto<HeaderName, Error: Into<http::Error>>,
value: impl TryInto<HeaderValue, Error: Into<http::Error>>,
) -> Self {
self.builder = self.builder.header(key, value);
self
}
/// Provides mutable access to the response headers.
///
/// Use this when you need to manipulate headers directly.
/// For simple header addition, prefer using the [`header`](Self::header) method.
///
/// When the builder has errors, this method will return `None`.
///
/// # Examples
///
/// ```
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// # let builder = HttpBodyBuilder::new_fake();
/// let mut response_builder = HttpResponseBuilder::new(&builder);
///
/// if let Some(headers) = response_builder.headers_mut() {
/// headers.insert("X-Custom-Header", "value".parse().unwrap());
/// }
///
/// # Ok::<(), HttpError>(())
/// ```
pub fn headers_mut(&mut self) -> Option<&mut http::HeaderMap<HeaderValue>> {
self.builder.headers_mut()
}
/// Sets the response body directly.
///
/// Use this when you already have an `HttpBody` instance.
/// For most cases, prefer the more specific methods like
/// [`text`](Self::text) or [`bytes`](Self::bytes).
///
/// # Examples
///
/// ```
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// # use http_extensions::HttpBody;
/// # let builder = HttpBodyBuilder::new_fake();
/// # let custom_body = builder.text("custom");
/// let response_builder = HttpResponseBuilder::new(&builder).body(custom_body);
///
/// # Ok::<(), HttpError>(())
/// ```
pub fn body(self, body: HttpBody) -> Self {
self.body_result(Ok(body))
}
/// Sets the response body from a result that might contain an error.
///
/// This is used internally by methods that might fail when creating the body.
fn body_result(mut self, body: Result<HttpBody>) -> Self {
self.body = Some(body);
self
}
/// Creates a response with the configured settings.
///
/// This method consumes the `HttpResponseBuilder` instance. It automatically sets
/// appropriate headers based on the body, such as `Content-Length` and `Content-Type`,
/// if they haven't been set already.
///
/// # Examples
///
/// ```
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponse, HttpResponseBuilder};
/// # let builder = HttpBodyBuilder::new_fake();
/// let response: HttpResponse = HttpResponseBuilder::new(&builder)
/// .status(200)
/// .text("Success")
/// .build()?;
/// # Ok::<(), HttpError>(())
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The response couldn't be built because of errors
/// - Body processing failed
pub fn build(mut self) -> Result<HttpResponse> {
let body = self.body.take().unwrap_or_else(|| Ok(self.body_builder.empty()))?;
if let Some(length) = body.content_length() {
try_content_length_header(&mut self.builder, length);
}
if let Some(content_type) = self.content_type.take() {
try_header(&mut self.builder, CONTENT_TYPE, content_type);
}
let body = self.builder.body(body)?;
Ok(body)
}
/// Sets a custom body implementation as the response body.
///
/// This is useful when you have a custom body implementation that implements
/// the `http_body::Body` trait and want to use it with the `HttpResponseBuilder`.
///
/// # Examples
///
/// ```
/// # use std::pin::Pin;
/// # use std::task::{Context, Poll};
/// # use http_body::Body;
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// # use bytesbuf::BytesView;
/// // Your custom body type
/// #[derive(Default)]
/// struct CustomBody;
///
/// // Implement the `Body` trait for your custom body type.
/// impl Body for CustomBody {
/// type Data = BytesView;
/// type Error = HttpError;
///
/// fn poll_frame(
/// self: Pin<&mut Self>,
/// _cx: &mut Context<'_>,
/// ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
/// // Implementation details...
/// Poll::Ready(None)
/// }
/// }
///
/// # let builder = HttpBodyBuilder::new_fake();
/// let response_builder = HttpResponseBuilder::new(&builder)
/// .status(200)
/// .custom_body(CustomBody::default());
/// ```
pub fn custom_body<B>(self, body: B) -> Self
where
B: http_body::Body<Data = BytesView, Error: Into<HttpError>> + Send + 'static,
{
let body = self.body_builder.body(body, &HttpBodyOptions::default());
self.body(body)
}
/// Sets a streaming body for the response.
///
/// This is a convenience wrapper around [`custom_body`](Self::custom_body) that accepts
/// a [`Stream`] of [`BytesView`] chunks. It avoids the need to manually wrap
/// the stream in a [`StreamBody`][http_body_util::StreamBody].
///
/// Note that streaming bodies do not have a known content length, so the
/// `Content-Length` header will not be set automatically.
///
/// # Examples
///
/// ```
/// # use http_extensions::{HttpBodyBuilder, HttpError, HttpResponseBuilder};
/// # use bytesbuf::BytesView;
/// # let body_builder = HttpBodyBuilder::new_fake();
/// let chunks = vec![
/// Ok(BytesView::copied_from_slice(b"hello ", &body_builder)),
/// Ok(BytesView::copied_from_slice(b"world", &body_builder)),
/// ];
/// let response = HttpResponseBuilder::new(&body_builder)
/// .status(200)
/// .stream(futures::stream::iter(chunks))
/// .build()?;
/// # Ok::<(), HttpError>(())
/// ```
pub fn stream<S>(self, stream: S) -> Self
where
S: Stream<Item = Result<BytesView>> + Send + 'static,
{
let body = self.body_builder.stream(stream, &HttpBodyOptions::default());
self.body(body)
}
}
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
use futures::executor::block_on;
use http::StatusCode;
use http::header::CONTENT_LENGTH;
use serde::Serialize;
use super::*;
use crate::HeaderMapExt;
use crate::testing::{SingleChunkBody, create_stream_body_from_chunks};
#[test]
fn new_with_borrowed_builder() {
let builder = HttpBodyBuilder::new_fake();
let response_builder = HttpResponseBuilder::new(&builder);
let response = response_builder.text("test").build().unwrap();
assert_eq!(block_on(response.into_body().into_text()).unwrap(), "test");
}
#[test]
fn json_body_ok() {
let response = HttpResponseBuilder::new_fake().json(&JsonData { id: 42 }).build().unwrap();
assert_eq!(response.headers().get_value_or(CONTENT_LENGTH, 0), 9);
assert_eq!(response.headers().get_str_value_or(CONTENT_TYPE, ""), "application/json");
assert_eq!(block_on(response.into_body().into_text()).unwrap(), "{\"id\":42}");
}
#[test]
fn json_does_not_override_existing_content_type() {
let response = HttpResponseBuilder::new_fake()
.header(CONTENT_TYPE, "application/custom")
.json(&JsonData { id: 42 })
.build()
.unwrap();
assert_eq!(response.headers().get_str_value_or(CONTENT_TYPE, ""), "application/custom");
}
#[test]
fn text_body_ok() {
let response = HttpResponseBuilder::new_fake().text("hello").build().unwrap();
assert_eq!(response.headers().get_value_or(CONTENT_LENGTH, 0), 5);
assert_eq!(response.headers().get_str_value_or(CONTENT_TYPE, ""), "text/plain");
assert_eq!(block_on(response.into_body().into_text()).unwrap(), "hello");
}
#[test]
fn text_does_not_override_existing_content_type() {
let response = HttpResponseBuilder::new_fake()
.header(CONTENT_TYPE, "text/custom")
.text("hello")
.build()
.unwrap();
assert_eq!(response.headers().get_str_value_or(CONTENT_TYPE, ""), "text/custom");
}
#[test]
fn status_with_status_code() {
let response = HttpResponseBuilder::new_fake()
.status(StatusCode::NOT_FOUND)
.text("not found")
.build()
.unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}
#[test]
fn version_setting() {
let response = HttpResponseBuilder::new_fake()
.version(Version::HTTP_2)
.text("hello")
.build()
.unwrap();
assert_eq!(response.version(), Version::HTTP_2);
}
#[test]
fn header_with_string_key_value() {
let response = HttpResponseBuilder::new_fake()
.header("X-Custom-Header", "custom-value")
.text("hello")
.build()
.unwrap();
assert_eq!(response.headers().get("X-Custom-Header").unwrap(), "custom-value");
}
#[test]
fn header_with_header_name_value() {
let header_name = HeaderName::from_static("x-test-header");
let header_value = HeaderValue::from_static("test-value");
let response = HttpResponseBuilder::new_fake()
.header(header_name.clone(), header_value.clone())
.text("hello")
.build()
.unwrap();
assert_eq!(response.headers().get(&header_name).unwrap(), &header_value);
}
#[test]
fn headers_mut_access() {
let mut response_builder = HttpResponseBuilder::new_fake();
// Test successful access to headers_mut
if let Some(headers) = response_builder.headers_mut() {
headers.insert("X-Mut-Header", "mut-value".parse().unwrap());
}
let response = response_builder.text("hello").build().unwrap();
assert_eq!(response.headers().get("X-Mut-Header").unwrap(), "mut-value");
}
#[test]
fn multiple_headers() {
let response = HttpResponseBuilder::new_fake()
.header("X-Header-1", "value1")
.header("X-Header-2", "value2")
.header("X-Header-3", "value3")
.text("hello")
.build()
.unwrap();
assert_eq!(response.headers().get("X-Header-1").unwrap(), "value1");
assert_eq!(response.headers().get("X-Header-2").unwrap(), "value2");
assert_eq!(response.headers().get("X-Header-3").unwrap(), "value3");
}
#[test]
fn direct_body_setting() {
let body = HttpBodyBuilder::new_fake().text("direct body");
let response = HttpResponseBuilder::new_fake().body(body).build().unwrap();
assert_eq!(block_on(response.into_body().into_text()).unwrap(), "direct body");
}
#[test]
fn chained_operations() {
let response = HttpResponseBuilder::new_fake()
.status(201)
.version(Version::HTTP_11)
.header("X-Custom", "value")
.header(CONTENT_TYPE, "application/custom")
.text("chained")
.build()
.unwrap();
assert_eq!(response.status(), StatusCode::CREATED);
assert_eq!(response.version(), Version::HTTP_11);
assert_eq!(response.headers().get("X-Custom").unwrap(), "value");
assert_eq!(response.headers().get(CONTENT_TYPE).unwrap(), "application/custom");
assert_eq!(block_on(response.into_body().into_text()).unwrap(), "chained");
}
#[test]
fn custom_body_functionality() {
let builder = HttpBodyBuilder::new_fake();
let body = create_stream_body_from_chunks(&builder, &[b"custom", b" body", b" content"], &HttpBodyOptions::default());
let response = HttpResponseBuilder::new_fake().body(body).build().unwrap();
assert_eq!(block_on(response.into_body().into_text()).unwrap(), "custom body content");
}
#[test]
fn custom_body_sets_body_from_custom_body_impl() {
let builder = HttpBodyBuilder::new_fake();
let response = HttpResponseBuilder::new_fake()
.status(200)
.custom_body(SingleChunkBody::new(BytesView::copied_from_slice(b"custom payload", &builder)))
.build()
.unwrap();
assert_eq!(block_on(response.into_body().into_text()).unwrap(), "custom payload");
}
#[test]
fn stream_sets_body_from_chunks() {
let builder = HttpBodyBuilder::new_fake();
let chunks: Vec<crate::Result<BytesView>> = vec![
Ok(BytesView::copied_from_slice(b"hello ", &builder)),
Ok(BytesView::copied_from_slice(b"streaming ", &builder)),
Ok(BytesView::copied_from_slice(b"world", &builder)),
];
let response = HttpResponseBuilder::new_fake()
.status(200)
.stream(futures::stream::iter(chunks))
.build()
.unwrap();
// Streams don't have a known content length
assert!(response.headers().get(CONTENT_LENGTH).is_none());
assert_eq!(block_on(response.into_body().into_text()).unwrap(), "hello streaming world");
}
#[test]
fn bytes_body_ok() {
let builder = HttpBodyBuilder::new_fake();
let response = HttpResponseBuilder::new_fake()
.bytes(BytesView::copied_from_slice(b"hello", &builder))
.build()
.unwrap();
assert_eq!(response.headers().get_value_or(CONTENT_LENGTH, 0), 5);
assert!(response.headers().get(CONTENT_TYPE).is_none());
assert_eq!(block_on(response.into_body().into_bytes()).unwrap(), b"hello");
}
#[test]
fn empty_body_ok() {
let response = HttpResponseBuilder::new_fake().build().unwrap();
assert_eq!(response.headers().get_value_or(CONTENT_LENGTH, -1), 0);
assert!(response.headers().get(CONTENT_TYPE).is_none());
assert_eq!(block_on(response.into_body().into_bytes()).unwrap().len(), 0,);
}
#[test]
fn content_length_set_to_zero_when_body_has_no_length() {
// Create a body without a known content length
let body = HttpBodyBuilder::new_fake().empty();
let response = HttpResponseBuilder::new_fake().body(body).build().unwrap();
// Content-Length should be set to 0 for empty body
assert_eq!(response.headers().get_value_or(CONTENT_LENGTH, -1), 0);
}
#[test]
fn bytes_with_different_data() {
let data = b"binary data";
let payload = BytesView::copied_from_slice(data, &HttpBodyBuilder::new_fake());
let response = HttpResponseBuilder::new_fake().bytes(payload).build().unwrap();
assert_eq!(response.headers().get_value_or(CONTENT_LENGTH, 0), data.len() as u64);
}
#[derive(Serialize, Debug)]
struct JsonData {
id: u32,
}
#[test]
fn extension_attaches_to_response() {
#[derive(Clone, Debug, PartialEq)]
struct RequestId(String);
let response = HttpResponseBuilder::new_fake()
.status(200)
.extension(RequestId("req-123".to_string()))
.build()
.unwrap();
let id = response.extensions().get::<RequestId>().expect("extension should be present");
assert_eq!(id.0, "req-123");
}
#[test]
fn extension_with_multiple_types() {
#[derive(Clone, Debug)]
struct RequestId(String);
#[derive(Clone, Debug)]
struct TraceId(u64);
let response = HttpResponseBuilder::new_fake()
.status(200)
.extension(RequestId("req-456".to_string()))
.extension(TraceId(12345))
.build()
.unwrap();
let request_id = response.extensions().get::<RequestId>().unwrap();
let trace_id = response.extensions().get::<TraceId>().unwrap();
assert_eq!(request_id.0, "req-456");
assert_eq!(trace_id.0, 12345);
}
}