hurl 8.0.0

Hurl, run and test HTTP requests
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
/*
 * Hurl (https://hurl.dev)
 * Copyright (C) 2026 Orange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
use std::iter::zip;
use std::time::Duration;

use chrono::{DateTime, Utc};

use crate::http::Call;
use crate::report::html::Testcase;
use crate::report::html::timeline::nice::NiceScale;
use crate::report::html::timeline::svg::Attribute::{
    Class, DX, DY, Fill, Filter, FloodOpacity, FontFamily, FontSize, FontWeight, Height, Href, Id,
    Opacity, StdDeviation, Stroke, StrokeWidth, TextDecoration, ViewBox, Width, X, Y,
};
use crate::report::html::timeline::svg::{Element, new_a};
use crate::report::html::timeline::unit::{
    Byte, Interval, Microsecond, Millisecond, Pixel, Px, Scale, Second, TimeUnit,
};
use crate::report::html::timeline::util::{
    new_failure_icon, new_retry_icon, new_stripes, new_success_icon, trunc_str,
};
use crate::report::html::timeline::{CALL_HEIGHT, CALL_INSET, CallContext, CallContextKind, svg};
use crate::util::redacted::Redact;

/// Returns the start and end date for these entries.
fn get_times_interval(calls: &[&Call]) -> Option<Interval<DateTime<Utc>>> {
    let begin = calls.first();
    let end = calls.last();
    match (begin, end) {
        (Some(start), Some(end)) => {
            let start = start.timings.begin_call;
            let end = end.timings.end_call;
            Some(Interval { start, end })
        }
        _ => None,
    }
}

impl Testcase {
    /// Returns the SVG string of this list of `calls`.
    pub fn get_waterfall_svg(
        &self,
        calls: &[&Call],
        call_ctxs: &[CallContext],
        secrets: &[&str],
    ) -> String {
        // Compute our scale (transform 0 based microsecond to 0 based pixels):
        let times = get_times_interval(calls);
        let times = match times {
            Some(t) => t,
            None => return String::new(),
        };

        let margin_top = 50.px();
        let margin_bottom = 250.px();
        let width = 1138.px();
        let height = (CALL_HEIGHT * calls.len()) + margin_top + margin_bottom;
        let height = Pixel::max(100.px(), height);

        let mut root = svg::new_svg();
        root.add_attr(ViewBox(0.0, 0.0, width.0, height.0));
        root.add_attr(Width(width.0.to_string()));
        root.add_attr(Height(height.0.to_string()));

        // Add styles, filters, symbols for success and failure icons:
        let elt = svg::new_style(include_str!("../resources/waterfall.css"));
        root.add_child(elt);
        let elt = new_filters();
        root.add_child(elt);
        let elt = new_success_icon("success");
        root.add_child(elt);
        let elt = new_failure_icon("failure");
        root.add_child(elt);
        let elt = new_retry_icon("retry");
        root.add_child(elt);

        // We add some space for the right last grid labels.
        let pixels_x = Interval::new(0.px(), width);
        let pixels_y = Interval::new(margin_top, height);
        let scale_x = Scale::new(times, pixels_x);

        let ticks_number = 10;
        let grid = new_grid(
            calls,
            times,
            ticks_number,
            scale_x,
            pixels_x,
            pixels_y,
            CALL_HEIGHT,
        );
        root.add_child(grid);

        let elts = zip(calls, call_ctxs).map(|(call, call_ctx)| {
            new_call(call, call_ctx, times, scale_x, pixels_x, pixels_y, secrets)
        });

        // We construct SVG calls from last to first so the detail of any call is not overridden
        // by the next call.
        elts.rev().for_each(|e| root.add_child(e));

        root.to_string()
    }
}

/// Returns the grid SVG with tick on "nice" times and stripes background.
fn new_grid(
    calls: &[&Call],
    times: Interval<DateTime<Utc>>,
    ticks_number: usize,
    scale_x: Scale,
    pixels_x: Interval<Pixel>,
    pixels_y: Interval<Pixel>,
    call_height: Pixel,
) -> Element {
    let mut grid = svg::new_group();
    let elt = new_stripes(calls.len(), call_height, pixels_x, pixels_y, "#f5f5f5");
    grid.add_child(elt);
    let elt = new_vert_lines(times, ticks_number, scale_x, pixels_y);
    grid.add_child(elt);
    grid
}

/// Returns the verticals lines with labels for the time ticks.
fn new_vert_lines(
    times: Interval<DateTime<Utc>>,
    ticks_number: usize,
    scale_x: Scale,
    pixels_y: Interval<Pixel>,
) -> Element {
    let mut group = svg::new_group();

    // We compute in which unit we're going to draw the grid
    let duration = times.end - times.start;
    let duration = duration.num_microseconds().unwrap() as f64;
    let duration = Microsecond(duration);
    let delta = Microsecond(duration.0 / ticks_number as f64);
    let (start, end) = match delta.0 {
        d if d < 1_000.0 => (TimeUnit::zero_mc(), TimeUnit::Microsecond(duration)),
        d if d < 1_000_000.0 => {
            let end = Millisecond::from(duration);
            (TimeUnit::zero_ms(), TimeUnit::Millisecond(end))
        }
        _ => {
            let end = Second::from(duration);
            (TimeUnit::zero_s(), TimeUnit::Second(end))
        }
    };
    let nice_scale = NiceScale::new(start.as_f64(), end.as_f64(), ticks_number);

    let mut t = start;
    let mut values = vec![];
    while t < end {
        let x = scale_x.to_pixel(Microsecond::from(t));
        // We want a integer pixel value:
        let x = x.0.round();
        values.push((x, t));
        t = t.add_raw(nice_scale.get_tick_spacing());
    }

    // Draw the vertical lines:
    let mut lines = svg::new_group();
    lines.add_attr(Class("grid-ticks".to_string()));
    lines.add_attr(Stroke("#ccc".to_string()));
    values.iter().for_each(|(x, _)| {
        if *x <= 0.0 {
            return;
        }
        let elt = svg::new_line(*x, 0.0, *x, pixels_y.end.0);
        lines.add_child(elt);
    });
    group.add_child(lines);

    // Finally, draw labels
    let mut labels = svg::new_group();
    labels.add_attr(FontSize("15px".to_string()));
    labels.add_attr(FontFamily("sans-serif".to_string()));
    labels.add_attr(Fill("#777".to_string()));
    values
        .iter()
        .map(|(x, t)| svg::new_text(*x + 5.0, 20.0, &format!("{} {}", t.as_f64(), t.unit())))
        .for_each(|l| labels.add_child(l));
    group.add_child(labels);

    group
}

/// Returns the SVG of this `call`.
/// `times` is the time interval of the complete run, `scale_x` allows to convert
/// between times and pixel for the X-axis.
fn new_call(
    call: &Call,
    call_ctx: &CallContext,
    times: Interval<DateTime<Utc>>,
    scale_x: Scale,
    pixels_x: Interval<Pixel>,
    pixels_y: Interval<Pixel>,
    secrets: &[&str],
) -> Element {
    let mut call_elt = svg::new_group();

    let summary = new_call_timings(call, call_ctx, times, scale_x, pixels_y);
    call_elt.add_child(summary);

    let detail = new_call_tooltip(call, call_ctx, times, scale_x, pixels_x, pixels_y, secrets);
    call_elt.add_child(detail);

    call_elt
}

/// Returns the SVG timings block of this `call`.
fn new_call_timings(
    call: &Call,
    call_ctx: &CallContext,
    times: Interval<DateTime<Utc>>,
    scale_x: Scale,
    pixels_y: Interval<Pixel>,
) -> Element {
    let mut group = svg::new_group();
    group.add_attr(Class("call-summary".to_string()));

    let offset_y = CALL_HEIGHT * call_ctx.call_index.to_zero_based() + pixels_y.start;
    let y = offset_y + CALL_INSET;
    let height = CALL_HEIGHT - CALL_INSET * 2;

    // DNS
    let dns_x = (call.timings.begin_call - times.start)
        .to_std()
        .unwrap_or_default();
    let dns_x = to_pixel(dns_x, scale_x);
    let dns_width = to_pixel(call.timings.name_lookup, scale_x);
    if dns_width.0 > 0.0 {
        let elt = svg::new_rect(dns_x.0, y.0, dns_width.0, height.0, "#1d9688");
        group.add_child(elt);
    }

    // TCP Handshake
    let tcp_x = to_pixel(call.timings.name_lookup, scale_x) + dns_x;
    let tcp_width = call.timings.connect.checked_sub(call.timings.name_lookup);
    if let Some(tcp_width) = tcp_width {
        let tcp_width = to_pixel(tcp_width, scale_x);
        if tcp_width.0 > 0.0 {
            let elt = svg::new_rect(tcp_x.0, y.0, tcp_width.0, height.0, "#fa7f03");
            group.add_child(elt);
        }
    }

    // SSL
    let ssl_x = to_pixel(call.timings.connect, scale_x) + dns_x;
    let ssl_width = call.timings.app_connect.checked_sub(call.timings.connect);
    if let Some(ssl_width) = ssl_width {
        let ssl_width = to_pixel(ssl_width, scale_x);
        if ssl_width.0 > 0.0 {
            let elt = svg::new_rect(ssl_x.0, y.0, ssl_width.0, height.0, "#9933ff");
            group.add_child(elt);
        }
    }

    // Wait
    let wait_x = to_pixel(call.timings.pre_transfer, scale_x) + dns_x;
    let wait_width = call
        .timings
        .start_transfer
        .checked_sub(call.timings.pre_transfer);
    if let Some(wait_width) = wait_width {
        let wait_width = to_pixel(wait_width, scale_x);
        if wait_width.0 > 0.0 {
            let elt = svg::new_rect(wait_x.0, y.0, wait_width.0, height.0, "#18c852");
            group.add_child(elt);
        }
    }

    // Data transfer
    let data_transfer_x = to_pixel(call.timings.start_transfer, scale_x) + dns_x;
    let data_transfer_width = call.timings.total.checked_sub(call.timings.start_transfer);
    if let Some(data_transfer_width) = data_transfer_width {
        let data_transfer_width = to_pixel(data_transfer_width, scale_x);
        if data_transfer_width.0 > 0.0 {
            let elt = svg::new_rect(
                data_transfer_x.0,
                y.0,
                data_transfer_width.0,
                height.0,
                "#36a9f4",
            );
            group.add_child(elt);
        }
    }

    group
}

/// Returns the SVG detail of this `call`.
/// Each call timings value is displayed under the timings (see [`crate::http::Timings`]).
fn new_call_tooltip(
    call: &Call,
    call_ctx: &CallContext,
    times: Interval<DateTime<Utc>>,
    scale_x: Scale,
    pixels_x: Interval<Pixel>,
    pixels_y: Interval<Pixel>,
    secrets: &[&str],
) -> Element {
    let mut group = svg::new_group();
    group.add_attr(Class("call-detail".to_string()));
    group.add_attr(FontFamily("sans-serif".to_string()));
    group.add_attr(FontSize("17px".to_string()));

    let width = 600.px();
    let height = 235.px();
    let offset_x = (call.timings.begin_call - times.start)
        .to_std()
        .unwrap_or_default();
    let offset_x = to_pixel(offset_x, scale_x);
    let offset_y = CALL_HEIGHT * call_ctx.call_index.to_zero_based() + pixels_y.start;
    let offset_y = offset_y + CALL_HEIGHT - CALL_INSET;
    let max_width = pixels_x.end - pixels_x.start;
    // We bound the tooltip background to the overall bounding box.
    let offset_x = Pixel::max(offset_x, 6.px());
    let offset_x = Pixel::min(offset_x, max_width - width - 6.px());

    let selection = new_call_sel(call, call_ctx, times, scale_x, pixels_y);
    group.add_child(selection);

    let mut x = offset_x;
    let mut y = offset_y;

    let mut elt = svg::new_rect(x.0, y.0, width.0, height.0, "white");
    elt.add_attr(Class("call-back".to_string()));
    elt.add_attr(Filter("url(#shadow)".to_string()));
    elt.add_attr(Stroke("#ccc".to_string()));
    elt.add_attr(StrokeWidth(1.0));
    group.add_child(elt);

    x += 14.px();
    y += 14.px();
    let delta_y = 30.px();

    let mut legend = svg::new_group();
    legend.add_attr(Class("call-legend".to_string()));
    legend.add_attr(Fill("#555".to_string()));

    // Icon + URL + method
    let mut elt = svg::new_use();
    let icon = match call_ctx.kind {
        CallContextKind::Success => "#success",
        CallContextKind::Failure => "#failure",
        CallContextKind::Retry => "#retry",
    };
    elt.add_attr(Href(icon.to_string()));
    elt.add_attr(X(x.0));
    elt.add_attr(Y(y.0));
    elt.add_attr(Width("20".to_string()));
    elt.add_attr(Height("20".to_string()));
    legend.add_child(elt);

    let url = call.request.url.to_string().redact(secrets);
    let text = format!("{} {}", call.request.method, url);
    let text = trunc_str(&text, 54);
    let text = format!("{text}  {}", call.response.status);
    let mut elt = svg::new_text(x.0 + 30.0, y.0 + 16.0, &text);
    if call_ctx.kind == CallContextKind::Failure {
        elt.add_attr(Fill("red".to_string()));
    }
    elt.add_attr(FontWeight("bold".to_string()));
    legend.add_child(elt);

    x += 12.px();
    y += 32.px();

    // DNS
    let duration = call.timings.name_lookup.as_micros();
    let duration = Microsecond(duration as f64);
    let elt = new_legend(x, y, "DNS lookup", Some("#1d9688"), duration);
    legend.add_child(elt);
    y += delta_y;

    // TCP handshake
    let duration = call.timings.connect.checked_sub(call.timings.name_lookup);
    if let Some(duration) = duration {
        let duration = Microsecond(duration.as_micros() as f64);
        let elt = new_legend(x, y, "TCP handshake", Some("#fa7f03"), duration);
        legend.add_child(elt);
        y += delta_y;
    }

    // SSL handshake
    let duration = call.timings.app_connect.checked_sub(call.timings.connect);
    if let Some(duration) = duration {
        let duration = Microsecond(duration.as_micros() as f64);
        let elt = new_legend(x, y, "SSL handshake", Some("#9933ff"), duration);
        legend.add_child(elt);
        y += delta_y;
    }

    // Wait
    let duration = call
        .timings
        .start_transfer
        .checked_sub(call.timings.pre_transfer);
    if let Some(duration) = duration {
        let duration = Microsecond(duration.as_micros() as f64);
        let elt = new_legend(x, y, "Wait", Some("#18c852"), duration);
        legend.add_child(elt);
        y += delta_y;
    }

    // Data transfer
    let duration = call.timings.total.checked_sub(call.timings.start_transfer);
    if let Some(duration) = duration {
        let duration = Microsecond(duration.as_micros() as f64);
        let elt = new_legend(x, y, "Data transfer", Some("#36a9f4"), duration);
        legend.add_child(elt);
        y += delta_y;
    }

    // Total
    let duration = call.timings.total.as_micros();
    let duration = Microsecond(duration as f64);
    let mut elt = new_legend(x, y, "Total", None, duration);
    elt.add_attr(FontWeight("bold".to_string()));
    legend.add_child(elt);
    y += delta_y;

    // Start and stop timestamps
    let start = (call.timings.begin_call - times.start)
        .to_std()
        .unwrap_or_default();
    let end = (call.timings.end_call - times.start)
        .to_std()
        .unwrap_or_default();
    x = offset_x + 380.px();
    y = offset_y + 64.px();
    let value = Microsecond(start.as_micros() as f64);
    let value = value.to_human_string();
    let elt = new_value("Start:", &value, x, y);
    legend.add_child(elt);
    y += delta_y;
    let value = Microsecond(end.as_micros() as f64);
    let value = value.to_human_string();
    let elt = new_value("Stop:", &value, x, y);
    legend.add_child(elt);

    y += delta_y;
    let value = Byte(call.response.body.len() as f64);
    let value = value.to_human_string();
    let elt = new_value("Transferred:", &value, x, y);
    legend.add_child(elt);

    // Run URL
    y += 56.px();
    let href = format!(
        "{}#e{}:c{}",
        call_ctx.run_filename, call_ctx.entry_index, call_ctx.call_entry_index
    );
    let elt = new_link(x, y, "(view run)", &href);
    legend.add_child(elt);

    // Source URL
    let href = format!("{}#l{}", call_ctx.source_filename, call_ctx.line);
    let elt = new_link(x + 90.px(), y, "(view source)", &href);
    legend.add_child(elt);

    // Timings explanation
    y += delta_y;
    let elt = new_link(
        x,
        y,
        "Explanation",
        "https://hurl.dev/docs/response.html#timings",
    );
    legend.add_child(elt);
    group.add_child(legend);

    group
}

fn new_link(x: Pixel, y: Pixel, text: &str, href: &str) -> Element {
    let mut elt = svg::new_text(x.0, y.0, text);
    elt.add_attr(Fill("royalblue".to_string()));
    elt.add_attr(TextDecoration("underline".to_string()));
    let mut a = new_a(href);
    a.add_child(elt);
    a
}

/// Returns the highlighted span time of a call.
fn new_call_sel(
    call: &Call,
    call_ctx: &CallContext,
    times: Interval<DateTime<Utc>>,
    scale_x: Scale,
    pixels_y: Interval<Pixel>,
) -> Element {
    let offset_x_start = (call.timings.begin_call - times.start)
        .to_std()
        .unwrap_or_default();
    let offset_x_start = to_pixel(offset_x_start, scale_x);
    let offset_x_end = (call.timings.end_call - times.start)
        .to_std()
        .unwrap_or_default();
    let offset_x_end = to_pixel(offset_x_end, scale_x);
    let color = match call_ctx.kind {
        CallContextKind::Success | CallContextKind::Retry => "green",
        CallContextKind::Failure => "red",
    };
    let mut elt = svg::new_rect(
        offset_x_start.0,
        0.0,
        (offset_x_end - offset_x_start).0,
        pixels_y.end.0,
        color,
    );
    elt.add_attr(Opacity(0.05));
    elt.add_attr(Class("call-sel".to_string()));
    elt
}

fn new_legend(
    x: Pixel,
    y: Pixel,
    text: &str,
    color: Option<&str>,
    duration: Microsecond,
) -> Element {
    let dx_label = 36.px();
    let dy_label = 17.px();
    let dx_duration = 180.px();

    let mut group = svg::new_group();

    if let Some(color) = color {
        let color_elt = svg::new_rect(x.0, y.0, 20.0, 20.0, color);
        group.add_child(color_elt);
    }

    let text_elt = svg::new_text((x + dx_label).0, (y + dy_label).0, text);
    group.add_child(text_elt);

    let duration = duration.to_human_string();
    let duration_elt = svg::new_text((x + dx_duration).0, (y + dy_label).0, &duration);
    group.add_child(duration_elt);

    group
}

fn new_value(label: &str, value: &str, x: Pixel, y: Pixel) -> Element {
    let mut group = svg::new_group();
    let elt = svg::new_text(x.0, y.0, label);
    group.add_child(elt);

    let x = x + 100.px();
    let elt = svg::new_text(x.0, y.0, value);
    group.add_child(elt);

    group
}

/// Converts a `duration` to pixel, using `scale_x`.
fn to_pixel(duration: Duration, scale_x: Scale) -> Pixel {
    let value = duration.as_micros();
    let value = Microsecond(value as f64);
    scale_x.to_pixel(value)
}

/// Creates SVG filters for the waterfall (used by drop shadow of call tooltip).
fn new_filters() -> Element {
    let mut defs = svg::new_defs();

    let mut filter = svg::new_filter();
    filter.add_attr(Id("shadow".to_string()));

    let mut shadow = svg::new_fe_drop_shadow();
    shadow.add_attr(DX(0.0));
    shadow.add_attr(DY(4.0));
    shadow.add_attr(StdDeviation(4.0));
    shadow.add_attr(FloodOpacity(0.25));
    filter.add_child(shadow);
    defs.add_child(filter);
    defs
}

impl Microsecond {
    /// Returns a human readable string of a microsecond.
    fn to_human_string(self) -> String {
        match self.0 {
            d if d < 0.0 => "_".to_string(),
            d if d < 1_000.0 => format!("{d:.1} µs"),
            d if d < 1_000_000.0 => format!("{:.1} ms", d / 1_000.0),
            d => format!("{:.1} s", d / 1_000_000.0),
        }
    }
}

impl Byte {
    /// Returns a human readable string of a byte.
    fn to_human_string(self) -> String {
        match self.0 {
            d if d < 0.0 => "_".to_string(),
            d if d < 1_000.0 => format!("{d:.1} B"),
            d if d < 1_000_000.0 => format!("{:.1} kB", d / 1_000.0),
            d => format!("{:.1} MB", d / 1_000_000.0),
        }
    }
}

#[cfg(test)]
mod tests {
    use chrono::{Duration, TimeZone, Utc};

    use super::*;
    use crate::report::html::timeline::unit::{Interval, Microsecond, Scale};

    #[test]
    fn legend_svg() {
        let x = 20.px();
        let y = 30.px();
        let text = "Hello world";
        let color = "red";
        let duration = Microsecond(2000.0);

        let elt = new_legend(x, y, text, Some(color), duration);
        assert_eq!(
            elt.to_string(),
            "<g>\
                <rect x=\"20\" y=\"30\" width=\"20\" height=\"20\" fill=\"red\" />\
                <text x=\"56\" y=\"47\">Hello world</text>\
                <text x=\"200\" y=\"47\">2.0 ms</text>\
            </g>"
        );
    }

    #[test]
    fn grid_vert_lines_svg() {
        let start = Utc.with_ymd_and_hms(2022, 1, 1, 8, 0, 0).unwrap();
        let end = start + Duration::try_seconds(1).unwrap();
        let times = Interval { start, end };
        let start = 0.px();
        let end = 1000.px();
        let pixels_x = Interval { start, end };
        let start = 0.px();
        let end = 100.px();
        let pixels_y = Interval { start, end };
        let scale_x = Scale::new(times, pixels_x);
        let ticks_number = 10;

        let elt = new_vert_lines(times, ticks_number, scale_x, pixels_y);
        assert_eq!(
            elt.to_string(),
            "<g>\
                <g class=\"grid-ticks\" stroke=\"#ccc\">\
                    <line x1=\"100\" y1=\"0\" x2=\"100\" y2=\"100\" />\
                    <line x1=\"200\" y1=\"0\" x2=\"200\" y2=\"100\" />\
                    <line x1=\"300\" y1=\"0\" x2=\"300\" y2=\"100\" />\
                    <line x1=\"400\" y1=\"0\" x2=\"400\" y2=\"100\" />\
                    <line x1=\"500\" y1=\"0\" x2=\"500\" y2=\"100\" />\
                    <line x1=\"600\" y1=\"0\" x2=\"600\" y2=\"100\" />\
                    <line x1=\"700\" y1=\"0\" x2=\"700\" y2=\"100\" />\
                    <line x1=\"800\" y1=\"0\" x2=\"800\" y2=\"100\" />\
                    <line x1=\"900\" y1=\"0\" x2=\"900\" y2=\"100\" />\
                </g>\
                <g font-size=\"15px\" font-family=\"sans-serif\" fill=\"#777\">\
                    <text x=\"5\" y=\"20\">0 ms</text>\
                    <text x=\"105\" y=\"20\">100 ms</text>\
                    <text x=\"205\" y=\"20\">200 ms</text>\
                    <text x=\"305\" y=\"20\">300 ms</text>\
                    <text x=\"405\" y=\"20\">400 ms</text>\
                    <text x=\"505\" y=\"20\">500 ms</text>\
                    <text x=\"605\" y=\"20\">600 ms</text>\
                    <text x=\"705\" y=\"20\">700 ms</text>\
                    <text x=\"805\" y=\"20\">800 ms</text>\
                    <text x=\"905\" y=\"20\">900 ms</text>\
                </g>\
            </g>"
        );
    }
}