cargo-lambda-watch 1.9.1

Cargo subcommand to work with AWS Lambda
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
use crate::{
    RefRuntimeState,
    error::ServerError,
    eventstream,
    requests::*,
    runtime::{LAMBDA_RUNTIME_AWS_REQUEST_ID, LAMBDA_RUNTIME_XRAY_TRACE_HEADER},
};
use aws_lambda_events::{
    apigw::{
        ApiGatewayV2httpRequest, ApiGatewayV2httpRequestContext,
        ApiGatewayV2httpRequestContextHttpDescription, ApiGatewayV2httpResponse,
    },
    encodings::Body as LambdaBody,
};
use axum::{
    Router,
    body::Body,
    extract::{Extension, Path, State},
    http::{HeaderValue, Request, response::Builder},
    response::Response,
    routing::{any, post},
};
use base64::{Engine as _, engine::general_purpose as b64};
use cargo_lambda_metadata::DEFAULT_PACKAGE_FUNCTION;
use chrono::Utc;
use http::Method;
use http_body_util::BodyExt;
use hyper::{HeaderMap, StatusCode, header};
use miette::Result;
use opentelemetry::{
    Context, KeyValue, global,
    trace::{TraceContextExt, Tracer},
};
use query_map::QueryMap;
use std::collections::{HashMap, HashSet};
use tokio::sync::{mpsc::Sender, oneshot};

const LAMBDA_URL_PREFIX: &str = "lambda-url";

pub(crate) fn routes() -> Router<RefRuntimeState> {
    Router::new()
        .route(
            "/2015-03-31/functions/:function_name/invocations",
            post(invoke_handler),
        )
        .route(
            "/2015-03-31/functions/:function_name/response-streaming-invocations",
            post(invoke_with_response_stream_handler),
        )
        .route("/lambda-url/:function_name/*path", any(furls_handler))
        .fallback(furls_handler)
}

async fn furls_handler(
    State(state): State<RefRuntimeState>,
    Extension(cmd_tx): Extension<Sender<Action>>,
    req: Request<Body>,
) -> Result<Response<Body>, ServerError> {
    tracing::debug!(path = %req.uri().path(), method = %req.method(), "http invocation received");

    let (parts, body) = req.into_parts();
    let uri = &parts.uri;

    let (function_name, mut path, path_parameters) =
        extract_path_parameters(uri.path(), &parts.method, &state);
    tracing::trace!(%function_name, %path, "received request in furls handler");

    if function_name == DEFAULT_PACKAGE_FUNCTION && !state.is_default_function_enabled() {
        return respond_with_disabled_default_function(&state, false);
    }

    if function_name != DEFAULT_PACKAGE_FUNCTION {
        if let Err(binaries) = state.is_function_available(&function_name) {
            return respond_with_missing_function(&binaries);
        }
    }

    let headers = &parts.headers;

    let body = body
        .collect()
        .await
        .map_err(ServerError::DataDeserialization)?
        .to_bytes();
    let text_content_type = match headers.get("content-type") {
        None => true,
        Some(c) => {
            let c = c.to_str().unwrap_or_default();
            c.starts_with("text/") || c.starts_with("application/json")
        }
    };

    let (body, is_base64_encoded) = if body.is_empty() {
        (None, false)
    } else if text_content_type {
        let body =
            String::from_utf8(body.into_iter().collect()).map_err(ServerError::StringBody)?;
        (Some(body), false)
    } else {
        let body = b64::STANDARD.encode(body.into_iter().collect::<Vec<u8>>());
        (Some(body), true)
    };

    let query_string_parameters = uri
        .query()
        .unwrap_or_default()
        .parse::<QueryMap>()
        .unwrap_or_default();

    let cookies = headers.get("cookie").map(|c| {
        c.to_str()
            .unwrap_or_default()
            .split("; ")
            .map(|s| s.trim().to_string())
            .collect()
    });

    let req_id = headers
        .get(LAMBDA_RUNTIME_AWS_REQUEST_ID)
        .expect("missing request id")
        .to_str()
        .expect("invalid request id format");

    let time = Utc::now();

    if !path.starts_with('/') {
        path = format!("/{path}");
    }

    let request_context = ApiGatewayV2httpRequestContext {
        stage: Some("$default".into()),
        route_key: Some("$default".into()),
        request_id: Some(req_id.into()),
        domain_name: Some("localhost".into()),
        domain_prefix: Some(function_name.clone()),
        http: ApiGatewayV2httpRequestContextHttpDescription {
            method: parts.method.clone(),
            path: Some(path.clone()),
            protocol: Some("http".into()),
            source_ip: Some("127.0.0.1".into()),
            user_agent: Some("cargo-lambda".into()),
        },
        time: Some(time.format("%d/%b/%Y:%T %z").to_string()),
        time_epoch: time.timestamp(),
        account_id: None,
        authorizer: None,
        authentication: None,
        apiid: None,
    };

    let event = ApiGatewayV2httpRequest {
        version: Some("2.0".into()),
        route_key: Some("$default".into()),
        raw_path: Some(path),
        raw_query_string: uri.query().map(String::from),
        headers: headers.clone(),
        body,
        request_context,
        cookies,
        query_string_parameters,
        is_base64_encoded,
        path_parameters,
        ..Default::default()
    };
    let event = serde_json::to_string(&event).map_err(ServerError::SerializationError)?;

    let req = Request::from_parts(parts, event.into());
    let resp = schedule_invocation(&cmd_tx, function_name, req).await?;
    let status_code = resp
        .extensions()
        .get::<StatusCode>()
        .cloned()
        .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);

    let (info, mut body) = resp.into_parts();

    let mut builder = Response::builder().status(status_code);

    let response = if status_code == StatusCode::OK {
        if is_streaming_response(&info.headers) {
            let status = create_streaming_response(&mut builder, &mut body).await?;

            builder.status(status).body(body)
        } else {
            let (status, body) = create_buffered_response(&mut builder, &mut body).await?;

            builder.status(status).body(body)
        }
    } else {
        builder.body(body)
    };

    response.map_err(ServerError::ResponseBuild)
}

