@@ -6,22 +6,41 @@ use async_std::task::{Context, Poll};
6
6
use std:: pin:: Pin ;
7
7
use std:: sync:: Mutex ;
8
8
9
+ #[ derive( Debug , Copy , Clone ) ]
10
+ #[ allow( dead_code) ]
11
+ enum Direction {
12
+ Client ,
13
+ Server ,
14
+ }
15
+
9
16
#[ derive( Clone ) ]
10
17
pub struct TestCase {
11
- request_fixture : Arc < File > ,
12
- response_fixture : Arc < Mutex < File > > ,
18
+ direction : Direction ,
19
+ source_fixture : Arc < File > ,
20
+ expected_fixture : Arc < Mutex < File > > ,
13
21
result : Arc < Mutex < File > > ,
14
22
}
15
23
16
24
impl TestCase {
17
- pub async fn new ( request_file_path : & str , response_file_path : & str ) -> TestCase {
25
+ pub async fn new_server ( request_file_path : & str , response_file_path : & str ) -> TestCase {
26
+ Self :: new ( Direction :: Server , request_file_path, response_file_path) . await
27
+ }
28
+
29
+ pub async fn new_client ( request_file_path : & str , response_file_path : & str ) -> TestCase {
30
+ Self :: new ( Direction :: Client , request_file_path, response_file_path) . await
31
+ }
32
+
33
+ async fn new (
34
+ direction : Direction ,
35
+ request_file_path : & str ,
36
+ response_file_path : & str ,
37
+ ) -> TestCase {
18
38
let request_fixture = File :: open ( fixture_path ( & request_file_path) )
19
39
. await
20
40
. expect ( & format ! (
21
41
"Could not open request fixture file: {:?}" ,
22
42
& fixture_path( request_file_path)
23
43
) ) ;
24
- let request_fixture = Arc :: new ( request_fixture) ;
25
44
26
45
let response_fixture =
27
46
File :: open ( fixture_path ( & response_file_path) )
@@ -30,14 +49,19 @@ impl TestCase {
30
49
"Could not open response fixture file: {:?}" ,
31
50
& fixture_path( response_file_path)
32
51
) ) ;
33
- let response_fixture = Arc :: new ( Mutex :: new ( response_fixture) ) ;
34
52
35
53
let temp = tempfile:: tempfile ( ) . expect ( "Failed to create tempfile" ) ;
36
54
let result = Arc :: new ( Mutex :: new ( temp. into ( ) ) ) ;
37
55
56
+ let ( source_fixture, expected_fixture) = match direction {
57
+ Direction :: Client => ( response_fixture, request_fixture) ,
58
+ Direction :: Server => ( request_fixture, response_fixture) ,
59
+ } ;
60
+
38
61
TestCase {
39
- request_fixture,
40
- response_fixture,
62
+ direction,
63
+ source_fixture : Arc :: new ( source_fixture) ,
64
+ expected_fixture : Arc :: new ( Mutex :: new ( expected_fixture) ) ,
41
65
result,
42
66
}
43
67
}
@@ -54,7 +78,7 @@ impl TestCase {
54
78
pub async fn read_expected ( & self ) -> String {
55
79
use async_std:: prelude:: * ;
56
80
let mut expected = std:: string:: String :: new ( ) ;
57
- self . response_fixture
81
+ self . expected_fixture
58
82
. lock ( )
59
83
. unwrap ( )
60
84
. read_to_string ( & mut expected)
@@ -83,13 +107,13 @@ pub(crate) fn fixture_path(relative_path: &str) -> PathBuf {
83
107
pub ( crate ) fn munge_date ( expected : & mut String , actual : & mut String ) {
84
108
match expected. find ( "{DATE}" ) {
85
109
Some ( i) => {
86
- expected . replace_range ( i..i + 6 , "" ) ;
87
- match expected . get ( i..i + 1 ) {
88
- Some ( byte ) => {
89
- let j = actual[ i ..] . find ( byte ) . expect ( "Byte not found " ) ;
90
- actual . replace_range ( i..i + j , "" ) ;
110
+ println ! ( "{}" , expected ) ;
111
+ match actual . find ( "date: " ) {
112
+ Some ( j ) => {
113
+ let eol = actual[ j + 6 ..] . find ( " \r \n " ) . expect ( "missing eol " ) ;
114
+ expected . replace_range ( i..i + 6 , & actual [ j + 6 ..j + 6 + eol ] ) ;
91
115
}
92
- None => expected. replace_range ( i.., "" ) ,
116
+ None => expected. replace_range ( i..i + 6 , "" ) ,
93
117
}
94
118
}
95
119
None => { }
@@ -102,7 +126,7 @@ impl Read for TestCase {
102
126
cx : & mut Context ,
103
127
buf : & mut [ u8 ] ,
104
128
) -> Poll < io:: Result < usize > > {
105
- Pin :: new ( & mut & * self . request_fixture ) . poll_read ( cx, buf)
129
+ Pin :: new ( & mut & * self . source_fixture ) . poll_read ( cx, buf)
106
130
}
107
131
}
108
132
0 commit comments