toad-cli 0.4.0

A developer-friendly REST client called toad
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
use crate::capture::Capture;
use crate::interpolate::interpolate;
use crate::time_limit::TimeLimits;
use anyhow::{Context, Result, anyhow};
use indexmap::IndexMap;
use serde::Deserialize;
use std::{
    collections::HashMap,
    fs,
    path::{Path, PathBuf},
};

#[derive(Debug)]
pub struct RequestFile {
    pub config: Config,
    pub vars: HashMap<String, String>,
    pub profiles: HashMap<String, HashMap<String, String>>,
    pub requests: IndexMap<String, RequestDef>,
}

/// The collection file as written. Requests are kept as raw tables so each one can be checked
/// on its own, and an error can name the request it came from.
#[derive(Deserialize)]
struct RawRequestFile {
    #[serde(default)]
    config: Config,

    #[serde(default)]
    vars: HashMap<String, String>,

    #[serde(default)]
    profiles: HashMap<String, HashMap<String, String>>,

    #[serde(flatten)]
    requests: IndexMap<String, toml::Value>,
}

/// If `name` looks like a misspelling of one of the special tables, returns that table's name.
fn misspelled_section(name: &str) -> Option<&'static str> {
    ["config", "vars", "profiles"]
        .into_iter()
        .find(|section| edit_distance(&name.to_lowercase(), section) <= 2)
}

/// Levenshtein distance between two strings.
fn edit_distance(a: &str, b: &str) -> usize {
    let b: Vec<char> = b.chars().collect();
    let mut prev: Vec<usize> = (0..=b.len()).collect();
    for (i, ca) in a.chars().enumerate() {
        let mut cur = vec![i + 1];
        for (j, cb) in b.iter().enumerate() {
            let cost = usize::from(ca != *cb);
            cur.push((prev[j] + cost).min(prev[j + 1] + 1).min(cur[j] + 1));
        }
        prev = cur;
    }
    prev[b.len()]
}

impl RequestFile {
    /// Parses a collection file. Unknown settings are an error, so a misspelled check can't be
    /// skipped without anyone noticing.
    pub fn parse(content: &str) -> Result<RequestFile> {
        let raw: RawRequestFile = toml::from_str(content)?;

        let mut requests = IndexMap::with_capacity(raw.requests.len());
        for (name, value) in raw.requests {
            if !value.is_table() {
                return Err(anyhow!(
                    "'{}' is not a request - every top-level table is a request, and settings for \
                     every request go in [config]",
                    name
                ));
            }
            let request: RequestDef =
                value
                    .try_into()
                    .map_err(|e: toml::de::Error| match misspelled_section(&name) {
                        Some(section) => anyhow!(
                            "request '{}': {} (did you mean [{}]?)",
                            name,
                            e.message(),
                            section
                        ),
                        None => anyhow!("request '{}': {}", name, e.message()),
                    })?;
            requests.insert(name, request);
        }

        Ok(RequestFile {
            config: raw.config,
            vars: raw.vars,
            profiles: raw.profiles,
            requests,
        })
    }
}

#[derive(Debug, Deserialize, Default, Clone)]
#[serde(deny_unknown_fields)]
pub struct Config {
    #[serde(default)]
    pub ignore_ssl: bool,

    #[serde(default)]
    pub use_custom_ca: Option<String>,

    #[serde(default)]
    pub auth: Option<String>,

    #[serde(default)]
    pub expect_max_ms: Option<u64>,

    #[serde(default)]
    pub retry: Option<u32>,

    #[serde(default)]
    pub retry_delay_ms: Option<u64>,
}

#[derive(Debug, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct RequestDef {
    #[serde(default = "default_method")]
    pub method: String,

    pub url: String,

    #[serde(default)]
    pub headers: HashMap<String, String>,

    #[serde(default)]
    pub query: HashMap<String, String>,

    pub body: Option<String>,

    pub body_file: Option<String>,

    /// When false, `body`/`body_file` is sent exactly as written, without `{{var}}` interpolation
    #[serde(default = "default_true")]
    pub interpolate_body: bool,

    #[serde(default)]
    pub auth: Option<String>,

    pub expect_status: Option<Vec<u16>>,

    pub expect_max_ms: Option<u64>,

    pub retry: Option<u32>,

    pub retry_delay_ms: Option<u64>,

    #[serde(default = "default_timeout")]
    pub timeout_secs: u64,

    #[serde(default)]
    pub capture: IndexMap<String, String>,

    #[serde(default)]
    pub ignore_config: Vec<String>,

    /// Parsed from `capture` by `parse_captures`.
    #[serde(skip)]
    pub captures: Vec<Capture>,
}