async fn invoke_handler(
    State(state): State<RefRuntimeState>,
    Extension(cmd_tx): Extension<Sender<Action>>,
    Path(function_name): Path<String>,
    req: Request<Body>,
) -> Result<Response<Body>, ServerError> {
    tracing::debug!(%function_name, "invocation received");

    if function_name == DEFAULT_PACKAGE_FUNCTION && !state.is_default_function_enabled() {
        tracing::error!(available_functions = ?state.initial_functions, "the default function route is disabled, use /lambda-url/:function_name to trigger a function call");
        return respond_with_disabled_default_function(&state, true);
    }

    if function_name != DEFAULT_PACKAGE_FUNCTION {
        if let Err(binaries) = state.is_function_available(&function_name) {
            return respond_with_missing_function(&binaries);
        }
    }

    let resp = schedule_invocation(&cmd_tx, function_name, req).await?;
    let status_code = resp
        .extensions()
        .get::<StatusCode>()
        .cloned()
        .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);

    let (info, mut body) = resp.into_parts();

    let mut builder = Response::builder().status(status_code);

    if is_streaming_response(&info.headers) && status_code == StatusCode::OK {
        let status = create_streaming_response(&mut builder, &mut body).await?;
        builder = builder.status(status);
    }

    builder.body(body).map_err(ServerError::ResponseBuild)
}

async fn invoke_with_response_stream_handler(
    State(state): State<RefRuntimeState>,
    Extension(cmd_tx): Extension<Sender<Action>>,
    Path(function_name): Path<String>,
    req: Request<Body>,
) -> Result<Response<Body>, ServerError> {
    tracing::debug!(%function_name, "response streaming invocation received");

    if function_name == DEFAULT_PACKAGE_FUNCTION && !state.is_default_function_enabled() {
        tracing::error!(available_functions = ?state.initial_functions, "the default function route is disabled, use /lambda-url/:function_name to trigger a function call");
        return respond_with_disabled_default_function(&state, true);
    }

    if function_name != DEFAULT_PACKAGE_FUNCTION {
        if let Err(binaries) = state.is_function_available(&function_name) {
            return respond_with_missing_function(&binaries);
        }
    }

    let resp = schedule_invocation(&cmd_tx, function_name, req).await?;
    let status_code = resp
        .extensions()
        .get::<StatusCode>()
        .cloned()
        .unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);

    let (info, mut body) = resp.into_parts();

    let mut builder = Response::builder().status(status_code);

    // For response streaming invocations, we expect a streaming response
    if is_streaming_response(&info.headers) && status_code == StatusCode::OK {
        // Parse the streaming prelude to get metadata
        let prelude = body
            .frame()
            .await
            .ok_or(ServerError::MissingStreamingPrelude)?
            .map_err(ServerError::DataDeserialization)?
            .into_data()
            .map_err(|_| ServerError::StreamingPreludeDeserialization)?;

        let prelude: StreamingPrelude =
            serde_json::from_slice(&prelude).map_err(ServerError::SerializationError)?;

        // Skip the separator frame
        let _separator = body
            .frame()
            .await
            .ok_or(ServerError::MissingStreamingPrelude)?
            .map_err(ServerError::DataDeserialization)?;

        // Set response status from prelude
        builder = builder.status(prelude.status_code);

        // Transform the streaming body into EventStream format
        eventstream::create_eventstream_response(builder, &mut body).await
    } else {
        // Non-streaming responses or errors should return an error
        // For now, just return the body as-is with an error status
        builder
            .status(StatusCode::BAD_REQUEST)
            .body(Body::from("Function did not return a streaming response"))
            .map_err(ServerError::ResponseBuild)
    }
}

