vorma 0.86.0-pre.2

Vorma framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use super::*;

#[tokio::test]
async fn runtime_host_is_a_tower_service() {
	let dist_dir = temp_dist_dir("tower-service");
	write_manifest(&dist_dir);
	let mut host = host(&dist_dir);

	let response = tower_service::Service::call(
		&mut host,
		Request::builder()
			.method(Method::GET)
			.uri("/api/ping")
			.body(Full::new(Bytes::new()))
			.unwrap(),
	)
	.await
	.unwrap();
	let body = response.into_body().collect().await.unwrap().to_bytes();

	assert_eq!(body, Bytes::from_static(br#"{"pong":true}"#));
	fs::remove_dir_all(dist_dir).unwrap();
}

#[tokio::test]
async fn runtime_host_enforces_request_body_limit_in_tower_service() {
	let dist_dir = temp_dist_dir("body-limit");
	write_manifest(&dist_dir);
	let views = Views::new();
	let resources = Resources::new();
	let middlewares = Middlewares::new();
	let mut app = default_app(cfg(&dist_dir), views, resources, middlewares);
	app.request_body_limit = 4;
	let mut host = RuntimeHost::new(app).unwrap();

	let response = tower_service::Service::call(
		&mut host,
		Request::builder()
			.method(Method::POST)
			.uri("/api/ping")
			.body(Full::new(Bytes::from_static(b"12345")))
			.unwrap(),
	)
	.await
	.unwrap();
	let status = response.status();
	let body = response.into_body().collect().await.unwrap().to_bytes();

	assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE);
	assert_eq!(body, Bytes::from_static(b"Payload Too Large\n"));
	fs::remove_dir_all(dist_dir).unwrap();
}

#[tokio::test]
async fn runtime_host_head_body_limit_has_no_body_in_tower_service() {
	let dist_dir = temp_dist_dir("head-body-limit-service");
	write_manifest(&dist_dir);
	let mut app = default_app(
		cfg(&dist_dir),
		Views::new(),
		Resources::new(),
		Middlewares::new(),
	);
	app.request_body_limit = 4;
	let mut host = RuntimeHost::new(app).unwrap();

	let response = tower_service::Service::call(
		&mut host,
		Request::builder()
			.method(Method::HEAD)
			.uri("/api/ping")
			.body(Full::new(Bytes::from_static(b"12345")))
			.unwrap(),
	)
	.await
	.unwrap();
	let status = response.status();
	let content_length = response.headers().get(CONTENT_LENGTH).cloned();
	let body = response.into_body().collect().await.unwrap().to_bytes();

	assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE);
	assert_eq!(content_length.unwrap(), "18");
	assert!(body.is_empty());
	fs::remove_dir_all(dist_dir).unwrap();
}

#[tokio::test]
async fn runtime_host_enforces_request_body_limit_in_direct_handler() {
	let dist_dir = temp_dist_dir("direct-body-limit");
	write_manifest(&dist_dir);
	let views = Views::new();
	let resources = Resources::new();
	let middlewares = Middlewares::new();
	let mut app = default_app(cfg(&dist_dir), views, resources, middlewares);
	app.request_body_limit = 4;
	let host = RuntimeHost::new(app).unwrap();

	let response = host
		.handle_request(
			Request::builder()
				.method(Method::POST)
				.uri("/api/ping")
				.body(Bytes::from_static(b"12345"))
				.unwrap(),
		)
		.await
		.unwrap();

	assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
	assert_eq!(response.body(), &Bytes::from_static(b"Payload Too Large\n"));
	fs::remove_dir_all(dist_dir).unwrap();
}

#[tokio::test]
async fn runtime_host_head_body_limit_has_no_body_in_direct_handler() {
	let dist_dir = temp_dist_dir("head-body-limit-direct");
	write_manifest(&dist_dir);
	let mut app = default_app(
		cfg(&dist_dir),
		Views::new(),
		Resources::new(),
		Middlewares::new(),
	);
	app.request_body_limit = 4;
	let host = RuntimeHost::new(app).unwrap();

	let response = host
		.handle_request(
			Request::builder()
				.method(Method::HEAD)
				.uri("/api/ping")
				.body(Bytes::from_static(b"12345"))
				.unwrap(),
		)
		.await
		.unwrap();

	assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);
	assert_eq!(response.headers().get(CONTENT_LENGTH).unwrap(), "18");
	assert!(response.body().is_empty());
	fs::remove_dir_all(dist_dir).unwrap();
}

