httpserver_test.rs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. mod common;
  19. use smartdns_ui::smartdns::LogLevel;
  20. use std::{fs::File, io::Write};
  21. #[test]
  22. fn test_http_server_indexhtml() {
  23. let mut server = common::TestServer::new();
  24. server.set_log_level(LogLevel::DEBUG);
  25. assert!(server.start().is_ok());
  26. let www_root = server.get_www_root();
  27. let mut index_html_file = File::create(www_root.clone() + "/index.html").unwrap();
  28. let content = "Hello, world!";
  29. index_html_file.write_all(content.as_bytes()).unwrap();
  30. let client = common::TestClient::new(&server.get_host());
  31. let c = client.get("/");
  32. assert!(c.is_ok());
  33. let (code, body) = c.unwrap();
  34. assert_eq!(code, 200);
  35. assert_eq!(body, content);
  36. }
  37. #[test]
  38. fn test_http_server_somehtml() {
  39. let mut server = common::TestServer::new();
  40. server.set_log_level(LogLevel::DEBUG);
  41. assert!(server.start().is_ok());
  42. let www_root = server.get_www_root();
  43. let mut index_html_file = File::create(www_root.clone() + "/index.html").unwrap();
  44. let content = "Hello, world!";
  45. index_html_file.write_all(content.as_bytes()).unwrap();
  46. let mut some_html_file = File::create(www_root.clone() + "/some.html").unwrap();
  47. let some_content = "Some index file!";
  48. some_html_file.write_all(some_content.as_bytes()).unwrap();
  49. let client = common::TestClient::new(&server.get_host());
  50. let c = client.get("/some.html");
  51. assert!(c.is_ok());
  52. let (code, body) = c.unwrap();
  53. assert_eq!(code, 200);
  54. assert_eq!(body, some_content);
  55. }
  56. #[test]
  57. fn test_http_server_redirect_indexhtml() {
  58. let mut server = common::TestServer::new();
  59. server.set_log_level(LogLevel::DEBUG);
  60. assert!(server.start().is_ok());
  61. let www_root = server.get_www_root();
  62. let mut index_html_file = File::create(www_root.clone() + "/index.html").unwrap();
  63. let content = "Hello, world!";
  64. index_html_file.write_all(content.as_bytes()).unwrap();
  65. let client = common::TestClient::new(&server.get_host());
  66. let c = client.get("/some.html");
  67. assert!(c.is_ok());
  68. let (code, body) = c.unwrap();
  69. assert_eq!(code, 200);
  70. assert_eq!(body, content);
  71. }
  72. #[test]
  73. fn test_http_server_404() {
  74. let mut server = common::TestServer::new();
  75. server.set_log_level(LogLevel::DEBUG);
  76. assert!(server.start().is_ok());
  77. let client = common::TestClient::new(&server.get_host());
  78. let c = client.get("/index.html");
  79. assert!(c.is_ok());
  80. let (code, _) = c.unwrap();
  81. assert_eq!(code, 404);
  82. }