xdiff-live 0.1.1

A live diff tool for comparing files and directories.
Documentation
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
//! Configuration management for XDiff-NG tools.
//!
//! This module provides configuration structures and utilities for loading
//! and validating configuration files for both `xdiff` and `xreq` tools.
//! It handles YAML configuration parsing, HTTP request building, and response processing.

mod xdiff;
mod xreq;

use anyhow::{Ok, Result};
use async_trait::async_trait;
use reqwest::{
    header::{self, HeaderMap, HeaderName, HeaderValue},
    Client, Method, Response,
};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::json;
use std::fmt::Write;
use std::str::FromStr;
use tokio::fs;
use url::Url;

pub use crate::{ExtraArgs, ResponseProfile};
pub use xdiff::*;
pub use xreq::*;

/// Checks if a value is equal to its default.
///
/// This utility function is used in serde serialization to determine
/// whether to skip serializing fields that have default values.
///
/// # Arguments
///
/// * `v` - The value to check against its default
///
/// # Returns
///
/// `true` if the value equals its default, `false` otherwise.
pub fn is_default<T: Default + PartialEq>(v: &T) -> bool {
    v == &T::default()
}

/// Trait for loading configuration from YAML files.
///
/// This trait provides functionality to load and parse configuration
/// from YAML files or strings, with built-in validation.
///
/// # Type Requirements
///
/// Types implementing this trait must also implement:
/// - `ValidateConfig` for validation logic
/// - `DeserializeOwned` for YAML deserialization
#[async_trait]
pub trait LoadConfig
where
    Self: ValidateConfig + DeserializeOwned,
{
    /// Loads configuration from a YAML file.
    ///
    /// This method reads the specified file and parses it as YAML,
    /// then validates the resulting configuration.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the YAML configuration file
    ///
    /// # Returns
    ///
    /// A `Result` containing the parsed and validated configuration,
    /// or an error if loading or validation fails.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use xdiff_live::config::{DiffConfig, LoadConfig};
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = DiffConfig::load_yaml("config.yml").await?;
    /// # Ok(())
    /// # }
    /// ```
    async fn load_yaml(path: &str) -> Result<Self> {
        let content = fs::read_to_string(path).await?;
        Self::from_yaml(&content)
    }

    /// Loads configuration from a YAML string.
    ///
    /// This method parses the provided YAML string and validates
    /// the resulting configuration.
    ///
    /// # Arguments
    ///
    /// * `content` - YAML content as a string
    ///
    /// # Returns
    ///
    /// A `Result` containing the parsed and validated configuration,
    /// or an error if parsing or validation fails.
    ///
    /// # Examples
    ///
    /// ```
    /// use xdiff_live::config::{LoadConfig, DiffConfig};
    ///
    /// let yaml = r#"
    /// profile1:
    ///   req1:
    ///     url: https://example.com
    ///   req2:
    ///     url: https://example.org
    /// "#;
    ///
    /// let config = DiffConfig::from_yaml(yaml).unwrap();
    /// ```
    fn from_yaml(content: &str) -> Result<Self> {
        let config: Self = serde_yaml::from_str(content)?;
        config.validate()?;
        Ok(config)
    }
}

/// Trait for validating configuration structures.
///
/// This trait ensures that loaded configurations are valid and
/// contain all required fields with appropriate values.
pub trait ValidateConfig {
    /// Validates the configuration.
    ///
    /// # Returns
    ///
    /// `Ok(())` if the configuration is valid, or an error describing
    /// what validation failed.
    fn validate(&self) -> Result<()>;
}