#[tokio::test]
async fn runtime_host_serves_view_json_payload() {
	let dist_dir = temp_dist_dir("view-json");
	write_view_manifest(&dist_dir);
	let host = view_host(&dist_dir);

	let response = host
		.handle_request(
			Request::builder()
				.method(Method::GET)
				.uri("/items/42?filter=active&vorma-json=_")
				.body(Bytes::new())
				.unwrap(),
		)
		.await
		.unwrap();
	let payload: serde_json::Value = serde_json::from_slice(response.body()).unwrap();

	assert_eq!(response.status(), StatusCode::OK);
	assert_eq!(payload["matched_patterns"][0], "/items/:id");
	assert_eq!(payload["params"]["id"], "42");
	assert_eq!(payload["import_urls"][0], "/static/items.js");
	assert_eq!(
		payload["deps"],
		json!(["/static/entry.js", "/static/shared.js", "/static/items.js"])
	);
	assert_eq!(
		payload["css_bundles"],
		json!([
			"/static/entry.css",
			"/static/shared.css",
			"/static/items.css"
		])
	);
	assert_eq!(payload.get("rest_head_els"), None);
	assert_eq!(payload["views_data"][0]["id"], "42");
	assert_eq!(
		payload["views_data"][0]["filter"],
		"filter=active&vorma-json=_"
	);
	fs::remove_dir_all(dist_dir).unwrap();
}

#[tokio::test]
async fn runtime_host_response_and_head_handles_do_not_deadlock_when_both_are_held() {
	let dist_dir = temp_dist_dir("view-response-head-handles");
	write_view_manifest(&dist_dir);
	let mut views = Views::new();
	views.push(View::new(
		"/items/:id",
		"items.tsx",
		InputParser::<()>::default_input(),
		|ctx: crate::core::ViewCtx<(), &'static str, ()>| async move {
			let mut response = ctx.response();
			response.set_header(
				HeaderName::from_static("x-before-head"),
				HeaderValue::from_static("1"),
			);
			ctx.head().title("Both Handles");
			response.set_header(
				HeaderName::from_static("x-after-head"),
				HeaderValue::from_static("1"),
			);
			Ok(json!({"id": ctx.param("id")}))
		},
	));
	let host = runtime_host(&dist_dir, views, Resources::new(), Middlewares::new());

	let response = tokio::time::timeout(
		Duration::from_millis(200),
		host.handle_request(empty_request(Method::GET, "/items/42")),
	)
	.await
	.expect("response/head handle locking should not hang")
	.unwrap();
	let body = String::from_utf8(response.body().to_vec()).unwrap();

	assert_eq!(response.status(), StatusCode::OK);
	assert_eq!(response.headers().get("x-before-head").unwrap(), "1");
	assert_eq!(response.headers().get("x-after-head").unwrap(), "1");
	assert!(body.contains("<title>Both Handles</title>"));
	fs::remove_dir_all(dist_dir).unwrap();
}

#[derive(Clone)]
struct RequestMarker(&'static str);

#[tokio::test]
async fn runtime_host_preserves_request_extensions_for_handlers() {
	let dist_dir = temp_dist_dir("request-extensions");
	write_view_manifest(&dist_dir);
	let mut views = Views::new();
	views.push(View::new(
		"/items/:id",
		"items.tsx",
		InputParser::<()>::default_input(),
		|ctx: crate::core::ViewCtx<(), &'static str, ()>| async move {
			let marker = ctx
				.request()
				.extension::<RequestMarker>()
				.map(|marker| marker.0)
				.unwrap_or("");
			Ok(json!({"marker": marker}))
		},
	));
	let host = runtime_host(&dist_dir, views, Resources::new(), Middlewares::new());

	let response = host
		.handle_request(
			Request::builder()
				.method(Method::GET)
				.uri("/items/42?vorma-json=_")
				.extension(RequestMarker("from-extension"))
				.body(Bytes::new())
				.unwrap(),
		)
		.await
		.unwrap();
	let payload: serde_json::Value = serde_json::from_slice(response.body()).unwrap();

	assert_eq!(response.status(), StatusCode::OK);
	assert_eq!(payload["views_data"][0]["marker"], "from-extension");
	fs::remove_dir_all(dist_dir).unwrap();
}

