gload 0.5.1

A command line client for the Gemini protocol.
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
//! This software is licensed as described in the file LICENSE, which
//! you should have received as part of this distribution.
//!
//! You may opt to use, copy, modify, merge, publish, distribute and/or sell
//! copies of the Software, and permit persons to whom the Software is
//! furnished to do so, under the terms of the LICENSE file.
//!
//! This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
//! KIND, either express or implied.
//!
//! SPDX-License-Identifier: BSD-3-Clause

#![cfg(feature = "logging")]

mod error;
mod logger;

#[cfg(test)]
mod test_server;

use crate::error::ExitReason;
use ::clap::{Parser, ValueHint};
use ::gload::{
	net::{
		CancellationToken, FetchOptions, IpResolutionLimit, REDIR_LIMIT, RedirectPolicy,
		ResponseHandler,
	},
	request::Request,
	response::{StatusCategory, StatusCode},
};
use ::log::{debug, info, warn};
use std::{io::Write, path::PathBuf};

#[cfg(not(tarpaulin_include))]
#[tokio::main(flavor = "current_thread")]
async fn main() -> ExitReason {
	let args = Args::parse();
	logger::init(args.verbose);

	// If requested, print the changelog and exit
	if args.info.changelog {
		let changelog = include_str!("../CHANGELOG.gmi");
		info!("{changelog}");
		return ExitReason::Success;
	}

	// If requested, print code license and exit
	if args.info.license {
		let license = include_str!("../LICENSE");
		info!("{license}");
		return ExitReason::Success;
	}

	run(args).await
}

async fn run(args: Args) -> ExitReason {
	// validity checks for URI
	let absolute_uri = args.url.as_ref().expect("clap ensures url is given");
	let req = match Request::from_uri_string(absolute_uri) {
		Err(err) => return err.into(),
		Ok(req) => req,
	};

	let redirect_policy = if args.location {
		match args.max_redirs {
			Some(max) => RedirectPolicy::follow_only(max),
			None => RedirectPolicy::follow(),
		}
	} else {
		RedirectPolicy::no_follow()
	};

	let address_resolution_limit = args
		.ip_resolution_limit
		.as_ref()
		.map(IpResolutionLimit::from);

	let cancellation_token = CancellationToken::new();

	// Cancel when an appropriate signal arrives
	let token = cancellation_token.clone();
	tokio::spawn(async move {
		use tokio::signal;

		#[cfg(unix)]
		{
			let mut sigint = signal::unix::signal(signal::unix::SignalKind::interrupt()).unwrap();
			let mut sigterm = signal::unix::signal(signal::unix::SignalKind::terminate()).unwrap();
			tokio::select! {
				_ = sigint.recv() => {},
				_ = sigterm.recv() => {},
			}
		}

		#[cfg(windows)]
		{
			let mut ctrl_break = signal::windows::ctrl_break().unwrap();
			let mut ctrl_c = signal::windows::ctrl_c().unwrap();
			let mut ctrl_close = signal::windows::ctrl_close().unwrap();
			let mut ctrl_logoff = signal::windows::ctrl_logoff().unwrap();
			let mut ctrl_shutdown = signal::windows::ctrl_shutdown().unwrap();
			tokio::select! {
				_ = ctrl_break.recv() => {},
				_ = ctrl_c.recv() => {},
				_ = ctrl_close.recv() => {},
				_ = ctrl_logoff.recv() => {},
				_ = ctrl_shutdown.recv() => {},
			}
		}

		// If any of the above return, then we got a kill signal and should cancel.
		#[cfg(any(unix, windows))]
		token.cancel();

		// Platforms that are neither Windows nor Unix should continue; we'll add proper handlers as they come up.
	});

	let options = FetchOptions {
		address_resolution_limit,
		allow_truncation: args.tls_allow_truncation,
		redirect_policy,
		response_handler: PrintingHandler(&args),
		cancellation_token,
	};

	let response = match gload::fetch(req, options).await {
		Err(err) => return err.into(),
		Ok(r) => r,
	};

	// Exit early if `--fail` is set and the server's response is errorful
	if args.fail && response.status().is_failure() {
		return ExitReason::ServerErrorResponse(response.status());
	}

	// Already printed header info, so we can quit here
	if args.head {
		return ExitReason::Success;
	}

	match response.status().category() {
		StatusCategory::InputExpected => ExitReason::InputExpected,

		StatusCategory::Success
		| StatusCategory::TemporaryFailure
		| StatusCategory::PermanentFailure
		| StatusCategory::Redirection => ExitReason::Success, // handler already printed the body

		StatusCategory::ClientCertificates => {
			if response.status() == StatusCode::CERTIFICATE_NOT_VALID {
				ExitReason::ClientCertificateNotValid
			} else {
				ExitReason::ClientCertificateRequired
			}
		}
	}

	// TODO: What to do if server wants input (status 1x)? Interactive input? stdin before run? --data param like curl?
	// interactive mode feels more like a browser thing tbh, so we should probs leave that to other implementers. Worth an example impl tho.
}

