XFFXFF commited on
Commit
12bb0ee
1 Parent(s): ecf77fc

add a integration test for index page

Browse files
Files changed (3) hide show
  1. src/bin/websurfx.rs +4 -31
  2. src/lib.rs +57 -0
  3. tests/index.rs +42 -0
src/bin/websurfx.rs CHANGED
@@ -3,15 +3,11 @@
3
  //! This module contains the main function which handles the logging of the application to the
4
  //! stdout and handles the command line arguments provided and launches the `websurfx` server.
5
 
6
- use std::ops::RangeInclusive;
7
 
8
- use websurfx::server::routes;
9
-
10
- use actix_files as fs;
11
- use actix_web::{middleware::Logger, web, App, HttpServer};
12
  use clap::{command, Parser};
13
  use env_logger::Env;
14
- use handlebars::Handlebars;
15
 
16
  /// A commandline arguments struct.
17
  #[derive(Parser, Debug, Default)]
@@ -67,30 +63,7 @@ async fn main() -> std::io::Result<()> {
67
 
68
  log::info!("started server on port {}", args.port);
69
 
70
- let mut handlebars: Handlebars = Handlebars::new();
71
-
72
- handlebars
73
- .register_templates_directory(".html", "./public/templates")
74
- .unwrap();
75
-
76
- let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
77
 
78
- HttpServer::new(move || {
79
- App::new()
80
- .app_data(handlebars_ref.clone())
81
- .wrap(Logger::default()) // added logging middleware for logging.
82
- // Serve images and static files (css and js files).
83
- .service(fs::Files::new("/static", "./public/static").show_files_listing())
84
- .service(fs::Files::new("/images", "./public/images").show_files_listing())
85
- .service(routes::robots_data) // robots.txt
86
- .service(routes::index) // index page
87
- .service(routes::search) // search page
88
- .service(routes::about) // about page
89
- .service(routes::settings) // settings page
90
- .default_service(web::route().to(routes::not_found)) // error page
91
- })
92
- // Start server on 127.0.0.1 with the user provided port number. for example 127.0.0.1:8080.
93
- .bind(("127.0.0.1", args.port))?
94
- .run()
95
- .await
96
  }
 
3
  //! This module contains the main function which handles the logging of the application to the
4
  //! stdout and handles the command line arguments provided and launches the `websurfx` server.
5
 
6
+ use std::{ops::RangeInclusive, net::TcpListener};
7
 
 
 
 
 
8
  use clap::{command, Parser};
9
  use env_logger::Env;
10
+ use websurfx::run;
11
 
12
  /// A commandline arguments struct.
13
  #[derive(Parser, Debug, Default)]
 
63
 
64
  log::info!("started server on port {}", args.port);
65
 
66
+ let listener = TcpListener::bind(("127.0.0.1", args.port))?;
 
 
 
 
 
 
67
 
68
+ run(listener)?.await
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  }
src/lib.rs CHANGED
@@ -1,3 +1,60 @@
1
  pub mod engines;
2
  pub mod server;
3
  pub mod search_results_handler;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  pub mod engines;
2
  pub mod server;
3
  pub mod search_results_handler;
4
+
5
+ use std::net::TcpListener;
6
+
7
+ use crate::server::routes;
8
+
9
+ use actix_files as fs;
10
+ use actix_web::{middleware::Logger, web, App, HttpServer, dev::Server};
11
+ use handlebars::Handlebars;
12
+
13
+
14
+ /// Runs the web server on the provided TCP listener and returns a `Server` instance.
15
+ ///
16
+ /// # Arguments
17
+ ///
18
+ /// * `listener` - A `TcpListener` instance representing the address and port to listen on.
19
+ ///
20
+ /// # Returns
21
+ ///
22
+ /// Returns a `Result` containing a `Server` instance on success, or an `std::io::Error` on failure.
23
+ ///
24
+ /// # Example
25
+ ///
26
+ /// ```
27
+ /// use std::net::TcpListener;
28
+ /// use web_server::Server;
29
+ ///
30
+ /// let listener = TcpListener::bind("127.0.0.1:8080").expect("Failed to bind address");
31
+ /// let server = Server::run(listener).expect("Failed to start server");
32
+ /// ```
33
+ pub fn run(listener: TcpListener) -> std::io::Result<Server> {
34
+ let mut handlebars: Handlebars = Handlebars::new();
35
+
36
+ handlebars
37
+ .register_templates_directory(".html", "./public/templates")
38
+ .unwrap();
39
+
40
+ let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
41
+
42
+ let server = HttpServer::new(move || {
43
+ App::new()
44
+ .app_data(handlebars_ref.clone())
45
+ .wrap(Logger::default()) // added logging middleware for logging.
46
+ // Serve images and static files (css and js files).
47
+ .service(fs::Files::new("/static", "./public/static").show_files_listing())
48
+ .service(fs::Files::new("/images", "./public/images").show_files_listing())
49
+ .service(routes::robots_data) // robots.txt
50
+ .service(routes::index) // index page
51
+ .service(routes::search) // search page
52
+ .service(routes::about) // about page
53
+ .service(routes::settings) // settings page
54
+ .default_service(web::route().to(routes::not_found)) // error page
55
+ })
56
+ // Start server on 127.0.0.1 with the user provided port number. for example 127.0.0.1:8080.
57
+ .listen(listener)?
58
+ .run();
59
+ Ok(server)
60
+ }
tests/index.rs ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ use std::net::TcpListener;
2
+
3
+ use handlebars::Handlebars;
4
+ use websurfx::run;
5
+
6
+
7
+ // Starts a new instance of the HTTP server, bound to a random available port
8
+ fn spawn_app() -> String {
9
+ // Binding to port 0 will trigger the OS to assign a port for us.
10
+ let listener = TcpListener::bind("127.0.0.1:0").expect("Failed to bind random port");
11
+ let port = listener.local_addr().unwrap().port();
12
+ let server = run(listener).expect("Failed to bind address");
13
+
14
+ tokio::spawn(server);
15
+ format!("http://127.0.0.1:{}/", port)
16
+ }
17
+
18
+ // Creates a new instance of Handlebars and registers the templates directory.
19
+ // This is used to compare the rendered template with the response body.
20
+ fn handlebars() -> Handlebars<'static> {
21
+ let mut handlebars = Handlebars::new();
22
+
23
+ handlebars
24
+ .register_templates_directory(".html", "./public/templates")
25
+ .unwrap();
26
+
27
+ handlebars
28
+ }
29
+
30
+
31
+ #[tokio::test]
32
+ async fn test_index() {
33
+ let address = spawn_app();
34
+
35
+ let client = reqwest::Client::new();
36
+ let res = client.get(address).send().await.unwrap();
37
+ assert_eq!(res.status(), 200);
38
+
39
+ let handlebars = handlebars();
40
+ let template = handlebars.render("index", &()).unwrap();
41
+ assert_eq!(res.text().await.unwrap(), template);
42
+ }