hen 0.14.0

Run API collections from the command line.
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
mod support;

use std::{
    io::{Read, Write},
    net::TcpListener,
    process::{Command, Stdio},
    sync::mpsc,
    thread,
    time::Duration,
};

use serde_json::Value;
use support::TestWorkspace;

#[test]
fn run_outputs_json_report() {
    let server_url = spawn_http_server(
        200,
        "OK",
        "application/json",
        r#"{"ok":true,"service":"hen"}"#,
    );
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "collection.hen",
        &format!(
            r#"JSON Fixture

Exercises machine-readable output.

---

Fetch fixture

GET {server_url}

^ & body.ok == true
[ true == false ] ^ & body.service == 'hen'
"#
        ),
    );

    let output = workspace.run_hen(["run", "collection.hen", "--output", "json"]);

    assert_eq!(output.status_code, 0, "stderr: {}", output.stderr);
    assert!(output.stderr.is_empty(), "stderr: {}", output.stderr);

    let parsed: Value = serde_json::from_str(&output.stdout).expect("stdout should be valid json");
    assert_eq!(parsed["plan"], serde_json::json!([0]));
    assert_eq!(parsed["selectedRequests"], serde_json::json!([0]));
    assert_eq!(parsed["records"][0]["status"], 200);
    assert_eq!(
        parsed["records"][0]["assertions"],
        serde_json::json!([
            {
                "assertion": "^ & body.ok == true",
                "status": "passed",
                "message": null,
            },
            {
                "assertion": "[ true == false ] ^ & body.service == 'hen'",
                "status": "skipped",
                "message": "guard evaluated to false",
            }
        ])
    );
    assert!(parsed["records"][0]["body"]
        .as_str()
        .expect("body should be serialized as a string")
        .contains("\"service\":\"hen\""));
    assert_eq!(parsed["records"][0]["bodyChars"], 27);
    assert_eq!(parsed["records"][0]["bodyTruncated"], false);
    assert_eq!(parsed["interrupted"], false);
    assert_eq!(parsed["interruptSignal"], Value::Null);
    assert_eq!(parsed["failures"], serde_json::json!([]));
}

#[test]
fn run_outputs_ndjson_report() {
    let server_url = spawn_http_server(
        200,
        "OK",
        "application/json",
        r#"{"ok":true,"service":"hen"}"#,
    );
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "collection.hen",
        &format!(
            r#"NDJSON Fixture

Exercises streaming machine-readable output.

---

Fetch fixture

GET {server_url}

^ & body.ok == true
[ true == false ] ^ & body.service == 'hen'
"#
        ),
    );

    let output = workspace.run_hen(["run", "collection.hen", "--output", "ndjson"]);

    assert_eq!(output.status_code, 0, "stderr: {}", output.stderr);
    assert!(output.stderr.is_empty(), "stderr: {}", output.stderr);

    let lines = output
        .stdout
        .lines()
        .map(|line| serde_json::from_str::<Value>(line).expect("line should be valid json"))
        .collect::<Vec<_>>();

    assert_eq!(lines.len(), 2);
    assert_eq!(lines[0]["type"], "run");
    assert_eq!(lines[0]["interrupted"], false);
    assert_eq!(lines[0]["interruptSignal"], Value::Null);
    assert_eq!(lines[1]["type"], "record");
    assert_eq!(lines[1]["status"], 200);
    assert_eq!(lines[1]["assertions"][0]["status"], "passed");
    assert_eq!(lines[1]["assertions"][1]["status"], "skipped");
}

#[test]
fn run_outputs_json_report_for_failed_assertions() {
    let server_url = spawn_http_server(
        200,
        "OK",
        "application/json",
        r#"{"ok":false,"service":"hen"}"#,
    );
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "collection.hen",
        &format!(
            r#"Failed Assertions Fixture

Exercises assertion-level failure reporting.

---

Fetch fixture

GET {server_url}

^ & body.service == 'hen'
[ true == false ] ^ & body.service == 'hen'
^ & body.ok == true
"#
        ),
    );

    let output = workspace.run_hen(["run", "collection.hen", "--output", "json"]);

    assert_eq!(output.status_code, 1, "stderr: {}", output.stderr);
    assert!(output.stderr.is_empty(), "stderr: {}", output.stderr);

    let parsed: Value = serde_json::from_str(&output.stdout).expect("stdout should be valid json");
    assert_eq!(parsed["executionFailed"], true);
    assert_eq!(parsed["interrupted"], false);
    assert_eq!(parsed["interruptSignal"], Value::Null);
    assert_eq!(parsed["records"], serde_json::json!([]));
    assert_eq!(
        parsed["failures"][0]["assertions"],
        serde_json::json!([
            {
                "assertion": "^ & body.service == 'hen'",
                "status": "passed",
                "message": null,
            },
            {
                "assertion": "[ true == false ] ^ & body.service == 'hen'",
                "status": "skipped",
                "message": "guard evaluated to false",
            },
            {
                "assertion": "^ & body.ok == true",
                "status": "failed",
                "message": "Assertion failed: ^ & body.ok == true (actual: 'false')",
            }
        ])
    );
}