struct PrintingHandler<'a>(&'a Args);

impl core::fmt::Debug for PrintingHandler<'_> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "PrintingHandler")
	}
}

impl ResponseHandler for PrintingHandler<'_> {
	type Error = PrintingError;

	fn handle(&self, res: &gload::Response) -> Result<(), Self::Error> {
		let args = self.0;
		let print_header = args.head || args.show_header;
		let print_body = !args.head;

		let status = res.status().as_u8();
		match res.meta() {
			None => debug!("< {status}"),
			Some(meta) => debug!("< {status} {meta}"),
		}
		debug!("<");

		if args.out_null {
			// Print nothing to stdout
			return Ok(());
		}

		if print_header {
			match res.meta() {
				None => info!("{status}"),
				Some(meta) => info!("{status} {meta}"),
			}
		}

		if print_body && let Some(body_bytes) = res.body() {
			if let Some(output) = &args.output {
				if output.to_string_lossy() == "-" {
					// Send to stdout anyway; the user is likely piping to something that can handle it
					if std::io::stdout().write_all(body_bytes).is_err() {
						return Err(PrintingError::Stdout(body_bytes.len()));
					}
				} else {
					// Write to file
					// TODO: no-clobber option
					if let Err(err) = std::fs::write(output, body_bytes) {
						warn!(
							"Warning: Failed to open the file {}: {err}",
							output.display()
						);
						return Err(PrintingError::File(body_bytes.len()));
					}
				}
			} else {
				// Stringify; don't let binary data do weird stuff to stdout
				match str::from_utf8(body_bytes) {
					Err(_) => {
						warn!(
							r#"Warning: Binary output can mess up your terminal. Use "--output <FILE>" to save to a file or consider "--output -" to tell {} to output to your terminal anyway."#,
							clap::crate_name!()
						);
						return Err(PrintingError::BinaryOutput);
					}
					Ok(body) => print!("{body}"),
				}
			}
		}

		Ok(())
	}
}

#[derive(Debug)]
enum PrintingError {
	/// Could not parse data as a string for printing to stdout.
	BinaryOutput,

	/// Failed to write data to file.
	File(usize),

	/// Failed to write data to stdout.
	Stdout(usize),
}

impl core::fmt::Display for PrintingError {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			Self::BinaryOutput => {
				write!(f, "could not parse data as a string for printing to stdout")
			}
			Self::File(size) => write!(f, "failed to write {size} bytes to file"),
			Self::Stdout(size) => write!(f, "failed to write {size} bytes to stdout"),
		}
	}
}

impl core::error::Error for PrintingError {}

static FULL_VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/full_version.txt"));

