@@ -15,7 +15,7 @@ use tokio_stream::wrappers::ReceiverStream;
15
15
use tonic:: body:: BoxBody ;
16
16
use tracing:: debug;
17
17
18
- use crate :: { headers :: Headers , mock_set:: MockSet , request:: Request } ;
18
+ use crate :: { mock_set:: MockSet , request:: Request } ;
19
19
20
20
/// Mock gRPC service.
21
21
#[ derive( Debug , Clone ) ]
@@ -38,10 +38,27 @@ impl Service<http::Request<Incoming>> for GrpcMockService {
38
38
let mocks = self . mocks . clone ( ) ;
39
39
let fut = async move {
40
40
debug ! ( ?req, "handling request" ) ;
41
- let headers: Headers = req. headers ( ) . into ( ) ;
42
- if !headers. has_content_type ( "application/grpc" ) {
43
- return Ok ( invalid_content_type_response ( ) ) ;
41
+
42
+ if req. method ( ) != http:: Method :: POST {
43
+ return Ok ( http:: Response :: builder ( )
44
+ . status ( http:: StatusCode :: METHOD_NOT_ALLOWED )
45
+ . header ( "Allow" , "POST" )
46
+ . body ( tonic:: body:: empty_body ( ) )
47
+ . unwrap ( ) ) ;
48
+ }
49
+ let content_type = req. headers ( ) . get ( "content-type" ) ;
50
+ if !content_type. is_some_and ( |v| {
51
+ v. to_str ( )
52
+ . unwrap_or_default ( )
53
+ . starts_with ( "application/grpc" )
54
+ } ) {
55
+ return Ok ( http:: Response :: builder ( )
56
+ . status ( http:: StatusCode :: UNSUPPORTED_MEDIA_TYPE )
57
+ . header ( "Accept-Post" , "application/grpc" )
58
+ . body ( tonic:: body:: empty_body ( ) )
59
+ . unwrap ( ) ) ;
44
60
}
61
+
45
62
let ( parts, body) = req. into_parts ( ) ;
46
63
let mut stream = body. into_data_stream ( ) ;
47
64
@@ -106,18 +123,6 @@ impl Service<http::Request<Incoming>> for GrpcMockService {
106
123
}
107
124
}
108
125
109
- fn invalid_content_type_response ( ) -> http:: Response < BoxBody > {
110
- http:: Response :: builder ( )
111
- . header ( "content-type" , "application/grpc" )
112
- . header ( "grpc-status" , tonic:: Code :: InvalidArgument as i32 )
113
- . header (
114
- "grpc-message" ,
115
- "invalid content-type: expected `application/grpc`" ,
116
- )
117
- . body ( tonic:: body:: empty_body ( ) )
118
- . unwrap ( )
119
- }
120
-
121
126
fn mock_not_found_trailer ( ) -> HeaderMap {
122
127
let mut headers = HeaderMap :: new ( ) ;
123
128
headers. insert ( "grpc-status" , ( tonic:: Code :: NotFound as i32 ) . into ( ) ) ;
0 commit comments