#[test]
fn run_outputs_junit_report_for_assertion_failures() {
    let server_url = spawn_http_server(
        200,
        "OK",
        "application/json",
        r#"{"ok":false,"service":"hen"}"#,
    );
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "collection.hen",
        &format!(
            r#"JUnit Fixture

Exercises CI-oriented test reporting.

---

Fail fixture

GET {server_url}

[ true == false ] ^ & body.service == 'hen'
^ & body.ok == true
"#
        ),
    );

    let output = workspace.run_hen(["run", "collection.hen", "--output", "junit"]);

    assert_eq!(output.status_code, 1, "stderr: {}", output.stderr);
    assert!(output.stderr.is_empty(), "stderr: {}", output.stderr);
    assert!(
        output.stdout.contains("<testsuite"),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains("<testcase"),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains("tests=\"2\""),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains("<failure type=\"assertion\""),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains("Assertion failed"),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains("<skipped message=\"guard evaluated to false\"/>"),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains("#0 GET") && output.stdout.contains(":: ^ &amp; body.ok == true"),
        "stdout: {}",
        output.stdout
    );
}

#[test]
fn run_outputs_junit_report_for_passed_and_skipped_assertions() {
    let server_url = spawn_http_server(
        200,
        "OK",
        "application/json",
        r#"{"ok":true,"service":"hen"}"#,
    );
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "collection.hen",
        &format!(
            r#"JUnit Success Fixture

Exercises assertion-level success reporting.

---

Fetch fixture

GET {server_url}

^ & body.ok == true
[ true == false ] ^ & body.service == 'hen'
"#
        ),
    );

    let output = workspace.run_hen(["run", "collection.hen", "--output", "junit"]);

    assert_eq!(output.status_code, 0, "stderr: {}", output.stderr);
    assert!(output.stderr.is_empty(), "stderr: {}", output.stderr);
    assert!(output.stdout.contains("tests=\"2\""), "stdout: {}", output.stdout);
    assert!(
        output.stdout.contains(":: ^ &amp; body.ok == true"),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains(":: [ true == false ] ^ &amp; body.service == &apos;hen&apos;"),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains("<skipped message=\"guard evaluated to false\"/>"),
        "stdout: {}",
        output.stdout
    );
}

#[test]
fn run_outputs_junit_report_for_dependency_capture_assertions() {
    let user_server_url = spawn_http_server(
        200,
        "OK",
        "application/json",
        r#"{"id":1,"email":"user@example.com","address":{"zipcode":"90210"}}"#,
    );
    let posts_server_url = spawn_http_server(
        200,
        "OK",
        "application/json",
        r#"[{"userId":1,"id":10,"title":"hello world"}]"#,
    );
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "collection.hen",
        &format!(
            r#"name = JUnit Dependency Capture Fixture
description = Exercises dependency captures followed by assertions.

---

Get User

GET {user_server_url}

& body.id -> $USER_ID
^ & status == 200

---

Get Posts

> requires: Get User

GET {posts_server_url}

& body.[0].title -> $FIRST_POST_TITLE
&[Get User].body.email -> $USER_EMAIL
&[Get User].body.address.zipcode -> $USER_ZIPCODE

^ & status == 200
^ & body.[0].userId == $USER_ID
^ $FIRST_POST_TITLE != ''
^ $USER_EMAIL ~= /@/
^ $USER_ZIPCODE == '90210'
"#
        ),
    );

    let output = workspace.run_hen(["run", "collection.hen", "all", "--output", "junit"]);

    assert_eq!(output.status_code, 0, "stderr: {}", output.stderr);
    assert!(output.stderr.is_empty(), "stderr: {}", output.stderr);
    assert!(output.stdout.contains("tests=\"6\""), "stdout: {}", output.stdout);
    assert!(
        output
            .stdout
            .contains("#1 GET")
            && output.stdout.contains(":: ^ $USER_EMAIL ~= /@/"),
        "stdout: {}",
        output.stdout
    );
    assert!(
        output.stdout.contains(":: ^ $USER_ZIPCODE == &apos;90210&apos;"),
        "stdout: {}",
        output.stdout
    );
}

#[cfg(unix)]
#[test]
fn run_outputs_partial_text_summary_when_interrupted_by_sigint() {
    let fast_server_url = spawn_http_server(200, "OK", "application/json", r#"{"step":1}"#);
    let (started_tx, started_rx) = mpsc::channel();
    let slow_server_url = spawn_blocking_http_server(started_tx);
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "collection.hen",
        &format!(
            r#"Interrupt Fixture

Exercises partial text summaries on SIGINT.

---

First request

GET {fast_server_url}

---

Second request

GET {slow_server_url}
"#
        ),
    );

    let output = run_until_signal(
        &workspace,
        &["run", "collection.hen", "all", "--parallel"],
        "-INT",
        started_rx,
    );
    let stdout = String::from_utf8(output.stdout).expect("stdout should be utf-8");
    let stderr = String::from_utf8(output.stderr).expect("stderr should be utf-8");

    assert_eq!(output.status.code(), Some(130), "stdout: {stdout}\nstderr: {stderr}");
    assert!(stdout.contains("[ok] #0"), "stdout: {stdout}");
    assert!(stdout.contains("Summary"), "stdout: {stdout}");
    assert!(stdout.contains("interrupted by SIGINT"), "stdout: {stdout}");
    assert!(!stdout.contains("[ok] #1"), "stdout: {stdout}");
    assert!(stderr.contains("Execution interrupted by SIGINT"), "stderr: {stderr}");
}

