@@ -6,6 +6,19 @@ use sqlpage::webserver::http::main_handler;
66
77use crate :: common:: { get_request_to, make_app_data} ;
88
9+ async fn req_with_accept (
10+ path : & str ,
11+ accept : & str ,
12+ ) -> actix_web:: Result < actix_web:: dev:: ServiceResponse > {
13+ let app_data = make_app_data ( ) . await ;
14+ let req = TestRequest :: get ( )
15+ . uri ( path)
16+ . insert_header ( ( header:: ACCEPT , accept) )
17+ . app_data ( app_data)
18+ . to_srv_request ( ) ;
19+ main_handler ( req) . await
20+ }
21+
922#[ actix_web:: test]
1023async fn test_json_body ( ) -> actix_web:: Result < ( ) > {
1124 let req = get_request_to ( "/tests/data_formats/json_data.sql" )
@@ -83,44 +96,33 @@ async fn test_json_columns() {
8396
8497#[ actix_web:: test]
8598async fn test_accept_json_returns_json_array ( ) -> actix_web:: Result < ( ) > {
86- let app_data = make_app_data ( ) . await ;
87- let req = TestRequest :: get ( )
88- . uri ( "/tests/data_formats/accept_json_test.sql" )
89- . insert_header ( ( header:: ACCEPT , "application/json" ) )
90- . app_data ( app_data)
91- . to_srv_request ( ) ;
92- let resp = main_handler ( req) . await ?;
93-
99+ let resp = req_with_accept (
100+ "/tests/sql_test_files/it_works_simple.sql" ,
101+ "application/json" ,
102+ )
103+ . await ?;
94104 assert_eq ! ( resp. status( ) , StatusCode :: OK ) ;
95105 assert_eq ! (
96106 resp. headers( ) . get( header:: CONTENT_TYPE ) . unwrap( ) ,
97107 "application/json"
98108 ) ;
99109 let body = test:: read_body ( resp) . await ;
100110 let body_json: serde_json:: Value = serde_json:: from_slice ( & body) . unwrap ( ) ;
101- assert ! ( body_json. is_array( ) , "response should be a JSON array" ) ;
111+ assert ! ( body_json. is_array( ) ) ;
102112 let arr = body_json. as_array ( ) . unwrap ( ) ;
103- assert_eq ! ( arr. len( ) , 4 ) ;
104- assert_eq ! ( arr[ 0 ] [ "component" ] , "text" ) ;
105- assert_eq ! ( arr[ 0 ] [ "contents" ] , "Hello World" ) ;
106- assert_eq ! ( arr[ 1 ] [ "component" ] , "table" ) ;
107- assert_eq ! ( arr[ 2 ] [ "id" ] , 1 ) ;
108- assert_eq ! ( arr[ 2 ] [ "name" ] , "Alice" ) ;
109- assert_eq ! ( arr[ 3 ] [ "id" ] , 2 ) ;
110- assert_eq ! ( arr[ 3 ] [ "name" ] , "Bob" ) ;
113+ assert ! ( arr. len( ) >= 2 ) ;
114+ assert_eq ! ( arr[ 0 ] [ "component" ] , "shell" ) ;
115+ assert_eq ! ( arr[ 1 ] [ "component" ] , "text" ) ;
111116 Ok ( ( ) )
112117}
113118
114119#[ actix_web:: test]
115120async fn test_accept_ndjson_returns_jsonlines ( ) -> actix_web:: Result < ( ) > {
116- let app_data = make_app_data ( ) . await ;
117- let req = TestRequest :: get ( )
118- . uri ( "/tests/data_formats/accept_json_test.sql" )
119- . insert_header ( ( header:: ACCEPT , "application/x-ndjson" ) )
120- . app_data ( app_data)
121- . to_srv_request ( ) ;
122- let resp = main_handler ( req) . await ?;
123-
121+ let resp = req_with_accept (
122+ "/tests/sql_test_files/it_works_simple.sql" ,
123+ "application/x-ndjson" ,
124+ )
125+ . await ?;
124126 assert_eq ! ( resp. status( ) , StatusCode :: OK ) ;
125127 assert_eq ! (
126128 resp. headers( ) . get( header:: CONTENT_TYPE ) . unwrap( ) ,
@@ -129,60 +131,34 @@ async fn test_accept_ndjson_returns_jsonlines() -> actix_web::Result<()> {
129131 let body = test:: read_body ( resp) . await ;
130132 let body_str = String :: from_utf8 ( body. to_vec ( ) ) . unwrap ( ) ;
131133 let lines: Vec < & str > = body_str. trim ( ) . lines ( ) . collect ( ) ;
132- assert_eq ! ( lines. len( ) , 4 ) ;
133-
134- let first: serde_json:: Value = serde_json:: from_str ( lines[ 0 ] ) . unwrap ( ) ;
135- assert_eq ! ( first[ "component" ] , "text" ) ;
136- assert_eq ! ( first[ "contents" ] , "Hello World" ) ;
137-
138- let second: serde_json:: Value = serde_json:: from_str ( lines[ 1 ] ) . unwrap ( ) ;
139- assert_eq ! ( second[ "component" ] , "table" ) ;
140-
141- let third: serde_json:: Value = serde_json:: from_str ( lines[ 2 ] ) . unwrap ( ) ;
142- assert_eq ! ( third[ "id" ] , 1 ) ;
143- assert_eq ! ( third[ "name" ] , "Alice" ) ;
144-
145- let fourth: serde_json:: Value = serde_json:: from_str ( lines[ 3 ] ) . unwrap ( ) ;
146- assert_eq ! ( fourth[ "id" ] , 2 ) ;
147- assert_eq ! ( fourth[ "name" ] , "Bob" ) ;
134+ assert ! ( lines. len( ) >= 2 ) ;
135+ assert_eq ! (
136+ serde_json:: from_str:: <serde_json:: Value >( lines[ 0 ] ) . unwrap( ) [ "component" ] ,
137+ "shell"
138+ ) ;
139+ assert_eq ! (
140+ serde_json:: from_str:: <serde_json:: Value >( lines[ 1 ] ) . unwrap( ) [ "component" ] ,
141+ "text"
142+ ) ;
148143 Ok ( ( ) )
149144}
150145
151146#[ actix_web:: test]
152147async fn test_accept_html_returns_html ( ) -> actix_web:: Result < ( ) > {
153- let app_data = make_app_data ( ) . await ;
154- let req = TestRequest :: get ( )
155- . uri ( "/tests/data_formats/accept_json_test.sql" )
156- . insert_header ( ( header:: ACCEPT , "text/html" ) )
157- . app_data ( app_data)
158- . to_srv_request ( ) ;
159- let resp = main_handler ( req) . await ?;
160-
148+ let resp = req_with_accept ( "/tests/sql_test_files/it_works_simple.sql" , "text/html" ) . await ?;
161149 assert_eq ! ( resp. status( ) , StatusCode :: OK ) ;
162150 assert_eq ! (
163151 resp. headers( ) . get( header:: CONTENT_TYPE ) . unwrap( ) ,
164152 "text/html; charset=utf-8"
165153 ) ;
166154 let body = test:: read_body ( resp) . await ;
167- let body_str = String :: from_utf8 ( body. to_vec ( ) ) . unwrap ( ) ;
168- assert ! ( body_str. contains( "<html" ) , "response should contain HTML" ) ;
169- assert ! (
170- body_str. contains( "Hello World" ) ,
171- "response should contain the text content"
172- ) ;
155+ assert ! ( body. starts_with( b"<!DOCTYPE html>" ) ) ;
173156 Ok ( ( ) )
174157}
175158
176159#[ actix_web:: test]
177160async fn test_accept_wildcard_returns_html ( ) -> actix_web:: Result < ( ) > {
178- let app_data = make_app_data ( ) . await ;
179- let req = TestRequest :: get ( )
180- . uri ( "/tests/data_formats/accept_json_test.sql" )
181- . insert_header ( ( header:: ACCEPT , "*/*" ) )
182- . app_data ( app_data)
183- . to_srv_request ( ) ;
184- let resp = main_handler ( req) . await ?;
185-
161+ let resp = req_with_accept ( "/tests/sql_test_files/it_works_simple.sql" , "*/*" ) . await ?;
186162 assert_eq ! ( resp. status( ) , StatusCode :: OK ) ;
187163 assert_eq ! (
188164 resp. headers( ) . get( header:: CONTENT_TYPE ) . unwrap( ) ,
@@ -193,45 +169,12 @@ async fn test_accept_wildcard_returns_html() -> actix_web::Result<()> {
193169
194170#[ actix_web:: test]
195171async fn test_accept_json_redirect_still_works ( ) -> actix_web:: Result < ( ) > {
196- let app_data = make_app_data ( ) . await ;
197- let req = TestRequest :: get ( )
198- . uri ( "/tests/data_formats/accept_json_redirect_test.sql" )
199- . insert_header ( ( header:: ACCEPT , "application/json" ) )
200- . app_data ( app_data)
201- . to_srv_request ( ) ;
202- let resp = main_handler ( req) . await ?;
203-
172+ let resp =
173+ req_with_accept ( "/tests/server_timing/redirect_test.sql" , "application/json" ) . await ?;
204174 assert_eq ! ( resp. status( ) , StatusCode :: FOUND ) ;
205- assert_eq ! ( resp. headers( ) . get( header:: LOCATION ) . unwrap( ) , "/target" ) ;
206- Ok ( ( ) )
207- }
208-
209- #[ actix_web:: test]
210- async fn test_accept_json_headers_still_work ( ) -> actix_web:: Result < ( ) > {
211- let app_data = make_app_data ( ) . await ;
212- let req = TestRequest :: get ( )
213- . uri ( "/tests/data_formats/accept_json_headers_test.sql" )
214- . insert_header ( ( header:: ACCEPT , "application/json" ) )
215- . app_data ( app_data)
216- . to_srv_request ( ) ;
217- let resp = main_handler ( req) . await ?;
218-
219- assert_eq ! ( resp. status( ) , StatusCode :: CREATED ) ;
220- let set_cookie = resp. headers ( ) . get ( header:: SET_COOKIE ) . unwrap ( ) ;
221- assert ! (
222- set_cookie
223- . to_str( )
224- . unwrap( )
225- . contains( "test_cookie=cookie_value" ) ,
226- "Cookie should be set: {:?}" ,
227- set_cookie
175+ assert_eq ! (
176+ resp. headers( ) . get( header:: LOCATION ) . unwrap( ) ,
177+ "/destination.sql"
228178 ) ;
229- let body = test:: read_body ( resp) . await ;
230- let body_json: serde_json:: Value = serde_json:: from_slice ( & body) . unwrap ( ) ;
231- assert ! ( body_json. is_array( ) ) ;
232- let arr = body_json. as_array ( ) . unwrap ( ) ;
233- assert_eq ! ( arr. len( ) , 1 ) ;
234- assert_eq ! ( arr[ 0 ] [ "component" ] , "text" ) ;
235- assert_eq ! ( arr[ 0 ] [ "contents" ] , "Created" ) ;
236179 Ok ( ( ) )
237180}
0 commit comments