|
1 | 1 | use crate::protobuf::raw_to_proto::new_empty_descriptor; |
2 | | -use crate::protobuf::{proto_to_yaml, raw_to_proto, reencode, yaml_to_pretty}; |
| 2 | +use crate::protobuf::{ |
| 3 | + existing_proto_definitions, proto_to_yaml, raw_to_proto, reencode, yaml_to_pretty, |
| 4 | +}; |
3 | 5 | use crate::{Metadata, Prettify, Reencode}; |
4 | 6 | use anyhow::{Context, Result}; |
5 | 7 | use mitmproxy_highlight::Language; |
@@ -35,9 +37,10 @@ impl Prettify for Protobuf { |
35 | 37 | Language::Yaml |
36 | 38 | } |
37 | 39 |
|
38 | | - fn prettify(&self, data: &[u8], _metadata: &dyn Metadata) -> Result<String> { |
39 | | - // FIXME use new create_new |
40 | | - self.prettify_with_descriptor(data, &new_empty_descriptor(None, "Unknown"), &[]) |
| 40 | + fn prettify(&self, data: &[u8], metadata: &dyn Metadata) -> Result<String> { |
| 41 | + let (descriptor, dependencies) = existing_proto_definitions::find_best_match(metadata)? |
| 42 | + .unwrap_or_else(|| (new_empty_descriptor(None, "Unknown"), vec![])); |
| 43 | + self.prettify_with_descriptor(data, &descriptor, &dependencies) |
41 | 44 | } |
42 | 45 |
|
43 | 46 | fn render_priority(&self, _data: &[u8], metadata: &dyn Metadata) -> f64 { |
@@ -211,4 +214,24 @@ mod tests { |
211 | 214 | let result = Protobuf.prettify(b"", &TestMetadata::default()).unwrap(); |
212 | 215 | assert_eq!(result, "{} # empty protobuf message"); |
213 | 216 | } |
| 217 | + |
| 218 | + #[test] |
| 219 | + fn test_existing() { |
| 220 | + let metadata = TestMetadata::default().with_protobuf_definitions(concat!( |
| 221 | + env!("CARGO_MANIFEST_DIR"), |
| 222 | + "/testdata/protobuf/simple.proto" |
| 223 | + )); |
| 224 | + let result = Protobuf.prettify(varint::PROTO, &metadata).unwrap(); |
| 225 | + assert_eq!(result, "example: 150\n"); |
| 226 | + } |
| 227 | + |
| 228 | + #[test] |
| 229 | + fn test_existing_mismatch() { |
| 230 | + let metadata = TestMetadata::default().with_protobuf_definitions(concat!( |
| 231 | + env!("CARGO_MANIFEST_DIR"), |
| 232 | + "/testdata/protobuf/simple.proto" |
| 233 | + )); |
| 234 | + let result = Protobuf.prettify(string::PROTO, &metadata); |
| 235 | + assert!(result.is_err()); |
| 236 | + } |
214 | 237 | } |
0 commit comments