#[cfg(unix)]
#[test]
fn run_outputs_partial_json_report_when_interrupted_by_sigterm() {
    let fast_server_url = spawn_http_server(200, "OK", "application/json", r#"{"step":1}"#);
    let (started_tx, started_rx) = mpsc::channel();
    let slow_server_url = spawn_blocking_http_server(started_tx);
    let workspace = TestWorkspace::new();
    workspace.write_file(
        "collection.hen",
        &format!(
            r#"Interrupt JSON Fixture

Exercises partial machine-readable summaries on SIGTERM.

---

First request

GET {fast_server_url}

---

Second request

GET {slow_server_url}
"#
        ),
    );

    let output = run_until_signal(
        &workspace,
        &["run", "collection.hen", "all", "--parallel", "--output", "json"],
        "-TERM",
        started_rx,
    );
    let stderr = String::from_utf8(output.stderr).expect("stderr should be utf-8");

    assert_eq!(output.status.code(), Some(143), "stderr: {stderr}");
    assert!(stderr.is_empty(), "stderr: {stderr}");

    let stdout = String::from_utf8(output.stdout).expect("stdout should be utf-8");
    let parsed: Value = serde_json::from_str(&stdout).expect("stdout should be valid json");
    assert_eq!(parsed["executionFailed"], true);
    assert_eq!(parsed["interrupted"], true);
    assert_eq!(parsed["interruptSignal"], "SIGTERM");
    assert_eq!(parsed["records"].as_array().map(Vec::len), Some(1));
    assert_eq!(parsed["records"][0]["status"], 200);
    assert_eq!(parsed["records"][0]["url"], fast_server_url);
    assert_eq!(parsed["failures"], serde_json::json!([]));
}

fn spawn_http_server(status: u16, reason: &str, content_type: &str, body: &str) -> String {
    let listener = TcpListener::bind("127.0.0.1:0").expect("listener should bind");
    let address = listener.local_addr().expect("address should be available");
    let reason = reason.to_string();
    let content_type = content_type.to_string();
    let body = body.to_string();

    thread::spawn(move || {
        let (mut stream, _) = listener.accept().expect("connection should be accepted");
        let mut buffer = [0_u8; 1024];
        let _ = stream.read(&mut buffer);
        let response = format!(
            "HTTP/1.1 {status} {reason}\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
            body.len()
        );
        stream
            .write_all(response.as_bytes())
            .expect("response should be written");
    });

    format!("http://{}", address)
}

#[cfg(unix)]
fn spawn_blocking_http_server(started_tx: mpsc::Sender<()>) -> String {
    let listener = TcpListener::bind("127.0.0.1:0").expect("listener should bind");
    let address = listener.local_addr().expect("address should be available");

    thread::spawn(move || {
        let (mut stream, _) = listener.accept().expect("connection should be accepted");
        let mut buffer = [0_u8; 1024];
        let _ = stream.read(&mut buffer);
        started_tx
            .send(())
            .expect("test should observe the blocking request");
        thread::sleep(Duration::from_secs(5));

        let body = r#"{"ok":true}"#;
        let response = format!(
            "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
            body.len()
        );
        let _ = stream.write_all(response.as_bytes());
    });

    format!("http://{}", address)
}

#[cfg(unix)]
fn run_until_signal(
    workspace: &TestWorkspace,
    args: &[&str],
    signal: &str,
    started_rx: mpsc::Receiver<()>,
) -> std::process::Output {
    let child = Command::new(env!("CARGO_BIN_EXE_hen"))
        .args(args)
        .current_dir(workspace.root())
        .env("NO_COLOR", "1")
        .env("TERM", "dumb")
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("hen command should execute");

    if started_rx.recv_timeout(Duration::from_secs(5)).is_err() {
        let output = child
            .wait_with_output()
            .expect("child output should be available");
        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);
        panic!(
            "second request should start before signalling\nstatus: {:?}\nstdout: {}\nstderr: {}",
            output.status.code(),
            stdout,
            stderr,
        );
    }

    thread::sleep(Duration::from_millis(200));

    let status = Command::new("kill")
        .args([signal, &child.id().to_string()])
        .status()
        .expect("signal command should execute");
    assert!(status.success(), "failed to send signal {signal}");

    child
        .wait_with_output()
        .expect("child output should be available")
}