/// Configuration for a single HTTP request.
///
/// This structure defines all the parameters needed to make an HTTP request,
/// including method, URL, headers, query parameters, and body content.
/// It serves as the base configuration for both individual requests and
/// request comparisons.
///
/// # Examples
///
/// ```
/// use xdiff_live::config::RequestProfile;
/// use reqwest::Method;
/// use url::Url;
///
/// let profile = RequestProfile {
///     method: Method::GET,
///     url: Url::parse("https://api.example.com/users").unwrap(),
///     params: None,
///     headers: Default::default(),
///     body: None,
/// };
/// ```
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct RequestProfile {
    /// HTTP method (GET, POST, PUT, DELETE, etc.)
    #[serde(with = "http_serde::method", default)]
    pub method: Method,
    /// Target URL for the request
    pub url: Url,
    /// Query parameters as JSON value
    // skip_serializing_if
    // 调用函数来确定是否跳过序列化该字段。
    // 给定的函数必须可调用为 fn(&T) -> bool,尽管它可能是T上的通用函数。
    // 例如,skip_serializing_if = "Option::is_none"将跳过为None的选项。
    #[serde(skip_serializing_if = "empty_json_value", default)]
    // #[serde(default)]: If the value is not present when deserializing, use the Default::default().
    pub params: Option<serde_json::Value>,
    /// HTTP headers for the request
    #[serde(
        skip_serializing_if = "HeaderMap::is_empty",
        with = "http_serde::header_map",
        default
    )]
    pub headers: HeaderMap,
    /// Request body content as JSON value
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub body: Option<serde_json::Value>,
}

/// Checks if a JSON value is empty or null.
///
/// This function is used by serde to determine whether to skip
/// serializing optional JSON values that are empty.
///
/// # Arguments
///
/// * `v` - Optional JSON value to check
///
/// # Returns
///
/// `true` if the value is None, null, or an empty object; `false` otherwise.
fn empty_json_value(v: &Option<serde_json::Value>) -> bool {
    v.as_ref().map_or(true, |v| {
        v.is_null() || (v.is_object() && v.as_object().unwrap().is_empty())
    })
}

/// Extended response wrapper with additional processing capabilities.
///
/// This structure wraps a `reqwest::Response` and provides additional
/// methods for extracting and formatting response data according to
/// filtering rules defined in response profiles.
#[derive(Debug)]
pub struct ResponseExt(Response);

impl ResponseExt {
    /// Extracts the inner Response object.
    ///
    /// # Returns
    ///
    /// The wrapped `reqwest::Response` object.
    pub fn into_inner(self) -> Response {
        self.0
    }

    /// Extracts formatted text from the response according to profile rules.
    ///
    /// This method processes the HTTP response and formats it as text,
    /// applying any skip rules defined in the response profile for headers
    /// and body content.
    ///
    /// # Arguments
    ///
    /// * `profile` - Response profile containing skip rules for headers and body
    ///
    /// # Returns
    ///
    /// A `Result<String>` containing the formatted response text, including
    /// status line, filtered headers, and filtered body content.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use xdiff_live::config::{ResponseExt, ResponseProfile};
    ///
    /// # async fn example(response_ext: ResponseExt) -> Result<(), Box<dyn std::error::Error>> {
    /// let profile = ResponseProfile {
    ///     skip_headers: vec!["date".to_string(), "server".to_string()],
    ///     skip_body: vec!["timestamp".to_string()],
    /// };
    ///
    /// let formatted_text = response_ext.get_text(&profile).await?;
    /// println!("{}", formatted_text);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_text(self, profile: &ResponseProfile) -> Result<String> {
        let res = self.0;
        let mut output = get_status_text(&res)?;

        write!(
            &mut output,
            "{}",
            get_headers_text(&res, &profile.skip_headers)?
        )?;

        // let mut output = get_headers_text(&res, &profile.skip_headers)?;
        // let content_type = get_content_type(res.headers());
        // let text = res.text().await?;

        // match content_type.as_deref() {
        //     Some("application/json") => {
        //         let text = filter_json(&text, &profile.skip_body)?;
        //         output.push_str(&text);
        //     }
        //     _ => {
        //         output.push_str(&text);
        //     }
        // }

        writeln!(
            &mut output,
            "{}",
            get_body_text(res, &profile.skip_body).await?
        )?;

        Ok(output)
    }

    /// Extracts all header keys from the response.
    ///
    /// This method returns a list of all header names present in the response,
    /// which can be useful for debugging or dynamic header processing.
    ///
    /// # Returns
    ///
    /// A vector of header names as strings.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use xdiff_live::config::ResponseExt;
    ///
    /// # fn example(response_ext: &ResponseExt) {
    /// let header_keys = response_ext.get_header_keys();
    /// println!("Response headers: {:?}", header_keys);
    /// # }
    /// ```
    pub fn get_header_keys(&self) -> Vec<String> {
        let res = &self.0;
        let headers = res.headers();
        headers
            .iter()
            .map(|(k, _)| k.as_str().to_string())
            .collect()
    }
}

/// Extracts and formats the body text from an HTTP response.
///
/// This function processes the response body according to its content type
/// and applies filtering rules for JSON content. For JSON responses, it
/// filters out specified fields; for other content types, it returns the
/// raw text.
///
/// # Arguments
///
/// * `res` - The HTTP response to process
/// * `skip_body` - A slice of field names to skip when filtering JSON content
///
/// # Returns
///
/// A `Result<String>` containing the processed body text, or an error if
/// processing fails.
///
/// # Examples
///
/// ```no_run
/// use xdiff_live::config::get_body_text;
///
/// # async fn example(response: reqwest::Response) -> Result<(), Box<dyn std::error::Error>> {
/// let skip_fields = vec!["timestamp".to_string(), "request_id".to_string()];
/// let body_text = get_body_text(response, &skip_fields).await?;
/// println!("{}", body_text);
/// # Ok(())
/// # }
/// ```
pub async fn get_body_text(res: Response, skip_body: &[String]) -> Result<String> {
    let content_type = get_content_type(res.headers());
    let text = res.text().await?;

    // match content_type.as_deref() {
    //     Some("application/json") => {
    //         let text = filter_json(&text, &profile.skip_body)?;
    //         writeln!(&mut output, "{}", text)?;
    //     }
    //     _ => {
    //         writeln!(&mut output, "{}", text)?;
    //     }
    // }

    match content_type.as_deref() {
        Some("application/json") => filter_json(&text, skip_body),
        _ => Ok(text),
    }
}

/// Formats the HTTP status line from a response.
///
/// This function extracts the HTTP version and status code from a response
/// and formats them into a human-readable status line.
///
/// # Arguments
///
/// * `res` - The HTTP response to extract status from
///
/// # Returns
///
/// A `Result<String>` containing the formatted status line (e.g., "HTTP/1.1 200 OK\n").
///
/// # Examples
///
/// ```no_run
/// use xdiff_live::config::get_status_text;
///
/// # fn example(response: &reqwest::Response) -> Result<(), Box<dyn std::error::Error>> {
/// let status_line = get_status_text(response)?;
/// println!("{}", status_line);
/// # Ok(())
/// # }
/// ```
pub fn get_status_text(res: &Response) -> Result<String> {
    Ok(format!("{:?} {}\n", res.version(), res.status()))
}

/// Formats HTTP headers from a response, excluding specified headers.
///
/// This function extracts headers from an HTTP response and formats them
/// as text, optionally skipping headers specified in the skip list.
///
/// # Arguments
///
/// * `res` - The HTTP response to extract headers from
/// * `skip_headers` - A slice of header names to exclude from the output
///
/// # Returns
///
/// A `Result<String>` containing the formatted headers text, or an error if
/// formatting fails.
///
/// # Examples
///
/// ```no_run
/// use xdiff_live::config::get_headers_text;
///
/// # fn example(response: &reqwest::Response) -> Result<(), Box<dyn std::error::Error>> {
/// let skip_list = vec!["date".to_string(), "server".to_string()];
/// let headers_text = get_headers_text(response, &skip_list)?;
/// println!("{}", headers_text);
/// # Ok(())
/// # }
/// ```
pub fn get_headers_text(res: &Response, skip_headers: &[String]) -> Result<String> {
    let mut output = String::new();
    // write!(output, "{:?} {}\r", self.0.version(), self.0.status())?;
    // output.push_str(&format!("{:?} {}\n", res.version(), res.status()));

    let headers = res.headers();
    for (k, v) in headers.iter() {
        if !skip_headers.contains(&k.to_string()) {
            // if !profile.skip_headers.iter().any(|x| x == k.as_str( ) ) {
            output.push_str(&format!("{}: {:?}\n", k, v));
            // write!(&mut output, "{}: {:?}\n", k, v)?;
        }
    }

    Ok(output)
}

fn filter_json(text: &str, skip: &[String]) -> Result<String> {
    let mut json: serde_json::Value = serde_json::from_str(text)?;

    // match json {
    //     serde_json::Value::Object(ref mut obj) => {
    //         for key in skip {
    //             obj.remove(key);
    //         }
    //     }
    //     _ =>
    //         // for now we just ignore non_object values, we don't how to filter them
    //         //  In future, we might support array of primitives
    //         {}
    // }

    // for now we just ignore non_object values, we don't how to filter them
    // In future, we might support array of objects
    if let serde_json::Value::Object(ref mut obj) = json {
        for key in skip {
            obj.remove(key);
        }
    }

    Ok(serde_json::to_string_pretty(&json)?)
}

impl RequestProfile {
    pub fn new(
        method: Method,
        url: Url,
        params: Option<serde_json::Value>,
        headers: HeaderMap,
        body: Option<serde_json::Value>,
    ) -> Self {
        Self {
            method,
            url,
            params,
            headers,
            body,
        }
    }

    pub async fn send(&self, args: &ExtraArgs) -> Result<ResponseExt> {
        let (headers, query, body) = self.generate(args)?;
        let client = Client::new();
        let req = client
            .request(self.method.clone(), self.url.clone())
            .query(&query)
            .headers(headers)
            .body(body)
            .build()?;

        let res = client.execute(req).await?;

        Ok(ResponseExt(res))
    }

    pub fn get_url(&self, args: &ExtraArgs) -> Result<String> {
        let (_, params, _) = self.generate(args)?;
        let mut url = self.url.clone();
        if !params.as_object().unwrap().is_empty() {
            let query = serde_qs::to_string(&params)?;
            url.set_query(Some(&query));
        }
        // url.set_query(None);
        // let mut query = serde_qs::to_string(&query)?;
        // if !query.is_empty() {
        //     // url.set_query(Some(&query));
        //     write!(url, "?{}", &query)?;
        // }
        Ok(url.to_string())
    }

    fn generate(&self, args: &ExtraArgs) -> Result<(HeaderMap, serde_json::Value, String)> {
        let mut headers = self.headers.clone();
        let mut query = self.params.clone().unwrap_or_else(|| json!({}));
        let mut body = self.body.clone().unwrap_or_else(|| json!({}));

        for (k, v) in &args.headers {
            // println!("测试:{}{}", k, v);
            headers.insert(HeaderName::from_str(k)?, HeaderValue::from_str(v)?);
        }

        if !headers.contains_key(header::CONTENT_TYPE) {
            // println!("测试:{}___{:?}", header::CONTENT_TYPE, HeaderValue::from_static("application/json"));
            headers.insert(
                header::CONTENT_TYPE,
                // 用于指示资源的媒体类型。
                // 在响应中,Content-Type 标头告诉客户端返回内容的实际内容类型。
                // 在某些情况下,浏览器会进行 MIME 嗅探,但不一定会遵循此标头的值;
                // 为了防止这种行为,可以将标头 X-Content-Type-Options 设置为 nosniff。
                // 在请求(例如 POST 或 PUT)中,客户端告诉服务器实际发送的数据类型。
                HeaderValue::from_static("application/json"),
            );
            // "Content-Type" 是 HTTP 请求头部中的一个字段,它用于指定请求或响应中携带的实体数据的媒体类型(即数据的类型和格式)
        }

        for (k, v) in &args.query {
            query[k] = v.parse()?;
            // parse() -> Result<T, <T as FromStr>::Err>
            // 将此字符串切片解析为另一种类型。
            // 由于解析非常通用,因此可能会导致类型推断出现问题。
            // 因此,解析是您会看到被亲切地称为“turbofish”的语法的少数情况之一:::<>。
            // 这有助于推理算法具体了解您要解析的类型。
        }

        for (k, v) in &args.body {
            body[k] = v.parse()?;
        }

        // println!("测试:{:?}", headers);

        let content_type = get_content_type(&headers);

        // println!("测试:{:?}", content_type);
        
        match content_type.as_deref() {
            // as_deref()是一个Rust标准库中的方法,它用于将Option<&T>转换为Option<&U>,其中T和U是具体的类型。
            Some("application/json") => {
                let body = serde_json::to_string(&body)?;
                Ok((headers, query, body))
            }
            Some("application/x-www-form-urlencoded" | "multipart/form-data") => {
                let body = serde_urlencoded::to_string(&body)?;
                Ok((headers, query, body))
            }
            _ => Err(anyhow::anyhow!("unsupported content-type")),
        }
    }
}

