1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

context, vweb: add ability to set and get values on vweb.Context (#18564)

This commit is contained in:
Casper Kuethe
2023-06-27 00:25:45 +02:00
committed by GitHub
parent 7a9c885b31
commit 21d9730cde
6 changed files with 153 additions and 3 deletions

View File

@ -239,6 +239,13 @@ fn test_redirect_middleware() {
assert received.ends_with('302 Found')
}
// Context's
fn test_middleware_with_context() {
x := http.get('http://${localserver}/with-context') or { panic(err) }
assert x.body == 'b'
}
fn testsuite_end() {
// This test is guaranteed to be called last.
// It sends a request to the server to shutdown.

View File

@ -46,6 +46,7 @@ fn main() {
'/admin/': [middleware1]
'/other/': [middleware1, middleware2]
'/redirect': [middleware_redirect]
'/with-context': [context1]
}
}
eprintln('>> webserver: pid: ${os.getpid()}, started on http://localhost:${http_port}/ , with maximum runtime of ${app.timeout} milliseconds.')
@ -220,6 +221,12 @@ pub fn (mut app App) redirect_route() vweb.Result {
return app.text('${result}should_never_reach!')
}
['/with-context']
pub fn (mut app App) with_context() vweb.Result {
a := app.get_value[string]('a') or { 'none' }
return app.text(a)
}
// middleware functions:
pub fn (mut app App) before_request() {
@ -256,6 +263,11 @@ fn middleware_redirect(mut ctx vweb.Context) bool {
return false
}
fn context1(mut ctx vweb.Context) bool {
ctx.set_value('a', 'b')
return true
}
// utility functions:
pub fn (mut app App) shutdown() vweb.Result {