async fn schedule_invocation(
    cmd_tx: &Sender<Action>,
    function_name: String,
    mut req: Request<Body>,
) -> Result<LambdaResponse, ServerError> {
    let headers = req.headers_mut();

    let span = global::tracer("cargo-lambda/emulator").start("invoke request");
    let cx = Context::current_with_span(span);

    let mut injector = HashMap::new();
    global::get_text_map_propagator(|propagator| {
        propagator.inject_context(&cx, &mut injector);
    });
    let xray_header = injector
        .get(AWS_XRAY_TRACE_HEADER)
        .expect("x-amzn-trace-id header not injected by the propagator") // this is Infaliable
        .parse()
        .expect("x-amzn-trace-id header is not in the expected format"); // this is Infaliable
    headers.insert(LAMBDA_RUNTIME_XRAY_TRACE_HEADER, xray_header);

    let (resp_tx, resp_rx) = oneshot::channel::<LambdaResponse>();
    let function_name = if function_name.is_empty() {
        DEFAULT_PACKAGE_FUNCTION.into()
    } else {
        function_name
    };

    let req = InvokeRequest {
        function_name,
        req,
        resp_tx,
    };

    cmd_tx
        .send(Action::Invoke(req))
        .await
        .map_err(|e| ServerError::SendActionMessage(Box::new(e)))?;

    let resp = resp_rx.await.map_err(ServerError::ReceiveFunctionMessage)?;

    if let Some(status_code) = resp.extensions().get::<StatusCode>() {
        cx.span().add_event(
            "function call completed",
            vec![KeyValue::new("status", status_code.to_string())],
        );
    }

    Ok(resp)
}

fn extract_path_parameters(
    path: &str,
    method: &Method,
    state: &RefRuntimeState,
) -> (String, String, HashMap<String, String>) {
    let mut comp = path.split('/');
    comp.next(); // skip the first empty string

    if let (Some(prefix), Some(fun_name)) = (comp.next(), comp.next()) {
        if prefix == LAMBDA_URL_PREFIX {
            let l = format!("/{prefix}/{fun_name}");
            let mut new_path = path.replace(&l, "");
            if !new_path.starts_with('/') {
                new_path = format!("/{new_path}");
            }

            let f = if fun_name.is_empty() {
                DEFAULT_PACKAGE_FUNCTION.to_string()
            } else {
                fun_name.to_string()
            };
            return (f, new_path, HashMap::new());
        }
    }

    tracing::trace!(?state.function_router, "checking function router");
    if let Some(router) = &state.function_router {
        if let Ok((route, params)) = router.at(path, method.to_string().as_str()) {
            return (route.to_string(), path.to_string(), params);
        }
    }

    (
        DEFAULT_PACKAGE_FUNCTION.to_string(),
        path.to_string(),
        HashMap::new(),
    )
}

async fn create_streaming_response(
    builder: &mut Builder,
    body: &mut Body,
) -> Result<StatusCode, ServerError> {
    let prelude = body
        .frame()
        .await
        .ok_or(ServerError::MissingStreamingPrelude)?
        .map_err(ServerError::DataDeserialization)?
        .into_data()
        .map_err(|_| ServerError::StreamingPreludeDeserialization)?;

    let prelude: StreamingPrelude =
        serde_json::from_slice(&prelude).map_err(ServerError::SerializationError)?;

    let _separator = body
        .frame()
        .await
        .ok_or(ServerError::MissingStreamingPrelude)?
        .map_err(ServerError::DataDeserialization)?;

    if let Some(headers) = builder.headers_mut() {
        headers.extend(prelude.headers);
        if let Some(content_length) = headers.remove("content-length") {
            headers.insert("x-amzn-remapped-content-length", content_length);
        }

        prelude.cookies.iter().try_for_each(|cookie| {
            let header_value =
                HeaderValue::try_from(cookie).map_err(|e| ServerError::ResponseBuild(e.into()))?;
            headers.append(header::SET_COOKIE, header_value);
            Ok::<(), ServerError>(())
        })?;

        headers.insert("transfer-encoding", HeaderValue::from_static("chunked"));
        headers.insert(
            "lambda-runtime-function-response-mode",
            HeaderValue::from_static("streaming"),
        );
    }

    Ok(prelude.status_code)
}

