Skip to content

Unit testing Headers and Cognito Authorizer #321

@leo-suspensify

Description

@leo-suspensify

Hi,

What is the recommended flow write unit tests involving headers and cognito authorization? The objective is to write unit tests which mimic frontend requests passing through the http api gateway. For example, suppose we have the function

pub async fn http_404(_request: Request, _context: Context) -> Result<Response<Body>, Error> {
    let body = json!({
        "message": "The requested resource or route do not exist.",
    });

    let response = Response::builder()
        .status(404)
        .header("content-type", "application/json")
        .body(Body::from(serde_json::to_string(&body)?))
        .unwrap();

    Ok(response)
}

#[tokio::test]
async fn get_auth_uri() {
    let global_settings = setup().await;

    let expected = json!({
        "message": "The requested resource or route do not exist.".to_string(),
    }).into_response();

    let request = Request::new(lambda_http::Body::Text("{}"));
    let response = handle_get_auth_uri(request, Context::default())
        .await
        .expect("expected Ok(_) value")
        .into_response();
    assert_eq!(response.body(), expected.body())
}

We can set a few things beside the body, such as query parameters with request.with_query_string_parameters but I don't see how to set headers or Cognito related parameters. This issue mentions that the CognitoIdentity parameters are deprecated and cannot be used in Context, but there also isn't a clear way to set the RequestContext for mocking. What is the recommended way to do this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions