tonic icon indicating copy to clipboard operation
tonic copied to clipboard

Inserting metadata in a test panics with index out of bounds

Open esiebert opened this issue 1 year ago • 2 comments

Bug Report

Version

> cargo tree | grep tonic
├── tonic v0.11.0
├── tonic-reflection v0.11.0
│   └── tonic v0.11.0 (*)
├── tonic-types v0.11.0
│   └── tonic v0.11.0 (*)
└── tonic-build v0.11.0

Description

When creating a simple test case for the authentication interceptor example:

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_check_auth_valid() {
        let mut req = Request::new(());
        let token: MetadataValue<_> = "Bearer some-secret-token".parse().unwrap();
        req.metadata_mut().insert("Authorization", token);

        let result = check_auth(req);

        assert!(result.is_ok());
    }
}

The test panics with the following message when inserting the metadata value:

---- actor::grpc::tests::test_check_auth_valid stdout ----
thread 'actor::grpc::tests::test_check_auth_valid' panicked at /Users/eee/.cargo/registry/src/index.crates.io-6f17d22bba15001f/http-0.2.12/src/header/name.rs:1270:13:
index out of bounds: the len is 0 but the index is 0

The panic disappears if the insert command is removed, but obviously fails because it depends on it.

I'm failing to understand how insert is panicking here.

esiebert avatar Jul 09 '24 10:07 esiebert

Same happening here. +1

Stay1444 avatar Jul 13 '24 15:07 Stay1444

It is hard to follow, but this is happening because internally insert() is calling HeaderName::from_static which will panic for invalid header names. It expects all lower-case header names to comply with the HTTP/2 header spec.

alexrudy avatar Jul 25 '24 16:07 alexrudy