impl RequestDef {
    /// The body as it will be sent: interpolated, unless `interpolate_body` is false.
    pub fn resolved_body(&self, vars: &HashMap<String, String>) -> Option<Result<String>> {
        self.body.as_ref().map(|body| {
            if self.interpolate_body {
                interpolate(body, vars)
            } else {
                Ok(body.clone())
            }
        })
    }

    /// The `expect_max_ms` for this request: its own value, or the default from `config`.
    pub fn max_ms(&self, config: &Config) -> Option<u64> {
        self.expect_max_ms.or(config.expect_max_ms)
    }
}

const CONFIG_KEYS: [&str; 6] = [
    "auth",
    "use_custom_ca",
    "ignore_ssl",
    "expect_max_ms",
    "retry",
    "retry_delay_ms",
];

impl Config {
    /// Returns a copy of this config with the listed keys reset to their defaults.
    pub fn without(&self, ignored: &[String]) -> Config {
        let mut config = self.clone();
        for key in ignored {
            match key.as_str() {
                "auth" => config.auth = None,
                "use_custom_ca" => config.use_custom_ca = None,
                "ignore_ssl" => config.ignore_ssl = false,
                "expect_max_ms" => config.expect_max_ms = None,
                "retry" => config.retry = None,
                "retry_delay_ms" => config.retry_delay_ms = None,
                _ => {}
            }
        }
        config
    }
}

fn default_method() -> String {
    "GET".to_string()
}
fn default_timeout() -> u64 {
    30
}
fn default_true() -> bool {
    true
}

fn resolve_relative(path: &str, base_dir: &Path) -> PathBuf {
    if Path::new(path).is_absolute() {
        PathBuf::from(path)
    } else {
        base_dir.join(path)
    }
}

pub fn load_ext_body(rf: &mut RequestFile, request_file_path: &Path) -> Result<()> {
    let base_dir = request_file_path.parent().unwrap_or(Path::new("."));

    for (request_name, request) in &mut rf.requests {
        match (&request.body, &request.body_file) {
            (Some(_), Some(_)) => {
                return Err(anyhow!(
                    "request '{}' specifies both 'body' and 'body_file' - only one may be specified",
                    request_name
                ));
            }
            (None, Some(path)) => {
                let body_path = resolve_relative(path, base_dir);

                let content = fs::read_to_string(body_path).with_context(|| {
                    format!(
                        "could not read body_file '{}' in request '{}'",
                        path, request_name
                    )
                })?;
                request.body = Some(content);
            }
            _ => {}
        }
    }
    Ok(())
}

pub fn parse_captures(rf: &mut RequestFile) -> Result<()> {
    for (request_name, request) in &mut rf.requests {
        request.captures = request
            .capture
            .iter()
            .map(|(name, expr)| {
                Capture::parse(name, expr).with_context(|| {
                    format!("invalid capture '{}' in request '{}'", name, request_name)
                })
            })
            .collect::<Result<_>>()?;
    }
    Ok(())
}

pub fn validate_ignore_config(rf: &RequestFile) -> Result<()> {
    for (request_name, request) in &rf.requests {
        for key in &request.ignore_config {
            if !CONFIG_KEYS.contains(&key.as_str()) {
                return Err(anyhow!(
                    "unknown key '{}' in ignore_config for request '{}' (valid keys: {})",
                    key,
                    request_name,
                    CONFIG_KEYS.join(", ")
                ));
            }
        }
    }
    Ok(())
}

pub fn validate_expect_max_ms(rf: &RequestFile) -> Result<()> {
    if rf.config.expect_max_ms == Some(0) {
        return Err(anyhow!("[config] expect_max_ms must be greater than 0"));
    }
    for (request_name, request) in &rf.requests {
        if request.expect_max_ms == Some(0) {
            return Err(anyhow!(
                "request '{}' has expect_max_ms = 0 - it must be greater than 0",
                request_name
            ));
        }
    }
    Ok(())
}

/// Warnings for time limits that can never trigger because the request's timeout is shorter.
pub fn time_limit_warnings(
    requests: &[(String, RequestDef)],
    config: &Config,
    limits: &TimeLimits,
) -> Vec<String> {
    requests
        .iter()
        .filter_map(|(name, req)| {
            let max_ms = req.max_ms(&config.without(&req.ignore_config))?;
            let effective = limits.effective(max_ms)?;
            (effective >= req.timeout_secs * 1000).then(|| {
                format!(
                    "warning: request '{}' expects at most {}, but timeout_secs = {} will stop it first",
                    name,
                    limits.describe(max_ms),
                    req.timeout_secs
                )
            })
        })
        .collect()
}

pub fn resolve_custom_ca(config: &mut Config, request_file_path: &Path) {
    let base_dir = request_file_path.parent().unwrap_or(Path::new("."));

    if let Some(path) = &config.use_custom_ca {
        config.use_custom_ca = Some(
            resolve_relative(path, base_dir)
                .to_string_lossy()
                .to_string(),
        );
    }
}