/// transfer a gemini:// URL
#[derive(Parser)]
#[cfg_attr(test, derive(Default))]
#[command(version = FULL_VERSION, about)]
struct Args {
	/// Fail fast with no output on Gemini errors.
	///
	/// If the server response has a status of 40 or greater, then gload will
	/// exit with code 22 and print nothing to stdout by default. If `-i`, `-I`,
	/// `--head`, or `--show-header` are set, then headers will be printed.
	#[arg(short, long)]
	fail: bool,

	/// Print the header only.
	///
	/// Unlike HTTP, the Gemini protocol doesn't have a separate request method to
	/// fetch only headers, so the whole response is still read into memory.
	/// The only difference is that the response body is ignored while the header
	/// is sent to stdout.
	#[arg(short = 'I', long)]
	head: bool,

	#[clap(flatten)]
	ip_resolution_limit: Option<IpResolutionLimitArgs>,

	/// Follow up to five (5) redirects.
	///
	/// This option is named after HTTP's `Location` header, which serves as that
	/// protocol's way of naming the redirect target. In Gemini, the target is named in
	/// the response header's "meta" area.
	///
	/// Temporary redirects (status 30) and Permanent redirects (status 31) are treated
	/// the same to gload. Redirects are not cached.
	///
	/// Limit the amount of redirects to follow by using the `--max-redirs` option.
	#[arg(short = 'L', long)]
	location: bool,

	/// Limits the amount of redirects to follow, up to five (5).
	#[arg(long, value_parser = parse_max_redirs)]
	max_redirs: Option<u8>, // Spec: "Client MUST limit the number of redirections they follow to 5 redirections."

	/// Write output to the given file instead of stdout.
	///
	/// Specify "-" as the filename to force the output to stdout, to override gload's
	/// internal binary output in terminal prevention.
	#[arg(short = 'o', long, conflicts_with = "out_null")]
	output: Option<PathBuf>,

	/// Discard response data into the void.
	///
	/// Functionally equivalent to `--output /dev/null`, or `--output nul` on Windows,
	/// but more portable and ostensibly faster, because no actual writes take place.
	#[arg(long, conflicts_with = "output")]
	out_null: bool,

	/// Show the response header in output.
	///
	/// Has no effect when `-i` or `--head` is used.
	#[arg(short = 'i', long)]
	show_header: bool,

	/// (TLS)  Do not throw an error if the server fails to send `close_notify`.
	///
	/// It is possible for an outside attacker to cause the TLS connection to break early, so it's
	/// important for clients to know how much message to expect from a server. Because the Gemini
	/// network protocol has no inherent way for clients to know how long a message will be before
	/// it is received, servers must send the TLS `close_notify` alert before closing the connection.
	///
	/// Enabling this option will print a warning to stderr if the server neglects `close_notify`.
	///
	/// __WARNING__: this option loosens TLS security. By using this flag, you ask for exactly that.
	#[arg(long)]
	tls_allow_truncation: bool,

	/// Prints extra details during the operation.
	///
	/// Providing --verbose or -v multiple times has no extra effect.
	#[arg(short, long)]
	verbose: bool,

	#[clap(required_unless_present_any(["changelog", "license"]), value_hint = ValueHint::Url)]
	url: Option<String>,

	#[clap(flatten)]
	info: InfoArgs,
}

fn parse_max_redirs(value: &str) -> Result<u8, String> {
	let max_redirs: u8 = value.parse().map_err(|e| format!("{e}"))?;
	if max_redirs > REDIR_LIMIT {
		Err(format!(
			"{max_redirs} is greater than the limit of {REDIR_LIMIT}"
		))
	} else {
		Ok(max_redirs)
	}
}

#[derive(clap::Args)]
#[group(multiple = false)]
struct IpResolutionLimitArgs {
	/// Only use IPv4 addresses when resolving hostnames.
	///
	/// When set, IPv6 will not be tried. If only IPv6 addresses
	/// are found for the hostname, the request will fail.
	///
	/// This option is mutually exclusive with `-6`/`--ipv6`.
	#[arg(short = '4', long)]
	ipv4: bool,