fn is_streaming_response(headers: &HeaderMap) -> bool {
    let Some(_streaming) = headers
        .get("lambda-runtime-function-response-mode")
        .map(|v| v == "streaming")
    else {
        return false;
    };

    headers
        .get("transfer-encoding")
        .map(|v| v == "chunked")
        .unwrap_or_default()
}

async fn create_buffered_response(
    builder: &mut Builder,
    body: &mut Body,
) -> Result<(StatusCode, Body), ServerError> {
    let body = body
        .collect()
        .await
        .map_err(ServerError::DataDeserialization)?
        .to_bytes();
    let resp_event: ApiGatewayV2httpResponse =
        serde_json::from_slice(&body).map_err(ServerError::SerializationError)?;

    let is_base64_encoded = resp_event.is_base64_encoded;
    let resp_body = match resp_event.body.unwrap_or(LambdaBody::Empty) {
        LambdaBody::Empty => Body::empty(),
        b if is_base64_encoded => Body::from(
            b64::STANDARD
                .decode(b.as_ref())
                .map_err(ServerError::BodyDecodeError)?,
        ),
        LambdaBody::Text(s) => Body::from(s),
        LambdaBody::Binary(b) => Body::from(b),
    };
    if let Some(headers) = builder.headers_mut() {
        headers.extend(resp_event.headers);
        headers.extend(resp_event.multi_value_headers);

        resp_event.cookies.iter().try_for_each(|cookie| {
            let header_value =
                HeaderValue::try_from(cookie).map_err(|e| ServerError::ResponseBuild(e.into()))?;
            headers.append(header::SET_COOKIE, header_value);
            Ok::<(), ServerError>(())
        })?;
    }

    let status: StatusCode = StatusCode::from_u16(resp_event.status_code as u16)
        .map_err(ServerError::InvalidStatusCode)?;

    Ok((status, resp_body))
}

#[allow(clippy::result_large_err)]
fn respond_with_disabled_default_function(
    state: &RefRuntimeState,
    invoke_call: bool,
) -> Result<Response<Body>, ServerError> {
    let detail = if invoke_call {
        "the default function route is disabled. To trigger a function call, add the name of a function as the invoke argument"
    } else {
        "the default function route is disabled, use /lambda-url/:function_name to trigger a function call"
    };
    tracing::error!(available_functions = ?state.initial_functions, detail);

    let body = Body::from(
        serde_json::json!({
            "title": "Default function disabled",
            "detail": format!("{}. Available functions: {:?}", detail, state.initial_functions),
        })
        .to_string(),
    );
    Response::builder()
        .status(StatusCode::NOT_FOUND)
        .body(body)
        .map_err(ServerError::ResponseBuild)
}

#[allow(clippy::result_large_err)]
fn respond_with_missing_function(
    binaries: &HashSet<String>,
) -> Result<Response<Body>, ServerError> {
    let detail = "that function doesn't exist as a binary in your project";
    tracing::error!(available_functions = ?binaries, detail);

    let body = Body::from(
        serde_json::json!({
            "title": "Missing function",
            "detail": format!("{}. Available functions: {:?}", detail, binaries),
        })
        .to_string(),
    );
    Response::builder()
        .status(StatusCode::NOT_FOUND)
        .body(body)
        .map_err(ServerError::ResponseBuild)
}

#[cfg(test)]
mod test {
    use std::{
        collections::HashSet,
        net::{IpAddr, Ipv4Addr, SocketAddr},
        path::PathBuf,
        sync::Arc,
    };

    use crate::RuntimeState;

    use super::extract_path_parameters;
    use cargo_lambda_metadata::{
        DEFAULT_PACKAGE_FUNCTION,
        cargo::{
            load_metadata,
            watch::{FunctionRouter, FunctionRoutes},
        },
        config::{ConfigOptions, load_config_without_cli_flags},
    };
    use http::Method;

