@@ -1924,6 +1924,170 @@ public void Abort_CreateRequestThenAbort_Success(Uri remoteServer)
19241924 request . Abort ( ) ;
19251925 }
19261926
1927+ [ Theory ]
1928+ [ InlineData ( HttpRequestCacheLevel . NoCacheNoStore , null , null , new string [ ] { "Pragma: no-cache" , "Cache-Control: no-store, no-cache" } ) ]
1929+ [ InlineData ( HttpRequestCacheLevel . Reload , null , null , new string [ ] { "Pragma: no-cache" , "Cache-Control: no-cache" } ) ]
1930+ [ InlineData ( HttpRequestCacheLevel . CacheOrNextCacheOnly , null , null , new string [ ] { "Cache-Control: only-if-cached" } ) ]
1931+ [ InlineData ( HttpRequestCacheLevel . Default , HttpCacheAgeControl . MinFresh , 10 , new string [ ] { "Cache-Control: min-fresh=10" } ) ]
1932+ [ InlineData ( HttpRequestCacheLevel . Default , HttpCacheAgeControl . MaxAge , 10 , new string [ ] { "Cache-Control: max-age=10" } ) ]
1933+ [ InlineData ( HttpRequestCacheLevel . Default , HttpCacheAgeControl . MaxStale , 10 , new string [ ] { "Cache-Control: max-stale=10" } ) ]
1934+ [ InlineData ( HttpRequestCacheLevel . Refresh , null , null , new string [ ] { "Pragma: no-cache" , "Cache-Control: max-age=0" } ) ]
1935+ public async Task SendHttpGetRequest_WithHttpCachePolicy_AddCacheHeaders (
1936+ HttpRequestCacheLevel requestCacheLevel , HttpCacheAgeControl ? ageControl , int ? age , string [ ] expectedHeaders )
1937+ {
1938+ await LoopbackServer . CreateServerAsync ( async ( server , uri ) =>
1939+ {
1940+ HttpWebRequest request = WebRequest . CreateHttp ( uri ) ;
1941+ request . CachePolicy = ageControl != null ?
1942+ new HttpRequestCachePolicy ( ageControl . Value , TimeSpan . FromSeconds ( ( double ) age ) )
1943+ : new HttpRequestCachePolicy ( requestCacheLevel ) ;
1944+ Task < WebResponse > getResponse = GetResponseAsync ( request ) ;
1945+
1946+ await server . AcceptConnectionAsync ( async connection =>
1947+ {
1948+ List < string > headers = await connection . ReadRequestHeaderAndSendResponseAsync ( ) ;
1949+
1950+ foreach ( string header in expectedHeaders )
1951+ {
1952+ Assert . Contains ( header , headers ) ;
1953+ }
1954+ } ) ;
1955+
1956+ using ( var response = ( HttpWebResponse ) await getResponse )
1957+ {
1958+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
1959+ }
1960+ } ) ;
1961+ }
1962+
1963+ [ Theory ]
1964+ [ InlineData ( RequestCacheLevel . NoCacheNoStore , new string [ ] { "Pragma: no-cache" , "Cache-Control: no-store, no-cache" } ) ]
1965+ [ InlineData ( RequestCacheLevel . Reload , new string [ ] { "Pragma: no-cache" , "Cache-Control: no-cache" } ) ]
1966+ public async Task SendHttpGetRequest_WithCachePolicy_AddCacheHeaders (
1967+ RequestCacheLevel requestCacheLevel , string [ ] expectedHeaders )
1968+ {
1969+ await LoopbackServer . CreateServerAsync ( async ( server , uri ) =>
1970+ {
1971+ HttpWebRequest request = WebRequest . CreateHttp ( uri ) ;
1972+ request . CachePolicy = new RequestCachePolicy ( requestCacheLevel ) ;
1973+ Task < WebResponse > getResponse = GetResponseAsync ( request ) ;
1974+
1975+ await server . AcceptConnectionAsync ( async connection =>
1976+ {
1977+ List < string > headers = await connection . ReadRequestHeaderAndSendResponseAsync ( ) ;
1978+
1979+ foreach ( string header in expectedHeaders )
1980+ {
1981+ Assert . Contains ( header , headers ) ;
1982+ }
1983+ } ) ;
1984+
1985+ using ( var response = ( HttpWebResponse ) await getResponse )
1986+ {
1987+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
1988+ }
1989+ } ) ;
1990+ }
1991+
1992+ [ ConditionalTheory ( typeof ( RemoteExecutor ) , nameof ( RemoteExecutor . IsSupported ) ) ]
1993+ [ InlineData ( RequestCacheLevel . NoCacheNoStore , new string [ ] { "Pragma: no-cache" , "Cache-Control: no-store, no-cache" } ) ]
1994+ [ InlineData ( RequestCacheLevel . Reload , new string [ ] { "Pragma: no-cache" , "Cache-Control: no-cache" } ) ]
1995+ public void SendHttpGetRequest_WithGlobalCachePolicy_AddCacheHeaders (
1996+ RequestCacheLevel requestCacheLevel , string [ ] expectedHeaders )
1997+ {
1998+ RemoteExecutor . Invoke ( async ( async , reqCacheLevel , eh0 , eh1 ) =>
1999+ {
2000+ await LoopbackServer . CreateServerAsync ( async ( server , uri ) =>
2001+ {
2002+ HttpWebRequest . DefaultCachePolicy = new RequestCachePolicy ( Enum . Parse < RequestCacheLevel > ( reqCacheLevel ) ) ;
2003+ HttpWebRequest request = WebRequest . CreateHttp ( uri ) ;
2004+ Task < WebResponse > getResponse = bool . Parse ( async) ? request . GetResponseAsync ( ) : Task . Run ( ( ) => request . GetResponse ( ) ) ;
2005+
2006+ await server . AcceptConnectionAsync ( async connection =>
2007+ {
2008+ List < string > headers = await connection . ReadRequestHeaderAndSendResponseAsync ( ) ;
2009+ Assert . Contains ( eh0 , headers ) ;
2010+ Assert . Contains ( eh1 , headers ) ;
2011+ } ) ;
2012+
2013+ using ( var response = ( HttpWebResponse ) await getResponse )
2014+ {
2015+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
2016+ }
2017+ } ) ;
2018+ } , ( this is HttpWebRequestTest_Async ) . ToString ( ) , requestCacheLevel . ToString ( ) , expectedHeaders [ 0 ] , expectedHeaders [ 1 ] ) . Dispose ( ) ;
2019+ }
2020+
2021+ [ Theory ]
2022+ [ InlineData ( true ) ]
2023+ [ InlineData ( false ) ]
2024+ public async Task SendHttpGetRequest_WithCachePolicyCacheOnly_ThrowException (
2025+ bool isHttpCachePolicy )
2026+ {
2027+ HttpWebRequest request = WebRequest . CreateHttp ( "http://anything" ) ;
2028+ request . CachePolicy = isHttpCachePolicy ? new HttpRequestCachePolicy ( HttpRequestCacheLevel . CacheOnly )
2029+ : new RequestCachePolicy ( RequestCacheLevel . CacheOnly ) ;
2030+ WebException exception = await Assert . ThrowsAsync < WebException > ( ( ) => GetResponseAsync ( request ) ) ;
2031+ Assert . Equal ( SR . CacheEntryNotFound , exception . Message ) ;
2032+ }
2033+
2034+ [ ConditionalFact ( typeof ( RemoteExecutor ) , nameof ( RemoteExecutor . IsSupported ) ) ]
2035+ public void SendHttpGetRequest_WithGlobalCachePolicyBypassCache_DoNotAddCacheHeaders ( )
2036+ {
2037+ RemoteExecutor . Invoke ( async ( ) =>
2038+ {
2039+ await LoopbackServer . CreateServerAsync ( async ( server , uri ) =>
2040+ {
2041+ HttpWebRequest . DefaultCachePolicy = new RequestCachePolicy ( RequestCacheLevel . BypassCache ) ;
2042+ HttpWebRequest request = WebRequest . CreateHttp ( uri ) ;
2043+ Task < WebResponse > getResponse = request . GetResponseAsync ( ) ;
2044+
2045+ await server . AcceptConnectionAsync ( async connection =>
2046+ {
2047+ List < string > headers = await connection . ReadRequestHeaderAndSendResponseAsync ( ) ;
2048+
2049+ foreach ( string header in headers )
2050+ {
2051+ Assert . DoesNotContain ( "Pragma" , header ) ;
2052+ Assert . DoesNotContain ( "Cache-Control" , header ) ;
2053+ }
2054+ } ) ;
2055+
2056+ using ( var response = ( HttpWebResponse ) await getResponse )
2057+ {
2058+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
2059+ }
2060+ } ) ;
2061+ } ) . Dispose ( ) ;
2062+ }
2063+
2064+ [ Fact ]
2065+ public async Task SendHttpGetRequest_WithCachePolicyBypassCache_DoNotAddHeaders ( )
2066+ {
2067+ await LoopbackServer . CreateServerAsync ( async ( server , uri ) =>
2068+ {
2069+ HttpWebRequest request = WebRequest . CreateHttp ( uri ) ;
2070+ request . CachePolicy = new RequestCachePolicy ( RequestCacheLevel . BypassCache ) ;
2071+ Task < WebResponse > getResponse = request . GetResponseAsync ( ) ;
2072+
2073+ await server . AcceptConnectionAsync ( async connection =>
2074+ {
2075+ List < string > headers = await connection . ReadRequestHeaderAndSendResponseAsync ( ) ;
2076+
2077+ foreach ( string header in headers )
2078+ {
2079+ Assert . DoesNotContain ( "Pragma" , header ) ;
2080+ Assert . DoesNotContain ( "Cache-Control" , header ) ;
2081+ }
2082+ } ) ;
2083+
2084+ using ( var response = ( HttpWebResponse ) await getResponse )
2085+ {
2086+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
2087+ }
2088+ } ) ;
2089+ }
2090+
19272091 private void RequestStreamCallback ( IAsyncResult asynchronousResult )
19282092 {
19292093 RequestState state = ( RequestState ) asynchronousResult . AsyncState ;
0 commit comments