grpc / grpc/grpc-rust

Inserting metadata in a test panics with index out of bounds

Open
#1,782 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
12.5k
Forks
1.3k
Avg merge
4d 7h
Merged PRs (30d)
24

Description

## 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](https://github.com/hyperium/tonic/blob/master/examples/src/authentication/server.rs#L33):
```rust
#[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 `insert`ing 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.