Skip to content

Commit

Permalink
x.vweb: accept query params as method arguments (#21201)
Browse files Browse the repository at this point in the history
  • Loading branch information
Casper64 committed Apr 7, 2024
1 parent c2c83f7 commit d0cbfc1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
6 changes: 6 additions & 0 deletions vlib/x/vweb/tests/vweb_test.v
Expand Up @@ -249,6 +249,12 @@ fn test_login_with_multipart_form_data_send_by_fetch() {
assert x.body == 'username: xmyusernamex | password: xmypassword123x'
}

fn test_query_params_are_passed_as_arguments() {
x := http.get('http://${localserver}/query_echo?c=3&a="test"&b=20')!
assert x.status() == .ok
assert x.body == 'a: x"test"x | b: x20x'
}

fn test_host() {
mut req := http.Request{
url: 'http://${localserver}/with_host'
Expand Down
5 changes: 5 additions & 0 deletions vlib/x/vweb/tests/vweb_test_server.v
Expand Up @@ -114,6 +114,11 @@ pub fn (mut app ServerApp) file_echo(mut ctx ServerContext) vweb.Result {
return ctx.text(ctx.files['file'][0].data)
}

@['/query_echo']
pub fn (mut app ServerApp) query_echo(mut ctx ServerContext, a string, b int) vweb.Result {
return ctx.text('a: x${a}x | b: x${b}x')
}

// Make sure [post] works without the path
@[post]
pub fn (mut app ServerApp) json(mut ctx ServerContext) vweb.Result {
Expand Down
34 changes: 30 additions & 4 deletions vlib/x/vweb/vweb.v
Expand Up @@ -824,6 +824,8 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string

// Skip if the host does not match or is empty
if route.host == '' || route.host == host {
can_have_data_args := user_context.Context.req.method == .post
|| user_context.Context.req.method == .get
// Route immediate matches first
// For example URL `/register` matches route `/:user`, but `fn register()`
// should be called first.
Expand All @@ -836,12 +838,19 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
}
}

if user_context.Context.req.method == .post && method.args.len > 1 {
// Populate method args with form values
if method.args.len > 1 && can_have_data_args {
// Populate method args with form or query values
mut args := []string{cap: method.args.len + 1}
data := if user_context.Context.req.method == .get {
user_context.Context.query
} else {
user_context.Context.form
}

for param in method.args[1..] {
args << user_context.Context.form[param.name]
args << data[param.name]
}

app.$method(mut user_context, args)
} else {
app.$method(mut user_context)
Expand All @@ -857,7 +866,24 @@ fn handle_route[A, X](mut app A, mut user_context X, url urllib.URL, host string
}
}

app.$method(mut user_context)
if method.args.len > 1 && can_have_data_args {
// Populate method args with form or query values
mut args := []string{cap: method.args.len + 1}

data := if user_context.Context.req.method == .get {
user_context.Context.query
} else {
user_context.Context.form
}

for param in method.args[1..] {
args << data[param.name]
}

app.$method(mut user_context, args)
} else {
app.$method(mut user_context)
}
return
}

Expand Down

0 comments on commit d0cbfc1

Please sign in to comment.