impl ValidateConfig for RequestProfile {
    fn validate(&self) -> Result<()> {
        if let Some(params) = self.params.as_ref() {
            if !params.is_object() {
                return Err(anyhow::anyhow!(
                    "Params must be an object but got\n{}",
                    serde_yaml::to_string(params)?
                ));
            }
        }
        if let Some(body) = self.body.as_ref() {
            if !body.is_object() {
                return Err(anyhow::anyhow!(
                    "Body must be an object but got\n{}",
                    serde_yaml::to_string(body)?
                ));
            }
        }
        Ok(())
    }
}

fn get_content_type(headers: &HeaderMap) -> Option<String> {
    headers
        .get(header::CONTENT_TYPE)
        // .map(|v| v.to_str().unwrap().split(';').next())
        // .flatten()
        // .map(|v| v.to_string())
        .and_then(|v| v.to_str().unwrap().split(";").next().map(|v| v.to_string()))
}

impl FromStr for RequestProfile {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self> {
        let mut url = Url::parse(s)?;
        let qs = url.query_pairs();
        let mut params = json!({});
        for (k, v) in qs {
            params[&*k] = v.parse()?;
        }

        url.set_query(None);

        Ok(RequestProfile::new(
            Method::GET,
            url,
            Some(params),
            HeaderMap::new(),
            None,
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mockito::{mock, Mock};
    use reqwest::StatusCode;

    #[tokio::test]
    async fn request_profile_send_should_work() {
        let _m = mock_for_url("/todo?a=1&b=2", json!({"id": 1, "title": "todo"}));
        let res = get_response("/todo?a=1&b=2", &Default::default())
            .await
            .into_inner();
        assert_eq!(res.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn request_profile_send_with_extra_args_should_work() {
        let _m = mock_for_url("/todo?a=1&b=3", json!({"id": 1, "title": "todo"}));

        let args = ExtraArgs::new_with_query(vec![("b".into(), "3".into())]);

        let res = get_response("/todo?a=1&b=2", &args).await.into_inner();
        assert_eq!(res.status(), StatusCode::OK);
    }

    #[test]
    fn request_profile_get_url_should_work() {
        let profile = get_profile("/todo?a=1&b=2");
        assert_eq!(
            profile.get_url(&Default::default()).unwrap(),
            get_url("/todo?a=1&b=2") // format!("{}/todo?a=1&b=2", mockito::server_url())
        );
    }

    #[test]
    fn request_profile_get_url_with_args_should_work() {
        let profile = get_profile("/todo?a=1&b=2");

        let args = ExtraArgs::new_with_query(vec![("c".into(), "3".into())]);

        assert_eq!(
            profile.get_url(&args).unwrap(),
            get_url("/todo?a=1&b=2&c=3") // format!("{}/todo?a=1&b=2&c=3", mockito::server_url())
        );
    }

    #[test]
    fn request_profile_validate_should_work() {
        let profile = get_profile("/todo?a=1&b=2");
        assert!(profile.validate().is_ok());
    }

    #[test]
    fn request_profile_with_bad_params_validate_should_fail() {
        let profile = RequestProfile::new(
            Method::GET,
            Url::parse("http://localhost:1234/todo").unwrap(),
            Some(json!([1, 2, 3])),
            HeaderMap::new(),
            None,
        );
        let result = profile.validate();
        assert!(profile.validate().is_err());
        assert_eq!(
            result.unwrap_err().to_string(),
            "Params must be an object but got\n- 1\n- 2\n- 3\n"
        );
    }

    #[tokio::test]
    async fn response_ext_get_text_should_work() {
        let _m = mock_for_url("/todo", json!({"id": 1, "title": "todo"}));
        let res = get_response("/todo", &Default::default()).await;

        let response_profile = ResponseProfile::new(
            vec!["connection".into(), "content-length".into()],
            vec!["title".into()],
        );
        assert_eq!(
            res.get_text(&response_profile).await.unwrap(),
            "HTTP/1.1 200 OK\ncontent-type: \"application/json\"\n{\n  \"id\": 1\n}\n"
        );
    }

    #[tokio::test]
    async fn response_ext_get_header_should_work() {
        let _m = mock_for_url("/todo", json!({"id": 1, "title": "todo"}));
        let res = get_response("/todo", &Default::default()).await;
        let mut sorted_header_keys = res.get_header_keys();
        sorted_header_keys.sort();
        let expected_header_keys = vec!["connection", "content-length", "content-type"];
        // assert_eq!(
        //     res.get_header_keys(),
        //     &["connection", "content-type", "content-length"]
        // );
        assert_eq!(sorted_header_keys, expected_header_keys);
    }

    #[test]
    fn test_get_content_type() {
        let mut headers = HeaderMap::new();
        headers.insert(
            header::CONTENT_TYPE,
            HeaderValue::from_static("application/json; charset=utf-8"),
        );
        assert_eq!(
            get_content_type(&headers),
            Some("application/json".to_string())
        );
    }

    #[tokio::test]
    async fn get_status_text_should_work() {
        let _m = mock_for_url("/todo", json!({"id": 1, "title": "todo"}));
        let res = get_response("/todo", &Default::default())
            .await
            .into_inner();
        assert_eq!(get_status_text(&res).unwrap(), "HTTP/1.1 200 OK\n");
    }

    #[tokio::test]
    async fn get_headers_text_should_work() {
        let _m = mock_for_url("/todo", json!({"id": 1, "title": "todo"}));
        let res = get_response("/todo", &Default::default())
            .await
            .into_inner();
        assert_eq!(
            get_headers_text(&res, &["connection".into(), "content-length".into()]).unwrap(),
            "content-type: \"application/json\"\n"
        );
    }

    #[tokio::test]
    async fn get_body_text_should_work() {
        let _m = mock_for_url("/todo", json!({"id": 1, "title": "todo"}));
        let res = get_response("/todo", &Default::default())
            .await
            .into_inner();
        assert_eq!(
            get_body_text(res, &["id".into()]).await.unwrap(),
            "{\n  \"title\": \"todo\"\n}"
        );
    }

    fn mock_for_url(path_and_query: &str, resp_body: serde_json::Value) -> Mock {
        mock("GET", path_and_query)
            .with_status(200)
            .with_header("content-type", "application/json")
            .with_body(serde_json::to_string(&resp_body).unwrap())
            .create()
    }

    fn get_url(path: &str) -> String {
        format!("{}{}", mockito::server_url(), path)
    }

    fn get_profile(path_and_query: &str) -> RequestProfile {
        let url = get_url(path_and_query);
        RequestProfile::from_str(&url).unwrap()
    }

    async fn get_response(path_and_query: &str, args: &ExtraArgs) -> ResponseExt {
        let profile = get_profile(path_and_query);
        profile.send(args).await.unwrap()
    }
}