MCPcopy Create free account
hub / github.com/actix/examples / test

Function test

templating/yarte/src/main.rs:43–121  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

41
42 #[actix_web::test]
43 async fn test() {
44 let app = atest::init_service(App::new().service(index)).await;
45
46 let req = atest::TestRequest::with_uri("/").to_request();
47 let resp = atest::call_service(&app, req).await;
48
49 assert!(resp.status().is_success());
50
51 assert_eq!(
52 resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
53 "text/html; charset=utf-8"
54 );
55
56 let bytes = atest::read_body(resp).await;
57 assert_eq!(
58 bytes,
59 Bytes::from_static(
60 "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
61 Web</title></head><body><h1 id=\"welcome\" \
62 class=\"welcome\">Welcome!</h1><div><h3>What is your name?</h3><form>Name: \
63 <input type=\"text\" name=\"name\"><br>Last name: <input type=\"text\" \
64 name=\"lastname\"><br><p><input type=\"submit\"></p></form></div></body></html>"
65 .as_ref()
66 )
67 );
68
69 let req = atest::TestRequest::with_uri("/?name=foo&lastname=bar").to_request();
70 let resp = atest::call_service(&app, req).await;
71
72 assert!(resp.status().is_success());
73
74 assert_eq!(
75 resp.headers().get(http::header::CONTENT_TYPE).unwrap(),
76 "text/html; charset=utf-8"
77 );
78
79 let bytes = atest::read_body(resp).await;
80 assert_eq!(
81 bytes,
82 Bytes::from_static(
83 "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
84 Web</title></head><body><h1>Hi, foo bar!</h1><p id=\"hi\" \
85 class=\"welcome\">Welcome</p></body></html>"
86 .as_ref()
87 )
88 );
89
90 let req = atest::TestRequest::with_uri("/?name=foo").to_request();
91 let resp = atest::call_service(&app, req).await;
92
93 assert!(resp.status().is_server_error());
94
95 let bytes = atest::read_body(resp).await;
96
97 assert_eq!(bytes, Bytes::from_static("Bad query".as_ref()));
98
99 let req = atest::TestRequest::with_uri("/?lastname=bar").to_request();
100 let resp = atest::call_service(&app, req).await;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected