|
| 1 | +use crate::test::TestMetadata; |
| 2 | +use crate::{Metadata, Prettify}; |
| 3 | +use anyhow::Context; |
| 4 | +use std::collections::HashMap; |
| 5 | +use std::path::Path; |
| 6 | + |
| 7 | +/// Contentview used for internal testing to ensure that the |
| 8 | +/// Python accessors in mitmproxy-rs all work properly. |
| 9 | +pub struct TestInspectMetadata; |
| 10 | + |
| 11 | +impl Prettify for TestInspectMetadata { |
| 12 | + fn name(&self) -> &'static str { |
| 13 | + "Inspect Metadata (test only)" |
| 14 | + } |
| 15 | + |
| 16 | + fn instance_name(&self) -> String { |
| 17 | + "_test_inspect_metadata".to_string() |
| 18 | + } |
| 19 | + |
| 20 | + fn prettify(&self, _data: &[u8], metadata: &dyn Metadata) -> anyhow::Result<String> { |
| 21 | + let mut headers = HashMap::new(); |
| 22 | + if let Some(host) = metadata.get_header("host") { |
| 23 | + headers.insert("host".to_string(), host); |
| 24 | + } |
| 25 | + let meta = TestMetadata { |
| 26 | + content_type: metadata.content_type().map(str::to_string), |
| 27 | + headers, |
| 28 | + path: metadata.get_path().map(str::to_string), |
| 29 | + is_http_request: metadata.is_http_request(), |
| 30 | + protobuf_definitions: metadata.protobuf_definitions().map(Path::to_path_buf), |
| 31 | + }; |
| 32 | + // JSON would be nicer to consume on the Python side, |
| 33 | + // but let's not add dependencies for this. |
| 34 | + serde_yaml::to_string(&meta).context("Failed to convert to YAML") |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +#[cfg(test)] |
| 39 | +mod tests { |
| 40 | + use super::*; |
| 41 | + |
| 42 | + #[test] |
| 43 | + fn prettify_simple() { |
| 44 | + let result = TestInspectMetadata |
| 45 | + .prettify(b"", &TestMetadata::default()) |
| 46 | + .unwrap(); |
| 47 | + assert_eq!( |
| 48 | + result, |
| 49 | + "content_type: null\nheaders: {}\nprotobuf_definitions: null\npath: null\nis_http_request: false\n" |
| 50 | + ); |
| 51 | + } |
| 52 | +} |
0 commit comments