Skip to content

Commit 8749adb

Browse files
committed
grpc tests + cleanup
1 parent 073f3bb commit 8749adb

File tree

8 files changed

+65
-16
lines changed

8 files changed

+65
-16
lines changed

mitmproxy-contentviews/benches/contentviews.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use criterion::{black_box, criterion_group, criterion_main, Criterion};
2-
use mitmproxy_contentviews::{MsgPack, Prettify, Protobuf, Reencode, TestMetadata};
2+
use mitmproxy_contentviews::{test::TestMetadata, MsgPack, Prettify, Protobuf, Reencode};
33

44
fn criterion_benchmark(c: &mut Criterion) {
55
c.bench_function("protobuf-prettify", |b| {

mitmproxy-contentviews/src/grpc.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,36 @@ impl Reencode for GRPC {
7171

7272
#[cfg(test)]
7373
mod tests {
74+
use super::*;
75+
use crate::test::TestMetadata;
76+
77+
const TEST_YAML: &str = "1: 150\n\n---\n\n1: 150\n";
78+
const TEST_GRPC: &[u8] = &[
79+
0, 0, 0, 0, 3, 8, 150, 1, // first message
80+
0, 0, 0, 0, 3, 8, 150, 1, // second message
81+
];
82+
83+
#[test]
84+
fn test_empty() {
85+
let res = GRPC.prettify(&vec![], &TestMetadata::default()).unwrap();
86+
assert_eq!(res, "");
87+
}
88+
89+
#[test]
90+
fn test_prettify_two_messages() {
91+
let res = GRPC.prettify(TEST_GRPC, &TestMetadata::default()).unwrap();
92+
assert_eq!(res, TEST_YAML);
93+
}
94+
95+
#[test]
96+
fn test_reencode_two_messages() {
97+
let res = GRPC.reencode(TEST_YAML, &TestMetadata::default()).unwrap();
98+
assert_eq!(res, TEST_GRPC);
99+
}
74100

75101
#[test]
76-
fn test_grpc() {
77-
// FIXME
102+
fn test_render_priority() {
103+
assert_eq!(GRPC.render_priority(b"", &TestMetadata::default().with_content_type("application/grpc")), 1.0);
104+
assert_eq!(GRPC.render_priority(b"", &TestMetadata::default().with_content_type("text/plain")), 0.0);
78105
}
79106
}

mitmproxy-contentviews/src/hex_dump.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl Prettify for HexDump {
3636
#[cfg(test)]
3737
mod tests {
3838
use super::*;
39-
use crate::TestMetadata;
39+
use crate::test::TestMetadata;
4040

4141
#[test]
4242
fn prettify_simple() {

mitmproxy-contentviews/src/hex_stream.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Reencode for HexStream {
5050
#[cfg(test)]
5151
mod tests {
5252
use super::*;
53-
use crate::TestMetadata;
53+
use crate::test::TestMetadata;
5454

5555
#[test]
5656
fn test_hex_stream() {

mitmproxy-contentviews/src/lib.rs

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,31 @@ use anyhow::Result;
1414
use mitmproxy_highlight::Language;
1515

1616
pub trait Metadata {
17+
/// The HTTP `content-type` of this message.
1718
fn content_type(&self) -> Option<&str>;
1819
}
1920

21+
/// See https://docs.mitmproxy.org/dev/api/mitmproxy/contentviews.html
22+
/// for API details.
2023
pub trait Prettify: Send + Sync {
24+
/// The name for this contentview, e.g. `gRPC` or `Protobuf`.
25+
/// Favor brevity.
2126
fn name(&self) -> &str;
2227

2328
fn instance_name(&self) -> String {
2429
self.name().to_lowercase().replace(" ", "_")
2530
}
2631

32+
/// The syntax highlighting that should be applied to the prettified output.
33+
/// This is useful for contentviews that prettify to JSON or YAML.
2734
fn syntax_highlight(&self) -> Language {
2835
Language::None
2936
}
3037

38+
/// Pretty-print `data`.
3139
fn prettify(&self, data: &[u8], metadata: &dyn Metadata) -> Result<String>;
3240

41+
/// Render priority - typically a float between 0 and 1 for builtin views.
3342
#[allow(unused_variables)]
3443
fn render_priority(&self, data: &[u8], metadata: &dyn Metadata) -> f64 {
3544
0.0
@@ -40,13 +49,27 @@ pub trait Reencode: Send + Sync {
4049
fn reencode(&self, data: &str, metadata: &dyn Metadata) -> Result<Vec<u8>>;
4150
}
4251

43-
#[derive(Default)]
44-
pub struct TestMetadata {
45-
pub content_type: Option<String>,
46-
}
52+
// no cfg(test) gate because it's used in benchmarks as well
53+
pub mod test {
54+
use crate::Metadata;
55+
56+
#[derive(Default)]
57+
pub struct TestMetadata {
58+
pub content_type: Option<String>,
59+
}
4760

48-
impl Metadata for TestMetadata {
49-
fn content_type(&self) -> Option<&str> {
50-
self.content_type.as_deref()
61+
impl TestMetadata {
62+
pub fn with_content_type(mut self, content_type: &str) -> Self {
63+
self.content_type = Some(content_type.to_string());
64+
self
65+
}
5166
}
67+
68+
impl Metadata for TestMetadata {
69+
fn content_type(&self) -> Option<&str> {
70+
self.content_type.as_deref()
71+
}
72+
}
73+
74+
5275
}

mitmproxy-contentviews/src/msgpack.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl Reencode for MsgPack {
4848
#[cfg(test)]
4949
mod tests {
5050
use super::*;
51-
use crate::TestMetadata;
51+
use crate::test::TestMetadata;
5252

5353
// Hardcoded MsgPack data for a simple object:
5454
// {

mitmproxy-contentviews/src/protobuf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ pub(super) mod reencode {
538538
#[cfg(test)]
539539
mod tests {
540540
use super::*;
541-
use crate::TestMetadata;
541+
use crate::test::TestMetadata;
542542

543543
macro_rules! test_roundtrip {
544544
($name:ident,$proto:literal,$yaml:literal) => {

mitmproxy-rs/src/contentview.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ impl Metadata for PythonMetadata<'_> {
2626
.extract::<String>()
2727
.ok()
2828
})
29-
.as_ref()
30-
.map(|ct| ct.as_str())
29+
.as_deref()
3130
}
3231
}
3332

0 commit comments

Comments
 (0)