	/// Only use IPv6 addresses when resolving hostnames.
	///
	/// When set, IPv4 will not be tried. If only IPv4 addresses
	/// are found for the hostname, the request will fail.
	///
	/// This option is mutually exclusive with `-4`/`--ipv4`.
	#[arg(short = '6', long)]
	ipv6: bool,
}

impl From<&IpResolutionLimitArgs> for IpResolutionLimit {
	fn from(value: &IpResolutionLimitArgs) -> Self {
		if value.ipv4 {
			Self::Ipv4
		} else if value.ipv6 {
			Self::Ipv6
		} else {
			unreachable!("clap ensures exactly one of these is true")
		}
	}
}

#[derive(clap::Args)]
#[cfg_attr(test, derive(Default))]
#[group(multiple = false)]
struct InfoArgs {
	/// Print the changelog and exit.
	#[arg(long)]
	changelog: bool,

	/// Print the source code license and exit.
	#[arg(long)]
	license: bool,
}

// MARK: - Tests

#[cfg(test)]
mod tests {
	use super::*;
	use crate::test_server::{
		new_simple_runtime, start_friendly_server, start_redir_server,
		start_unfriendly_server_no_close_notify, start_unfriendly_server_slow,
	};
	use assert_cmd::cargo::CommandCargoExt;
	use clap::crate_name;
	use core::time::Duration;
	use signal_child::Signalable;
	use std::process::Command;

	#[tokio::test]
	async fn test_quits_for_malformed_url() {
		let cases = ["ge:// f d"];
		for url in cases {
			let args = Args::parse_from([crate_name!(), url]);
			let res = run(args).await;
			assert!(matches!(res, ExitReason::UrlMalformed));
		}
	}

	#[tokio::test]
	async fn test_quits_for_non_gemini_protocol() {
		let cases = [
			("foo://bar", "foo"),
			("Foo://bar", "foo"),
			("bar://", "bar"),
			("nope:", "nope"),
			("ReallyNo:", "reallyno"),
		];
		for (url, proto) in cases {
			let args = Args::parse_from([crate_name!(), url]);
			let res = run(args).await;
			assert!(matches!(res, ExitReason::UnsupportedProtocol(p) if p == proto));
		}
	}

	#[tokio::test]
	async fn test_quits_for_userinfo_url() {
		let cases = [
			"gemini://foo:bar@localhost",
			"gemini://foo:@localhost",
			"gemini://:bar@localhost",
		];
		for url in cases {
			let args = Args::parse_from([crate_name!(), url]);
			let res = run(args).await;
			assert!(matches!(res, ExitReason::UrlMalformed));
		}
	}

	#[test]
	fn test_default_behavior() {
		let key = rcgen::generate_simple_self_signed(["localhost".into()]).unwrap();
		let server = start_friendly_server(key);
		let addr = server.addr();

		let runtime = new_simple_runtime("test-client-runtime-worker");
		let uri = format!("gemini://localhost:{}/", addr.port());
		let args = Args::parse_from([crate_name!(), &uri]);
		let res = runtime.block_on(run(args)); // TODO: Somehow assert stdout
		assert!(matches!(res, ExitReason::Success));
		assert_eq!(server.request_count(), 1);
	}

	#[test]
	fn test_default_redir_behavior() {
		let server = start_redir_server("/");
		let addr = server.addr();

		let runtime = new_simple_runtime("test-client-runtime-worker");
		let uri = format!("gemini://localhost:{}/", addr.port());
		let args = Args::parse_from([crate_name!(), &uri, "-I"]);
		let res = runtime.block_on(run(args));
		assert!(matches!(res, ExitReason::Success), "{uri}: {res:?}"); // fails if redirects get followed
		assert_eq!(server.request_count(), 1);
	}

