@@ -19,6 +19,7 @@ public class HttpPipelineBuilderTest : PolicyTestBase
1919 [ Theory ]
2020 [ TestCase ( HttpPipelinePosition . PerCall , 1 ) ]
2121 [ TestCase ( HttpPipelinePosition . PerRetry , 2 ) ]
22+ [ TestCase ( HttpPipelinePosition . BeforeTransport , 2 ) ]
2223 public async Task CanAddCustomPolicy ( HttpPipelinePosition position , int expectedCount )
2324 {
2425 var policy = new CounterPolicy ( ) ;
@@ -40,6 +41,52 @@ public async Task CanAddCustomPolicy(HttpPipelinePosition position, int expected
4041 Assert . AreEqual ( expectedCount , policy . ExecutionCount ) ;
4142 }
4243
44+ [ Test ]
45+ public async Task CustomPolicyOrdering ( )
46+ {
47+ bool perCallRan = false ;
48+ bool perRetryRan = false ;
49+ bool beforeTransportRan = false ;
50+
51+ var transport = new MockTransport ( new MockResponse ( 200 ) ) ;
52+ var options = new TestOptions ( ) ;
53+
54+ options . AddPolicy ( new CallbackPolicy ( m =>
55+ {
56+ perCallRan = true ;
57+ Assert . False ( perRetryRan ) ;
58+ Assert . False ( beforeTransportRan ) ;
59+ } ) , HttpPipelinePosition . PerCall ) ;
60+
61+ options . AddPolicy ( new CallbackPolicy ( m =>
62+ {
63+ perRetryRan = true ;
64+ Assert . True ( perCallRan ) ;
65+ Assert . False ( beforeTransportRan ) ;
66+ } ) , HttpPipelinePosition . PerRetry ) ;
67+
68+ options . AddPolicy ( new CallbackPolicy ( m =>
69+ {
70+ beforeTransportRan = true ;
71+ Assert . True ( perRetryRan ) ;
72+ Assert . True ( perCallRan ) ;
73+ } ) , HttpPipelinePosition . BeforeTransport ) ;
74+
75+ options . Transport = transport ;
76+
77+ HttpPipeline pipeline = HttpPipelineBuilder . Build ( options ) ;
78+
79+ using Request request = transport . CreateRequest ( ) ;
80+ request . Method = RequestMethod . Get ;
81+ request . Uri . Reset ( new Uri ( "http://example.com" ) ) ;
82+
83+ await pipeline . SendRequestAsync ( request , CancellationToken . None ) ;
84+
85+ Assert . True ( perRetryRan ) ;
86+ Assert . True ( perCallRan ) ;
87+ Assert . True ( beforeTransportRan ) ;
88+ }
89+
4390 [ Test ]
4491 public async Task UsesAssemblyNameAndInformationalVersionForTelemetryPolicySettings ( )
4592 {
@@ -155,5 +202,20 @@ public override void OnSendingRequest(HttpMessage message)
155202
156203 public int ExecutionCount { get ; set ; }
157204 }
205+
206+ private class CallbackPolicy : HttpPipelineSynchronousPolicy
207+ {
208+ private readonly Action < HttpMessage > _message ;
209+
210+ public CallbackPolicy ( Action < HttpMessage > message )
211+ {
212+ _message = message ;
213+ }
214+
215+ public override void OnSendingRequest ( HttpMessage message )
216+ {
217+ _message ( message ) ;
218+ }
219+ }
158220 }
159221}
0 commit comments