@@ -16,7 +16,7 @@ namespace Flurl.Http.Testing
16
16
/// </summary>
17
17
public abstract class HttpTestSetup
18
18
{
19
- private readonly List < Func < HttpResponseMessage > > _responses = new ( ) ;
19
+ private readonly List < Func < IFlurlRequest , HttpResponseMessage > > _responses = new ( ) ;
20
20
21
21
private int _respIndex = 0 ;
22
22
private bool _allowRealHttp = false ;
@@ -34,13 +34,13 @@ protected HttpTestSetup(FlurlHttpSettings settings) {
34
34
/// </summary>
35
35
public FlurlHttpSettings Settings { get ; }
36
36
37
- internal HttpResponseMessage GetNextResponse ( ) {
37
+ internal HttpResponseMessage GetNextResponse ( IFlurlRequest request ) {
38
38
if ( _allowRealHttp )
39
39
return null ;
40
40
41
41
// atomically get the next response in the list, or the last one if we're past the end
42
42
if ( _responses . Any ( ) )
43
- return _responses [ Math . Min ( Interlocked . Increment ( ref _respIndex ) , _responses . Count ) - 1 ] ( ) ;
43
+ return _responses [ Math . Min ( Interlocked . Increment ( ref _respIndex ) , _responses . Count ) - 1 ] ( request ) ;
44
44
45
45
return new HttpResponseMessage {
46
46
StatusCode = HttpStatusCode . OK ,
@@ -61,6 +61,20 @@ public HttpTestSetup RespondWith(string body, int status = 200, object headers =
61
61
return RespondWith ( ( ) => new CapturedStringContent ( body ) , status , headers , cookies , replaceUnderscoreWithHyphen ) ;
62
62
}
63
63
64
+
65
+ /// <summary>
66
+ /// Adds a fake HTTP response to the response queue.
67
+ /// </summary>
68
+ /// <param name="buildContent">A function that builds the simulated response body content. Optional.</param>
69
+ /// <param name="status">The simulated HTTP status. Default is 200.</param>
70
+ /// <param name="headers">The simulated response headers (optional).</param>
71
+ /// <param name="cookies">The simulated response cookies (optional).</param>
72
+ /// <param name="replaceUnderscoreWithHyphen">If true, underscores in property names of headers will be replaced by hyphens. Default is true.</param>
73
+ /// <returns>The current HttpTest object (so more responses can be chained).</returns>
74
+ public HttpTestSetup RespondWith ( Func < IFlurlRequest , string > buildContent , int status = 200 , object headers = null , object cookies = null , bool replaceUnderscoreWithHyphen = true ) {
75
+ return RespondWith ( ( request ) => new CapturedStringContent ( buildContent ( request ) ) , status , headers , cookies , replaceUnderscoreWithHyphen ) ;
76
+ }
77
+
64
78
/// <summary>
65
79
/// Adds a fake HTTP response to the response queue with the given data serialized to JSON as the content body.
66
80
/// </summary>
@@ -75,6 +89,37 @@ public HttpTestSetup RespondWithJson(object body, int status = 200, object heade
75
89
return RespondWith ( ( ) => new CapturedJsonContent ( s ) , status , headers , cookies , replaceUnderscoreWithHyphen ) ;
76
90
}
77
91
92
+ /// <summary>
93
+ /// Adds a fake HTTP response to the response queue with the given data serialized to JSON as the content body.
94
+ /// </summary>
95
+ /// <param name="buildContent">A function that builds the simulated response body content. Optional.</param>
96
+ /// <param name="status">The simulated HTTP status. Default is 200.</param>
97
+ /// <param name="headers">The simulated response headers (optional).</param>
98
+ /// <param name="cookies">The simulated response cookies (optional).</param>
99
+ /// <param name="replaceUnderscoreWithHyphen">If true, underscores in property names of headers will be replaced by hyphens. Default is true.</param>
100
+ /// <returns>The current HttpTest object (so more responses can be chained).</returns>
101
+ public HttpTestSetup RespondWithJson ( Func < IFlurlRequest , object > buildContent , int status = 200 , object headers = null , object cookies = null , bool replaceUnderscoreWithHyphen = true ) {
102
+ return RespondWith ( ( request ) =>
103
+ {
104
+ var s = Settings . JsonSerializer . Serialize ( buildContent ( request ) ) ;
105
+ return new CapturedJsonContent ( s ) ;
106
+ } , status , headers , cookies , replaceUnderscoreWithHyphen ) ;
107
+ }
108
+
109
+ /// <summary>
110
+ /// Adds a fake HTTP response to the response queue.
111
+ /// </summary>
112
+ /// <param name="buildContent">A function that builds the simulated response body content. Optional.</param>
113
+ /// <param name="status">The simulated HTTP status. Optional. Default is 200.</param>
114
+ /// <param name="headers">The simulated response headers. Optional.</param>
115
+ /// <param name="cookies">The simulated response cookies. Optional.</param>
116
+ /// <param name="replaceUnderscoreWithHyphen">If true, underscores in property names of headers will be replaced by hyphens. Default is true.</param>
117
+ /// <returns>The current HttpTest object (so more responses can be chained).</returns>
118
+ public HttpTestSetup RespondWith ( Func < HttpContent > buildContent , int status = 200 , object headers = null , object cookies = null , bool replaceUnderscoreWithHyphen = true )
119
+ {
120
+ return RespondWith ( request => buildContent ? . Invoke ( ) , status , headers , cookies , replaceUnderscoreWithHyphen ) ;
121
+ }
122
+
78
123
/// <summary>
79
124
/// Adds a fake HTTP response to the response queue.
80
125
/// </summary>
@@ -84,11 +129,11 @@ public HttpTestSetup RespondWithJson(object body, int status = 200, object heade
84
129
/// <param name="cookies">The simulated response cookies. Optional.</param>
85
130
/// <param name="replaceUnderscoreWithHyphen">If true, underscores in property names of headers will be replaced by hyphens. Default is true.</param>
86
131
/// <returns>The current HttpTest object (so more responses can be chained).</returns>
87
- public HttpTestSetup RespondWith ( Func < HttpContent > buildContent = null , int status = 200 , object headers = null , object cookies = null , bool replaceUnderscoreWithHyphen = true ) {
88
- _responses . Add ( ( ) => {
132
+ public HttpTestSetup RespondWith ( Func < IFlurlRequest , HttpContent > buildContent = null , int status = 200 , object headers = null , object cookies = null , bool replaceUnderscoreWithHyphen = true ) {
133
+ _responses . Add ( ( request ) => {
89
134
var response = new HttpResponseMessage {
90
135
StatusCode = ( HttpStatusCode ) status ,
91
- Content = buildContent ? . Invoke ( )
136
+ Content = buildContent ? . Invoke ( request )
92
137
} ;
93
138
94
139
if ( headers != null ) {
@@ -118,7 +163,7 @@ public HttpTestSetup SimulateTimeout() =>
118
163
/// </summary>
119
164
/// <param name="exception">The exception to throw when the call is simulated.</param>
120
165
public HttpTestSetup SimulateException ( Exception exception ) {
121
- _responses . Add ( ( ) => throw exception ) ;
166
+ _responses . Add ( ( request ) => throw exception ) ;
122
167
return this ;
123
168
}
124
169
0 commit comments