#[tokio::test]
async fn runtime_host_cancels_view_exec_ctx_when_request_future_is_dropped() {
	let dist_dir = temp_dist_dir("request-cancellation");
	write_view_manifest(&dist_dir);
	let (token_tx, token_rx) = tokio::sync::oneshot::channel::<CancelToken>();
	let token_tx = Arc::new(Mutex::new(Some(token_tx)));
	let mut views = Views::new();
	views.push(View::new(
		"/items/:id",
		"items.tsx",
		InputParser::<()>::default_input(),
		move |ctx: crate::core::ViewCtx<(), &'static str, ()>| {
			let token_tx = token_tx.clone();
			async move {
				if let Some(token_tx) = token_tx.lock().unwrap().take() {
					let _ = token_tx.send(ctx.exec_ctx().cancel_token().clone());
				}
				std::future::pending::<vorma_tasks::Result<serde_json::Value, &'static str>>().await
			}
		},
	));
	let host = RuntimeHost::new(default_app(
		cfg(&dist_dir),
		views,
		Resources::new(),
		Middlewares::new(),
	))
	.unwrap();
	let host_for_request = host.clone();
	let request = Request::builder()
		.method(Method::GET)
		.uri("/items/42?vorma-json=_")
		.body(Bytes::new())
		.unwrap();
	let handle = tokio::spawn(async move { host_for_request.handle_request(request).await });
	let token = tokio::time::timeout(Duration::from_secs(1), token_rx)
		.await
		.unwrap()
		.unwrap();

	handle.abort();
	let _ = handle.await;
	tokio::time::timeout(Duration::from_secs(1), token.cancelled())
		.await
		.unwrap();
	assert!(token.is_cancelled());
	fs::remove_dir_all(dist_dir).unwrap();
}

#[tokio::test]
async fn runtime_host_cancels_api_exec_ctx_when_request_future_is_dropped() {
	let dist_dir = temp_dist_dir("api-request-cancellation");
	write_manifest(&dist_dir);
	let (token_tx, token_rx) = tokio::sync::oneshot::channel::<CancelToken>();
	let token_tx = Arc::new(Mutex::new(Some(token_tx)));
	let views = Views::new();
	let mut resources = Resources::new();
	resources.push(Resource::new(
		Method::GET,
		"/pending",
		Some(ResourceKind::Query),
		InputParser::<()>::default_input(),
		move |ctx: crate::core::ResourceCtx<(), &'static str, ()>| {
			let token_tx = token_tx.clone();
			async move {
				if let Some(token_tx) = token_tx.lock().unwrap().take() {
					let _ = token_tx.send(ctx.exec_ctx().cancel_token().clone());
				}
				std::future::pending::<vorma_tasks::Result<serde_json::Value, &'static str>>().await
			}
		},
	));
	let host = runtime_host(&dist_dir, views, resources, Middlewares::new());
	let host_for_request = host.clone();
	let request = Request::builder()
		.method(Method::GET)
		.uri("/api/pending")
		.body(Bytes::new())
		.unwrap();
	let handle = tokio::spawn(async move { host_for_request.handle_request(request).await });
	let token = tokio::time::timeout(Duration::from_secs(1), token_rx)
		.await
		.unwrap()
		.unwrap();

	handle.abort();
	let _ = handle.await;
	tokio::time::timeout(Duration::from_secs(1), token.cancelled())
		.await
		.unwrap();
	assert!(token.is_cancelled());
	fs::remove_dir_all(dist_dir).unwrap();
}

#[tokio::test]
async fn runtime_host_view_context_resolves_public_urls() {
	let dist_dir = temp_dist_dir("view-public-url");
	write_view_manifest(&dist_dir);
	let mut views = Views::new();
	views.push(View::new(
		"/items/:id",
		"items.tsx",
		InputParser::<()>::default_input(),
		|ctx: crate::core::ViewCtx<(), &'static str, ()>| async move {
			Ok(json!({"app_css": ctx.public_url("app.css").unwrap()}))
		},
	));
	let host = runtime_host(&dist_dir, views, Resources::new(), Middlewares::new());

	let response = host
		.handle_request(empty_request(Method::GET, "/items/42?vorma-json=_"))
		.await
		.unwrap();
	let payload: serde_json::Value = serde_json::from_slice(response.body()).unwrap();

	assert_eq!(response.status(), StatusCode::OK);
	assert_eq!(payload["views_data"][0]["app_css"], "/static/app.css");
	fs::remove_dir_all(dist_dir).unwrap();
}

#[tokio::test]
async fn runtime_host_api_context_resolves_public_urls() {
	let dist_dir = temp_dist_dir("api-public-url");
	write_manifest(&dist_dir);
	let mut resources = Resources::new();
	resources.push(Resource::new(
		Method::GET,
		"/asset",
		Some(ResourceKind::Query),
		InputParser::<()>::default_input(),
		|ctx: crate::core::ResourceCtx<(), &'static str, ()>| async move {
			Ok(json!({"app_css": ctx.public_url("app.css").unwrap()}))
		},
	));
	let host = runtime_host(&dist_dir, Views::new(), resources, Middlewares::new());

	let response = host
		.handle_request(empty_request(Method::GET, "/api/asset"))
		.await
		.unwrap();

	assert_eq!(response.status(), StatusCode::OK);
	assert_eq!(
		response.body(),
		&Bytes::from_static(br#"{"app_css":"/static/app.css"}"#)
	);
	fs::remove_dir_all(dist_dir).unwrap();
}