Skip to content

Commit

Permalink
add basic conversion tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Dec 4, 2024
1 parent afdb13e commit 59324b0
Showing 1 changed file with 104 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/core/generator/proto/connect_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,107 @@ impl From<Grpc> for Http {
}
}
}

#[cfg(test)]
mod tests {
use serde_json::json;

use super::*;
use crate::core::config::KeyValue;

#[test]
fn test_grpc_to_http_basic_conversion() {
let grpc = Grpc {
url: "http://localhost:8080".to_string(),
method: "package.service.method".to_string(),
body: Some(json!({"key": "value"})),
headers: Default::default(),
batch_key: Default::default(),
dedupe: Default::default(),
select: Default::default(),
on_response_body: Default::default(),
};

let http = Http::from(grpc);

assert_eq!(http.url, "http://localhost:8080/package.service/method");
assert_eq!(http.method, crate::core::http::Method::POST);
assert_eq!(http.body, Some(r#"{"key":"value"}"#.to_string()));
}

#[test]
fn test_grpc_to_http_empty_body() {
let grpc = Grpc {
url: "http://localhost:8080".to_string(),
method: "package.service.method".to_string(),
body: Default::default(),
headers: Default::default(),
batch_key: Default::default(),
dedupe: Default::default(),
select: Default::default(),
on_response_body: Default::default(),
};

let http = Http::from(grpc);

assert_eq!(http.body, Some("{}".to_string()));
}

#[test]
fn test_grpc_to_http_with_headers() {
let grpc = Grpc {
url: "http://localhost:8080".to_string(),
method: "a.b.c".to_string(),
body: None,
headers: vec![KeyValue { key: "X-Foo".to_string(), value: "bar".to_string() }],
batch_key: Default::default(),
dedupe: Default::default(),
select: Default::default(),
on_response_body: Default::default(),
};

let http = Http::from(grpc);

assert_eq!(http.url, "http://localhost:8080/a.b/c");
assert_eq!(
http.headers
.iter()
.find(|h| h.key == "X-Foo")
.unwrap()
.value,
"bar".to_string()
);
}

#[test]
fn test_grpc_to_http_all_fields() {
let grpc = Grpc {
url: "http://localhost:8080".to_string(),
method: "package.service.method".to_string(),
body: Some(json!({"key": "value"})),
headers: vec![KeyValue { key: "X-Foo".to_string(), value: "bar".to_string() }],
batch_key: Some("batch_key_value".to_string()),

Check failure on line 148 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

mismatched types

Check failure on line 148 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

mismatched types

Check failure on line 148 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

mismatched types

Check failure on line 148 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

mismatched types

Check failure on line 148 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

mismatched types

Check failure on line 148 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

mismatched types

Check failure on line 148 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

mismatched types

Check failure on line 148 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

mismatched types

Check failure on line 148 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

mismatched types
dedupe: Some(true),
select: Some("select_value".to_string()),

Check failure on line 150 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

mismatched types

Check failure on line 150 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

mismatched types

Check failure on line 150 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

mismatched types

Check failure on line 150 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

mismatched types

Check failure on line 150 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

mismatched types

Check failure on line 150 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

mismatched types

Check failure on line 150 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

mismatched types

Check failure on line 150 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

mismatched types

Check failure on line 150 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

mismatched types
on_response_body: Some("on_response_body_value".to_string()),
};

let http = Http::from(grpc);

assert_eq!(http.url, "http://localhost:8080/package.service/method");
assert_eq!(http.method, crate::core::http::Method::POST);
assert_eq!(http.body, Some(r#"{"key":"value"}"#.to_string()));
assert_eq!(
http.headers
.iter()
.find(|h| h.key == "X-Foo")
.unwrap()
.value,
"bar".to_string()
);
assert_eq!(http.batch_key, Some("batch_key_value".to_string()));

Check warning on line 167 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

Diff in /home/runner/work/tailcall/tailcall/src/core/generator/proto/connect_rpc.rs

Check failure on line 167 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

can't compare `std::vec::Vec<std::string::String>` with `std::option::Option<std::string::String>`

Check failure on line 167 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

can't compare `std::vec::Vec<std::string::String>` with `std::option::Option<std::string::String>`

Check failure on line 167 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

can't compare `std::vec::Vec<std::string::String>` with `std::option::Option<std::string::String>`

Check failure on line 167 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

can't compare `std::vec::Vec<std::string::String>` with `std::option::Option<std::string::String>`

Check failure on line 167 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

can't compare `std::vec::Vec<std::string::String>` with `std::option::Option<std::string::String>`

Check failure on line 167 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

can't compare `std::vec::Vec<std::string::String>` with `std::option::Option<std::string::String>`

Check failure on line 167 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

can't compare `std::vec::Vec<std::string::String>` with `std::option::Option<std::string::String>`

Check failure on line 167 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

can't compare `std::vec::Vec<std::string::String>` with `std::option::Option<std::string::String>`

Check failure on line 167 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

can't compare `std::vec::Vec<std::string::String>` with `std::option::Option<std::string::String>`
assert_eq!(http.dedupe, Some(true));
assert_eq!(http.select, Some("select_value".to_string()));

Check failure on line 169 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Formatter and Lint Check

mismatched types

Check failure on line 169 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-arm64

mismatched types

Check failure on line 169 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on darwin-x64

mismatched types

Check failure on line 169 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-musl

mismatched types

Check failure on line 169 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-ia32-gnu

mismatched types

Check failure on line 169 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-arm64-gnu

mismatched types

Check failure on line 169 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on linux-x64-musl

mismatched types

Check failure on line 169 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-x64-msvc

mismatched types

Check failure on line 169 in src/core/generator/proto/connect_rpc.rs

View workflow job for this annotation

GitHub Actions / Run Tests on win32-ia32-msvc

mismatched types
assert_eq!(http.on_response_body, Some("on_response_body_value".to_string()));
}
}

0 comments on commit 59324b0

Please sign in to comment.