pub fn load_requests(rf: &RequestFile, name: Option<&str>) -> Result<Vec<(String, RequestDef)>> {
    match name {
        Some(n) => {
            let req = rf.requests.get(n).cloned().ok_or_else(|| {
                let available: Vec<&String> = rf.requests.keys().collect();
                anyhow!("no request named '{}'. Available: {:?}", n, available)
            })?;
            Ok(vec![(n.to_string(), req)])
        }
        None => Ok(rf
            .requests
            .iter()
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn parse(toml_str: &str) -> RequestFile {
        RequestFile::parse(toml_str).unwrap()
    }

    #[test]
    fn unknown_request_key_names_the_request() {
        let err = RequestFile::parse(
            r#"
            [get-user]
            url = "http://x"
            expect_stauts = [200]
            "#,
        )
        .unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.starts_with("request 'get-user': unknown field `expect_stauts`"),
            "{msg}"
        );
    }

    #[test]
    fn unknown_request_subtable_is_an_error() {
        let err = RequestFile::parse(
            r#"
            [get-user]
            url = "http://x"

            [get-user.headres]
            X-Test = "1"
            "#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("unknown field `headres`"));
    }

    #[test]
    fn description_is_not_a_setting() {
        let err = RequestFile::parse(
            r#"
            [get-user]
            url = "http://x"
            description = "Fetch a user"
            "#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("unknown field `description`"));
    }

    #[test]
    fn unknown_config_key_is_an_error() {
        let err = RequestFile::parse(
            r#"
            [config]
            retyr = 3
            "#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("unknown field `retyr`"));
    }

    #[test]
    fn top_level_setting_is_an_error() {
        let err = RequestFile::parse(
            r#"
            retry = 3

            [get-user]
            url = "http://x"
            "#,
        )
        .unwrap_err();
        assert!(
            err.to_string()
                .contains("settings for every request go in [config]")
        );
    }

    #[test]
    fn misspelled_config_table_gets_a_hint() {
        let err = RequestFile::parse(
            r#"
            [confg]
            retry = 3
            "#,
        )
        .unwrap_err();
        assert_eq!(
            err.to_string(),
            "request 'confg': missing field `url` (did you mean [config]?)"
        );
    }

    #[test]
    fn request_names_are_not_mistaken_for_sections() {
        assert_eq!(misspelled_section("get-user"), None);
        assert_eq!(misspelled_section("login"), None);
        assert_eq!(misspelled_section("Vars"), Some("vars"));
        assert_eq!(misspelled_section("profile"), Some("profiles"));
    }

    #[test]
    fn without_resets_ignored_keys() {
        let config = Config {
            ignore_ssl: true,
            use_custom_ca: Some("ca.pem".to_string()),
            auth: Some("bearer {{token}}".to_string()),
            expect_max_ms: Some(500),
            retry: Some(3),
            retry_delay_ms: Some(250),
        };
        let config = config.without(&[
            "auth".to_string(),
            "ignore_ssl".to_string(),
            "expect_max_ms".to_string(),
            "retry".to_string(),
            "retry_delay_ms".to_string(),
        ]);
        assert_eq!(config.auth, None);
        assert!(!config.ignore_ssl);
        assert_eq!(config.expect_max_ms, None);
        assert_eq!(config.retry, None);
        assert_eq!(config.retry_delay_ms, None);
        assert_eq!(config.use_custom_ca.as_deref(), Some("ca.pem"));
    }

    #[test]
    fn request_max_ms_overrides_config() {
        let rf = parse(
            r#"
            [config]
            expect_max_ms = 1000

            [default]
            url = "http://x"

            [tight]
            url = "http://x"
            expect_max_ms = 300
            "#,
        );
        assert_eq!(rf.requests["default"].max_ms(&rf.config), Some(1000));
        assert_eq!(rf.requests["tight"].max_ms(&rf.config), Some(300));
    }

    #[test]
    fn zero_expect_max_ms_is_an_error() {
        let rf = parse(
            r#"
            [a]
            url = "http://x"
            expect_max_ms = 0
            "#,
        );
        let err = validate_expect_max_ms(&rf).unwrap_err();
        assert!(err.to_string().contains("must be greater than 0"));

        let rf = parse(
            r#"
            [config]
            expect_max_ms = 0
            "#,
        );
        assert!(validate_expect_max_ms(&rf).is_err());
    }

    #[test]
    fn warns_when_limit_cannot_trigger() {
        use crate::time_limit::TimeScale;

        let rf = parse(
            r#"
            [config]
            expect_max_ms = 15000

            [inherits]
            url = "http://x"

            [too-long]
            url = "http://x"
            expect_max_ms = 40000

            [ignored]
            url = "http://x"
            expect_max_ms = 40000
            ignore_config = ["expect_max_ms"]
            "#,
        );
        let requests = load_requests(&rf, None).unwrap();

        let warnings = time_limit_warnings(&requests, &rf.config, &TimeLimits::default());
        assert_eq!(warnings.len(), 2);
        assert_eq!(
            warnings[0],
            "warning: request 'too-long' expects at most 40000ms, but timeout_secs = 30 will stop it first"
        );

        let scaled = TimeLimits::resolve(None, Some("3"));
        let warnings = time_limit_warnings(&requests, &rf.config, &scaled);
        assert_eq!(warnings.len(), 3);
        assert!(warnings[0].contains("45000ms (15000ms x 3 from TOAD_TIME_SCALE)"));

        let off = TimeLimits::resolve(Some(TimeScale::Off), None);
        assert!(time_limit_warnings(&requests, &rf.config, &off).is_empty());
    }

    #[test]
    fn unknown_ignore_config_key_is_an_error() {
        let rf = parse(
            r#"
            [login]
            url = "http://x"
            ignore_config = ["auht"]
            "#,
        );
        let err = validate_ignore_config(&rf).unwrap_err();
        assert!(
            err.to_string()
                .contains("unknown key 'auht' in ignore_config")
        );
    }

    #[test]
    fn captures_are_parsed_in_order() {
        let mut rf = parse(
            r#"
            [create-user]
            url = "http://x"

            [create-user.capture]
            user_id = "$.id"
            location = "header:Location"
            "#,
        );
        parse_captures(&mut rf).unwrap();
        let names: Vec<&str> = rf.requests["create-user"]
            .captures
            .iter()
            .map(|c| c.name.as_str())
            .collect();
        assert_eq!(names, ["user_id", "location"]);
    }

    #[test]
    fn invalid_capture_is_an_error() {
        let mut rf = parse(
            r#"
            [create-user]
            url = "http://x"

            [create-user.capture]
            user_id = "id"
            "#,
        );
        let err = parse_captures(&mut rf).unwrap_err();
        assert_eq!(
            err.to_string(),
            "invalid capture 'user_id' in request 'create-user'"
        );
    }
}