@@ -6,22 +6,41 @@ use async_std::task::{Context, Poll};
66use std:: pin:: Pin ;
77use std:: sync:: Mutex ;
88
9+ #[ derive( Debug , Copy , Clone ) ]
10+ #[ allow( dead_code) ]
11+ enum Direction {
12+ Client ,
13+ Server ,
14+ }
15+
916#[ derive( Clone ) ]
1017pub 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 > > ,
1321 result : Arc < Mutex < File > > ,
1422}
1523
1624impl 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 {
1838 let request_fixture = File :: open ( fixture_path ( & request_file_path) )
1939 . await
2040 . expect ( & format ! (
2141 "Could not open request fixture file: {:?}" ,
2242 & fixture_path( request_file_path)
2343 ) ) ;
24- let request_fixture = Arc :: new ( request_fixture) ;
2544
2645 let response_fixture =
2746 File :: open ( fixture_path ( & response_file_path) )
@@ -30,14 +49,19 @@ impl TestCase {
3049 "Could not open response fixture file: {:?}" ,
3150 & fixture_path( response_file_path)
3251 ) ) ;
33- let response_fixture = Arc :: new ( Mutex :: new ( response_fixture) ) ;
3452
3553 let temp = tempfile:: tempfile ( ) . expect ( "Failed to create tempfile" ) ;
3654 let result = Arc :: new ( Mutex :: new ( temp. into ( ) ) ) ;
3755
56+ let ( source_fixture, expected_fixture) = match direction {
57+ Direction :: Client => ( response_fixture, request_fixture) ,
58+ Direction :: Server => ( request_fixture, response_fixture) ,
59+ } ;
60+
3861 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) ) ,
4165 result,
4266 }
4367 }
@@ -54,7 +78,7 @@ impl TestCase {
5478 pub async fn read_expected ( & self ) -> String {
5579 use async_std:: prelude:: * ;
5680 let mut expected = std:: string:: String :: new ( ) ;
57- self . response_fixture
81+ self . expected_fixture
5882 . lock ( )
5983 . unwrap ( )
6084 . read_to_string ( & mut expected)
@@ -83,13 +107,13 @@ pub(crate) fn fixture_path(relative_path: &str) -> PathBuf {
83107pub ( crate ) fn munge_date ( expected : & mut String , actual : & mut String ) {
84108 match expected. find ( "{DATE}" ) {
85109 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 ] ) ;
91115 }
92- None => expected. replace_range ( i.., "" ) ,
116+ None => expected. replace_range ( i..i + 6 , "" ) ,
93117 }
94118 }
95119 None => { }
@@ -102,7 +126,7 @@ impl Read for TestCase {
102126 cx : & mut Context ,
103127 buf : & mut [ u8 ] ,
104128 ) -> 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)
106130 }
107131}
108132
0 commit comments