3
3
4
4
#![ doc = include_str ! ( "../README.md" ) ]
5
5
6
- #[ cfg( not( target_arch = "wasm32" ) ) ]
7
6
pub mod proxy;
8
- #[ cfg( not( target_arch = "wasm32" ) ) ]
9
7
pub mod recorded;
10
- #[ cfg( not( target_arch = "wasm32" ) ) ]
11
8
mod recording;
12
- mod transport;
13
9
14
10
pub use azure_core:: test:: TestMode ;
15
- use azure_core:: { ClientOptions , TransportOptions } ;
16
- #[ cfg( not( target_arch = "wasm32" ) ) ]
11
+ pub use proxy:: { matchers:: * , sanitizers:: * } ;
17
12
pub use recording:: * ;
18
- use std:: {
19
- path:: { Path , PathBuf } ,
20
- sync:: Arc ,
21
- } ;
13
+ use std:: path:: { Path , PathBuf } ;
22
14
23
15
const SPAN_TARGET : & str = "test-proxy" ;
24
16
25
17
/// Context information required by recorded client library tests.
26
18
///
27
19
/// This context is required for any recorded tests not attributed as `#[recorded::test(live)]`
28
20
/// to setup up the HTTP client to record or play back session records.
29
- #[ derive( Clone , Debug ) ]
21
+ #[ derive( Debug ) ]
30
22
pub struct TestContext {
31
- test_mode : TestMode ,
32
23
crate_dir : & ' static Path ,
33
24
test_name : & ' static str ,
25
+ recording : Option < Recording > ,
34
26
}
35
27
36
28
impl TestContext {
37
29
/// Not intended for use outside the `azure_core` crates.
38
30
#[ doc( hidden) ]
39
- pub fn new ( test_mode : TestMode , crate_dir : & ' static str , test_name : & ' static str ) -> Self {
31
+ pub fn new ( crate_dir : & ' static str , test_name : & ' static str ) -> Self {
40
32
Self {
41
- test_mode,
42
33
crate_dir : Path :: new ( crate_dir) ,
43
34
test_name,
35
+ recording : None ,
44
36
}
45
37
}
46
38
47
- /// Instruments the [`ClientOptions`] to support recording and playing back of session records.
48
- ///
49
- /// # Examples
50
- ///
51
- /// ```no_run
52
- /// use azure_core_test::{recorded, TestContext};
53
- ///
54
- /// # struct MyClient;
55
- /// # #[derive(Default)]
56
- /// # struct MyClientOptions { client_options: azure_core::ClientOptions };
57
- /// # impl MyClient {
58
- /// # fn new(endpoint: impl AsRef<str>, options: Option<MyClientOptions>) -> Self { todo!() }
59
- /// # async fn invoke(&self) -> azure_core::Result<()> { todo!() }
60
- /// # }
61
- /// #[recorded::test]
62
- /// async fn test_invoke(ctx: TestContext) -> azure_core::Result<()> {
63
- /// let mut options = MyClientOptions::default();
64
- /// ctx.instrument(&mut options.client_options);
65
- ///
66
- /// let client = MyClient::new("https://azure.net", Some(options));
67
- /// client.invoke().await
68
- /// }
69
- /// ```
70
- pub fn instrument ( & self , options : & mut ClientOptions ) {
71
- let transport = options. transport . clone ( ) . unwrap_or_default ( ) ;
72
- options. transport = Some ( TransportOptions :: new_custom_policy ( Arc :: new (
73
- transport:: ProxyTransportPolicy {
74
- inner : transport,
75
- mode : self . test_mode ,
76
- } ,
77
- ) ) ) ;
78
- }
79
-
80
39
/// Gets the root directory of the crate under test.
81
40
pub fn crate_dir ( & self ) -> & ' static Path {
82
41
self . crate_dir
83
42
}
84
43
44
+ /// Gets the [`Recording`].
45
+ ///
46
+ /// # Panics
47
+ ///
48
+ /// Panics if a recording or playback has not been started.
49
+ pub fn recording ( & self ) -> & Recording {
50
+ self . recording
51
+ . as_ref ( )
52
+ . expect ( "not recording or playback started" )
53
+ }
54
+
85
55
/// Gets the test data directory under [`Self::crate_dir`].
86
56
pub fn test_data_dir ( & self ) -> PathBuf {
87
57
self . crate_dir . join ( "tests/data" )
88
58
}
89
59
90
- /// Gets the current [`TestMode`].
91
- pub fn test_mode ( & self ) -> TestMode {
92
- self . test_mode
93
- }
94
-
95
60
/// Gets the current test function name.
96
61
pub fn test_name ( & self ) -> & ' static str {
97
62
self . test_name
@@ -118,12 +83,8 @@ mod tests {
118
83
119
84
#[ test]
120
85
fn test_content_new ( ) {
121
- let ctx = TestContext :: new (
122
- TestMode :: default ( ) ,
123
- env ! ( "CARGO_MANIFEST_DIR" ) ,
124
- "test_content_new" ,
125
- ) ;
126
- assert_eq ! ( ctx. test_mode( ) , TestMode :: Playback ) ;
86
+ let ctx = TestContext :: new ( env ! ( "CARGO_MANIFEST_DIR" ) , "test_content_new" ) ;
87
+ assert ! ( ctx. recording. is_none( ) ) ;
127
88
assert ! ( ctx
128
89
. crate_dir( )
129
90
. to_str( )
0 commit comments