@@ -6,26 +6,52 @@ mod pb {
6
6
mod tests {
7
7
use std:: time:: Duration ;
8
8
9
- use futures:: { StreamExt , stream } ;
9
+ use futures:: { stream , StreamExt } ;
10
10
use mocktail:: prelude:: * ;
11
11
use tokio_stream:: wrappers:: ReceiverStream ;
12
12
use tonic:: transport:: Channel ;
13
13
use tracing:: debug;
14
14
15
- use super :: pb:: { HelloRequest , HelloResponse , hello_client:: HelloClient } ;
15
+ use super :: pb:: { hello_client:: HelloClient , HelloRequest , HelloResponse } ;
16
16
17
17
#[ test_log:: test( tokio:: test) ]
18
18
async fn test_hello_unary ( ) -> Result < ( ) , anyhow:: Error > {
19
19
let mut mocks = MockSet :: new ( ) ;
20
20
mocks. insert (
21
- MockPath :: new ( Method :: POST , "/example.Hello/HelloUnary" ) ,
21
+ MockPath :: post ( "/example.Hello/HelloUnary" ) ,
22
22
Mock :: new (
23
23
MockRequest :: pb ( HelloRequest { name : "Dan" . into ( ) } ) ,
24
24
MockResponse :: pb ( HelloResponse {
25
25
message : "Hello Dan!" . into ( ) ,
26
26
} ) ,
27
27
) ,
28
28
) ;
29
+ mocks. insert (
30
+ MockPath :: post ( "/example.Hello/HelloUnary" ) ,
31
+ Mock :: new (
32
+ MockRequest :: pb ( HelloRequest {
33
+ name : "InternalError" . into ( ) ,
34
+ } ) ,
35
+ MockResponse :: empty ( )
36
+ . with_code ( StatusCode :: INTERNAL_SERVER_ERROR )
37
+ . with_message ( "woops" ) ,
38
+ ) ,
39
+ ) ;
40
+ // mocks.insert(
41
+ // MockPath::post("/example.Hello/HelloUnary"),
42
+ // Mock::new(
43
+ // MockRequest::pb(HelloRequest {
44
+ // name: "Header".into(),
45
+ // })
46
+ // .with_headers(HeaderMap::from_iter([(
47
+ // HeaderName::from_static("some-header"),
48
+ // HeaderValue::from_static(":D"),
49
+ // )])),
50
+ // MockResponse::pb(HelloResponse {
51
+ // message: "Hello Header!".into(),
52
+ // }),
53
+ // ),
54
+ // );
29
55
30
56
let server = GrpcMockServer :: new ( "example.Hello" , mocks) ?;
31
57
server. start ( ) . await ?;
@@ -36,28 +62,52 @@ mod tests {
36
62
. await ?;
37
63
let mut client = HelloClient :: new ( channel) ;
38
64
65
+ // Success response
39
66
let result = client
40
- . hello_unary ( HelloRequest { name : "Dan" . into ( ) } )
67
+ . hello_unary ( HelloRequest {
68
+ name : "Header" . into ( ) ,
69
+ } )
41
70
. await ;
42
71
dbg ! ( & result) ;
43
- assert ! ( result. is_ok( ) ) ;
44
-
72
+ //assert!(result.is_ok());
73
+
74
+ // Success response w/ header matching
75
+ // let mut request = tonic::Request::new(HelloRequest { name: "Dan".into() });
76
+ // request
77
+ // .metadata_mut()
78
+ // .insert("some-header", ":D".parse().unwrap());
79
+ // let result = client.hello_unary(request).await;
80
+ // dbg!(&result);
81
+ // assert!(result.is_ok());
82
+
83
+ // Error response (mock not found)
45
84
let result = client
46
85
. hello_unary ( HelloRequest {
47
- name : "NotFound1 " . into ( ) ,
86
+ name : "NotFoundError " . into ( ) ,
48
87
} )
49
88
. await ;
50
89
dbg ! ( & result) ;
51
90
assert ! ( result. is_err_and( |e| e. code( ) == tonic:: Code :: NotFound ) ) ;
52
91
92
+ // Error response (internal)
93
+ let result = client
94
+ . hello_unary ( HelloRequest {
95
+ name : "InternalError" . into ( ) ,
96
+ } )
97
+ . await ;
98
+ dbg ! ( & result) ;
99
+ assert ! (
100
+ result. is_err_and( |e| { e. code( ) == tonic:: Code :: Internal && e. message( ) == "woops" } )
101
+ ) ;
102
+
53
103
Ok ( ( ) )
54
104
}
55
105
56
106
#[ test_log:: test( tokio:: test) ]
57
107
async fn test_hello_streaming ( ) -> Result < ( ) , anyhow:: Error > {
58
108
let mut mocks = MockSet :: new ( ) ;
59
109
mocks. insert (
60
- MockPath :: new ( Method :: POST , "/example.Hello/HelloClientStreaming" ) ,
110
+ MockPath :: post ( "/example.Hello/HelloClientStreaming" ) ,
61
111
Mock :: new (
62
112
MockRequest :: stream ( [
63
113
HelloRequest { name : "Dan" . into ( ) } . to_bytes ( ) ,
@@ -72,7 +122,7 @@ mod tests {
72
122
) ,
73
123
) ;
74
124
mocks. insert (
75
- MockPath :: new ( Method :: POST , "/example.Hello/HelloServerStreaming" ) ,
125
+ MockPath :: post ( "/example.Hello/HelloServerStreaming" ) ,
76
126
Mock :: new (
77
127
MockRequest :: pb ( HelloRequest {
78
128
name : "Dan, Gaurav" . into ( ) ,
@@ -89,7 +139,7 @@ mod tests {
89
139
) ,
90
140
) ;
91
141
mocks. insert (
92
- MockPath :: new ( Method :: POST , "/example.Hello/HelloBidiStreaming" ) ,
142
+ MockPath :: post ( "/example.Hello/HelloBidiStreaming" ) ,
93
143
Mock :: new (
94
144
MockRequest :: pb_stream ( [
95
145
HelloRequest {
0 commit comments