	#[test]
	fn test_default_behavior_on_redir() {
		// Server redirs to root path forever
		let server = start_redir_server("/now-you-see-me");
		let addr = server.addr();

		let runtime = new_simple_runtime("test-client-runtime-worker");
		let uri = format!("gemini://localhost:{}/", addr.port());
		let args = Args::parse_from([crate_name!(), &uri, "-IL"]);
		let res = runtime.block_on(run(args));
		assert!(matches!(res, ExitReason::Success), "{uri}: {res:?}"); // TODO: Pass in a handle to stdout and stderr and assert their contents (this case should print "30 /now-you-see-me\r\n")
		assert_eq!(server.request_count(), 2);
	}

	#[test]
	fn test_redirects_at_default_limit() {
		// Server redirs to root path forever
		let server = start_redir_server("/");
		let addr = server.addr();

		// Should fail, since we only permit up to 5 redirs
		let runtime = new_simple_runtime("test-client-runtime-worker");
		let uri = format!("gemini://localhost:{}/", addr.port());
		let args = Args::parse_from([crate_name!(), &uri, "-L"]);
		let res = runtime.block_on(run(args));
		assert!(
			matches!(res, ExitReason::TooManyRedirects { max: 5 }), // Spec: "Client MUST limit the number of redirections they follow to 5 redirections."
			"{uri}: {res:?}"
		);
		assert_eq!(server.request_count(), 6);
	}

	#[test]
	fn test_redirects_at_limit() {
		for limit in 0..=5 {
			// Server redirs to root path forever
			let server = start_redir_server("/");
			let addr = server.addr();

			// Spec: "Client MUST limit the number of redirections they follow to 5 redirections."
			// Should always fail, since we only permit 0-5 redirs
			let runtime = new_simple_runtime("test-client-runtime-worker");
			let uri = format!("gemini://localhost:{}/", addr.port());
			let max = &limit.to_string();
			let args = Args::parse_from([crate_name!(), &uri, "-L", "--max-redirs", max]);
			let res = runtime.block_on(run(args));
			assert!(
				matches!(res, ExitReason::TooManyRedirects { max } if max == limit),
				"{uri}: {res:?}"
			);
			assert_eq!(server.request_count(), limit + 1);
		}
	}

	#[test]
	fn test_redirects_to_not_found_page() {
		// Server redirs once to a code 51
		let server = start_redir_server("/foo");
		let addr = server.addr();

		// Should fail, since 51 is in error range
		let runtime = new_simple_runtime("test-client-runtime-worker");
		let uri = format!("gemini://localhost:{}/", addr.port());
		let args = Args::parse_from([crate_name!(), &uri, "-fL"]);
		let res = runtime.block_on(run(args));
		assert!(
			matches!(res, ExitReason::ServerErrorResponse(StatusCode::NOT_FOUND)),
			"{uri}: {res:?}"
		);
		assert_eq!(server.request_count(), 2);
	}

	#[test]
	fn test_redirects_only_once() {
		// Server redirs once to a code 20
		let server = start_redir_server("/hello");
		let addr = server.addr();

		// Should succeed, since we permit only one redir
		let runtime = new_simple_runtime("test-client-runtime-worker");
		let uri = format!("gemini://localhost:{}/", addr.port());
		let args = Args::parse_from([crate_name!(), &uri, "-fL", "--max-redirs", "1"]);
		let res = runtime.block_on(run(args));
		assert!(matches!(res, ExitReason::Success), "{uri}: {res:?}");
		assert_eq!(server.request_count(), 2);
	}

	#[test]
	fn test_fails_to_redirects_when_none_permitted() {
		// Server redirs once to a code 20
		let server = start_redir_server("/hello");
		let addr = server.addr();

		// Should fail, since we permit no redirs
		let runtime = new_simple_runtime("test-client-runtime-worker");
		let uri = format!("gemini://localhost:{}/", addr.port());
		let args = Args::parse_from([crate_name!(), &uri, "-fL", "--max-redirs", "0"]);
		let res = runtime.block_on(run(args));
		assert!(
			matches!(res, ExitReason::TooManyRedirects { max: 0 }),
			"{uri}: {res:?}"
		);
		assert_eq!(server.request_count(), 1);
	}

