Skip to content

Commit

Permalink
feat(web): Remove enable api
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto committed Oct 9, 2024
1 parent be4e0e7 commit 868b02f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
4 changes: 4 additions & 0 deletions tonic-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ const DEFAULT_ALLOW_HEADERS: [HeaderName; 4] = [
/// Enable a tonic service to handle grpc-web requests with the default configuration.
///
/// You can customize the CORS configuration composing the [`GrpcWebLayer`] with the cors layer of your choice.
#[deprecated(
since = "0.12.4",
note = "compose the `GrpcWebLayer` with the cors layer of your choice"
)]
pub fn enable<S>(service: S) -> CorsGrpcWeb<S>
where
S: Service<http::Request<BoxBody>, Response = http::Response<BoxBody>>,
Expand Down
32 changes: 22 additions & 10 deletions tonic-web/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ mod tests {
use http::header::{
ACCESS_CONTROL_REQUEST_HEADERS, ACCESS_CONTROL_REQUEST_METHOD, CONTENT_TYPE, ORIGIN,
};
use tower_layer::Layer as _;

type BoxFuture<T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send>>;

Expand All @@ -255,6 +256,17 @@ mod tests {
const NAME: &'static str = "test";
}

fn enable<S>(service: S) -> tower_http::cors::Cors<GrpcWebService<S>>
where
S: Service<http::Request<BoxBody>, Response = http::Response<BoxBody>>,
{
tower_layer::Stack::new(
crate::GrpcWebLayer::new(),
tower_http::cors::CorsLayer::new(),
)
.layer(service)
}

mod grpc_web {
use super::*;
use tower_layer::Layer;
Expand All @@ -270,7 +282,7 @@ mod tests {

#[tokio::test]
async fn default_cors_config() {
let mut svc = crate::enable(Svc);
let mut svc = enable(Svc);
let res = svc.call(request()).await.unwrap();

assert_eq!(res.status(), StatusCode::OK);
Expand All @@ -286,7 +298,7 @@ mod tests {

#[tokio::test]
async fn without_origin() {
let mut svc = crate::enable(Svc);
let mut svc = enable(Svc);

let mut req = request();
req.headers_mut().remove(ORIGIN);
Expand All @@ -298,7 +310,7 @@ mod tests {

#[tokio::test]
async fn only_post_and_options_allowed() {
let mut svc = crate::enable(Svc);
let mut svc = enable(Svc);

for method in &[
Method::GET,
Expand All @@ -323,7 +335,7 @@ mod tests {

#[tokio::test]
async fn grpc_web_content_types() {
let mut svc = crate::enable(Svc);
let mut svc = enable(Svc);

for ct in &[GRPC_WEB_TEXT, GRPC_WEB_PROTO, GRPC_WEB_TEXT_PROTO, GRPC_WEB] {
let mut req = request();
Expand Down Expand Up @@ -352,7 +364,7 @@ mod tests {

#[tokio::test]
async fn valid_grpc_web_preflight() {
let mut svc = crate::enable(Svc);
let mut svc = enable(Svc);
let res = svc.call(request()).await.unwrap();

assert_eq!(res.status(), StatusCode::OK);
Expand All @@ -372,7 +384,7 @@ mod tests {

#[tokio::test]
async fn h2_is_ok() {
let mut svc = crate::enable(Svc);
let mut svc = enable(Svc);

let req = request();
let res = svc.call(req).await.unwrap();
Expand All @@ -382,7 +394,7 @@ mod tests {

#[tokio::test]
async fn h1_is_err() {
let mut svc = crate::enable(Svc);
let mut svc = enable(Svc);

let req = Request::builder()
.header(CONTENT_TYPE, GRPC_CONTENT_TYPE)
Expand All @@ -395,7 +407,7 @@ mod tests {

#[tokio::test]
async fn content_type_variants() {
let mut svc = crate::enable(Svc);
let mut svc = enable(Svc);

for variant in &["grpc", "grpc+proto", "grpc+thrift", "grpc+foo"] {
let mut req = request();
Expand Down Expand Up @@ -423,15 +435,15 @@ mod tests {

#[tokio::test]
async fn h1_is_err() {
let mut svc = crate::enable(Svc);
let mut svc = enable(Svc);
let res = svc.call(request()).await.unwrap();

assert_eq!(res.status(), StatusCode::BAD_REQUEST)
}

#[tokio::test]
async fn h2_is_ok() {
let mut svc = crate::enable(Svc);
let mut svc = enable(Svc);
let mut req = request();
*req.version_mut() = Version::HTTP_2;

Expand Down

0 comments on commit 868b02f

Please sign in to comment.