/tests/serve_request.rs

https://github.com/svenstaro/miniserve · Rust · 124 lines · 100 code · 24 blank · 0 comment · 8 complexity · 0934d262e35a0deb671e4351d0910631 MD5 · raw file

  1. mod fixtures;
  2. use assert_cmd::prelude::*;
  3. use assert_fs::fixture::TempDir;
  4. use fixtures::{port, tmpdir, Error, DIRECTORIES, FILES};
  5. use regex::Regex;
  6. use rstest::rstest;
  7. use select::document::Document;
  8. use select::node::Node;
  9. use std::process::{Command, Stdio};
  10. use std::thread::sleep;
  11. use std::time::Duration;
  12. #[rstest]
  13. fn serves_requests_with_no_options(tmpdir: TempDir) -> Result<(), Error> {
  14. let mut child = Command::cargo_bin("miniserve")?
  15. .arg(tmpdir.path())
  16. .stdout(Stdio::null())
  17. .spawn()?;
  18. sleep(Duration::from_secs(1));
  19. let body = reqwest::blocking::get("http://localhost:8080")?.error_for_status()?;
  20. let parsed = Document::from_read(body)?;
  21. for &file in FILES {
  22. assert!(parsed.find(|x: &Node| x.text() == file).next().is_some());
  23. }
  24. child.kill()?;
  25. Ok(())
  26. }
  27. #[rstest]
  28. fn serves_requests_with_non_default_port(tmpdir: TempDir, port: u16) -> Result<(), Error> {
  29. let mut child = Command::cargo_bin("miniserve")?
  30. .arg(tmpdir.path())
  31. .arg("-p")
  32. .arg(port.to_string())
  33. .stdout(Stdio::null())
  34. .spawn()?;
  35. sleep(Duration::from_secs(1));
  36. let body = reqwest::blocking::get(format!("http://localhost:{}", port).as_str())?
  37. .error_for_status()?;
  38. let parsed = Document::from_read(body)?;
  39. for &file in FILES {
  40. let f = parsed.find(|x: &Node| x.text() == file).next().unwrap();
  41. assert_eq!(
  42. format!("/{}", file),
  43. percent_encoding::percent_decode_str(f.attr("href").unwrap()).decode_utf8_lossy(),
  44. );
  45. }
  46. for &directory in DIRECTORIES {
  47. assert!(parsed
  48. .find(|x: &Node| x.text() == directory)
  49. .next()
  50. .is_some());
  51. let dir_body =
  52. reqwest::blocking::get(format!("http://localhost:{}/{}", port, directory).as_str())?
  53. .error_for_status()?;
  54. let dir_body_parsed = Document::from_read(dir_body)?;
  55. for &file in FILES {
  56. assert!(dir_body_parsed
  57. .find(|x: &Node| x.text() == file)
  58. .next()
  59. .is_some());
  60. }
  61. }
  62. child.kill()?;
  63. Ok(())
  64. }
  65. #[rstest]
  66. fn serves_requests_with_randomly_assigned_port(tmpdir: TempDir) -> Result<(), Error> {
  67. let mut child = Command::cargo_bin("miniserve")?
  68. .arg(tmpdir.path())
  69. .arg("-p")
  70. .arg("0".to_string())
  71. .stdout(Stdio::piped())
  72. .spawn()?;
  73. sleep(Duration::from_secs(1));
  74. child.kill()?;
  75. let output = child.wait_with_output().expect("Failed to read stdout");
  76. let all_text = String::from_utf8(output.stdout)?;
  77. let re = Regex::new(r"http://127.0.0.1:(\d+)").unwrap();
  78. let caps = re.captures(all_text.as_str()).unwrap();
  79. let port_num = caps.get(1).unwrap().as_str().parse::<u16>().unwrap();
  80. assert!(port_num > 0);
  81. Ok(())
  82. }
  83. #[rstest]
  84. fn serves_requests_custom_index_notice(tmpdir: TempDir, port: u16) -> Result<(), Error> {
  85. let mut child = Command::cargo_bin("miniserve")?
  86. .arg("--index=not.html")
  87. .arg("-p")
  88. .arg(port.to_string())
  89. .arg(tmpdir.path())
  90. .stdout(Stdio::piped())
  91. .spawn()?;
  92. sleep(Duration::from_secs(1));
  93. child.kill()?;
  94. let output = child.wait_with_output().expect("Failed to read stdout");
  95. let all_text = String::from_utf8(output.stdout);
  96. assert!(all_text
  97. .unwrap()
  98. .contains("The provided index file could not be found"));
  99. Ok(())
  100. }