	#[test]
	fn test_redirects_only_once_with_more_permitted() {
		for limit in 2..=5 {
			// Server redirs once to a code 20
			let server = start_redir_server("/hello");
			let addr = server.addr();

			// Spec: "Client MUST limit the number of redirections they follow to 5 redirections."
			// Should always succeed, since we permit enough redirs
			let runtime = new_simple_runtime("test-client-runtime-worker");
			let uri = format!("gemini://localhost:{}/", addr.port());
			let max = &limit.to_string();
			let args = Args::parse_from([crate_name!(), &uri, "-fL", "--max-redirs", max]);
			let res = runtime.block_on(run(args));
			assert!(matches!(res, ExitReason::Success), "{uri}: {res:?}");
			assert_eq!(server.request_count(), 2); // at least one request before redirs can be "followed"
		}
	}

	#[test]
	fn test_fails_at_no_close_notify() {
		let server = start_unfriendly_server_no_close_notify();
		let addr = server.addr();

		// Spec: "Gemini servers MUST use the TLS close_notify implementation to close the connection.
		//   [...] Both clients and servers SHOULD handle the case when the TLS close_notify
		//   mechanism is not used [...]. A client SHOULD notify the user of such a case; the server MAY log such a case."
		let runtime = new_simple_runtime("test-client-runtime-worker");
		let uri = format!("gemini://localhost:{}/", addr.port());
		let args = Args::parse_from([crate_name!(), &uri]);
		let res = runtime.block_on(run(args));
		assert!(matches!(res, ExitReason::AbruptClosure), "{uri}: {res:?}");
		assert_eq!(server.request_count(), 1);
	}

	#[test]
	fn test_allows_missing_close_notify_with_flag() {
		let server = start_unfriendly_server_no_close_notify();
		let addr = server.addr();

		let runtime = new_simple_runtime("test-client-runtime-worker");
		let uri = format!("gemini://localhost:{}/", addr.port());
		let args = Args::parse_from([crate_name!(), &uri, "--tls-allow-truncation"]);
		let res = runtime.block_on(run(args));
		assert!(matches!(res, ExitReason::Success), "{uri}: {res:?}");
		assert_eq!(server.request_count(), 1);
	}

	#[test]
	fn test_cancels_on_sigint() {
		let server = start_unfriendly_server_slow();
		let addr = server.addr();

		let runtime = new_simple_runtime("test-client-runtime-worker");
		let status = runtime
			.block_on(async {
				let mut child = Command::cargo_bin(crate_name!())
					.unwrap()
					.arg(format!("gemini://localhost:{}/", addr.port()))
					.spawn()
					.unwrap();
				tokio::time::sleep(Duration::from_millis(100)).await;
				child.interrupt().unwrap();
				child.wait()
			})
			.unwrap();
		assert_eq!(status.code(), None); // process was terminated by a signal
		assert_eq!(server.request_count(), 1); // request took flight before we were cancelled
	}

	#[test]
	fn test_cancels_on_sigterm() {
		let server = start_unfriendly_server_slow();
		let addr = server.addr();

		let runtime = new_simple_runtime("test-client-runtime-worker");
		let status = runtime
			.block_on(async {
				let mut child = Command::cargo_bin(crate_name!())
					.unwrap()
					.arg(format!("gemini://localhost:{}/", addr.port()))
					.spawn()
					.unwrap();
				tokio::time::sleep(Duration::from_millis(100)).await;
				child.term().unwrap();
				child.wait()
			})
			.unwrap();
		assert_eq!(status.code(), None); // process was terminated by a signal
		assert_eq!(server.request_count(), 1); // request took flight before we were cancelled
	}
}