    #[test]
    fn test_extract_path_parameters() {
        let state = Arc::new(RuntimeState::new(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0),
            None,
            PathBuf::new(),
            false,
            HashSet::new(),
            None,
            1,
        ));

        let (func, path, _) = extract_path_parameters("", &Method::GET, &state);
        assert_eq!(DEFAULT_PACKAGE_FUNCTION, func);
        assert_eq!("", path);

        let (func, path, _) = extract_path_parameters("/", &Method::GET, &state);
        assert_eq!(DEFAULT_PACKAGE_FUNCTION, func);
        assert_eq!("/", path);

        let (func, path, _) = extract_path_parameters("/foo", &Method::GET, &state);
        assert_eq!(DEFAULT_PACKAGE_FUNCTION, func);
        assert_eq!("/foo", path);

        let (func, path, _) = extract_path_parameters("/foo/", &Method::GET, &state);
        assert_eq!(DEFAULT_PACKAGE_FUNCTION, func);
        assert_eq!("/foo/", path);

        let (func, path, _) =
            extract_path_parameters("/lambda-url/func-name", &Method::GET, &state);
        assert_eq!("func-name", func);
        assert_eq!("/", path);

        let (func, path, _) =
            extract_path_parameters("/lambda-url/func-name/", &Method::GET, &state);
        assert_eq!("func-name", func);
        assert_eq!("/", path);

        let (func, path, _) =
            extract_path_parameters("/lambda-url/func-name/foo", &Method::GET, &state);
        assert_eq!("func-name", func);
        assert_eq!("/foo", path);

        let (func, path, _) =
            extract_path_parameters("/lambda-url/func-name/foo/", &Method::GET, &state);
        assert_eq!("func-name", func);
        assert_eq!("/foo/", path);

        let mut new_router = FunctionRouter::default();
        new_router
            .insert("/foo", FunctionRoutes::Single("bar".to_string()))
            .unwrap();
        let state = Arc::new(RuntimeState::new(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0),
            None,
            PathBuf::new(),
            false,
            HashSet::new(),
            Some(new_router),
            1,
        ));

        let (func, path, _) = extract_path_parameters("/foo", &Method::GET, &state);
        assert_eq!("bar", func);
        assert_eq!("/foo", path);
    }

    #[test]
    fn test_extract_path_parameters_with_router_params() {
        let mut new_router = FunctionRouter::default();
        new_router
            .insert(
                "/users/{user_id}/posts/{post_id}",
                FunctionRoutes::Single("user-posts".to_string()),
            )
            .unwrap();

        let state = Arc::new(RuntimeState::new(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0),
            None,
            PathBuf::new(),
            false,
            HashSet::new(),
            Some(new_router),
            1,
        ));

        // Test with path parameters
        let (func, path, params) =
            extract_path_parameters("/users/123/posts/456", &Method::GET, &state);
        assert_eq!("user-posts", func);
        assert_eq!("/users/123/posts/456", path);
        assert_eq!(params.get("user_id").unwrap(), "123");
        assert_eq!(params.get("post_id").unwrap(), "456");

        // Test with non-matching path
        let (func, path, params) = extract_path_parameters("/invalid/path", &Method::GET, &state);
        assert_eq!(DEFAULT_PACKAGE_FUNCTION, func);
        assert_eq!("/invalid/path", path);
        assert!(params.is_empty());
    }

    #[test]
    fn test_extract_path_parameters_with_router_params_and_method() {
        let metadata =
            load_metadata("../../tests/fixtures/workspace-package/Cargo.toml", None).unwrap();
        let config = load_config_without_cli_flags(&metadata, &ConfigOptions::default()).unwrap();

        let state = Arc::new(RuntimeState::new(
            SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 0),
            None,
            PathBuf::new(),
            false,
            HashSet::new(),
            config.watch.router,
            1,
        ));

        // Test with path parameters and method
        let (func, path, params) =
            extract_path_parameters("/users/123/posts/456", &Method::GET, &state);
        assert_eq!("crate-3", func);
        assert_eq!("/users/123/posts/456", path);
        assert_eq!(params.get("user_id").unwrap(), "123");
        assert_eq!(params.get("post_id").unwrap(), "456");

        // Test with non-matching path and method
        let (func, path, params) =
            extract_path_parameters("/orgs/123/posts/456", &Method::POST, &state);
        assert_eq!(DEFAULT_PACKAGE_FUNCTION, func);
        assert_eq!("/orgs/123/posts/456", path);
        assert!(params.is_empty());
    }
}