Skip to content

Commit 39113ae

Browse files
author
Bernt Røskar Brenna
committed
Scripts can override content type
1 parent 6ac27af commit 39113ae

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

UnitTests/TestDynamicResponse.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,29 @@ public async Task ScriptsCanSetResponseStatusCode()
188188

189189
Assert.Equal(102, (int) response.HttpStatusCode);
190190
}
191+
192+
[Fact]
193+
public async Task ScriptsCanSetContentType()
194+
{
195+
var endpoint = new Endpoint("foo", "bar");
196+
var responseCreator = new LiteralDynamicResponseCreator("ContentType = \"image/png\"; return \"\";", endpoint);
197+
var response = new TestableHttpResponse();
198+
await responseCreator.CreateResponseAsync(new TestableHttpRequest("/", null), new byte[0], response, endpoint);
199+
200+
Assert.Equal("image/png", response.ContentType);
201+
}
202+
203+
[Fact]
204+
public async Task ScriptWithoutReturnValueThrowsException()
205+
{
206+
var endpoint = new Endpoint("foo", "bar");
207+
var responseCreator = new LiteralDynamicResponseCreator("ContentType = \"image/png\";", endpoint);
208+
var response = new TestableHttpResponse();
209+
await Assert.ThrowsAsync(
210+
typeof(ArgumentNullException),
211+
async () => await responseCreator.CreateResponseAsync(new TestableHttpRequest("/", null), new byte[0], response, endpoint)
212+
);
213+
}
191214
}
192215

193216
public class TestCheckScriptModifications : IDisposable

netmockery/DynamicResponseCreator.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,20 @@ protected override void SetStatusCode(RequestInfo requestInfo, IHttpResponseWrap
151151
base.SetStatusCode(requestInfo, response);
152152
}
153153
}
154+
155+
protected override void SetContentType(RequestInfo requestInfo, IHttpResponseWrapper response)
156+
{
157+
Debug.Assert(requestInfo != null);
158+
Debug.Assert(response != null);
159+
if (requestInfo.ContentType != RequestInfo.USE_CONFIGURED_CONTENT_TYPE)
160+
{
161+
response.ContentType = requestInfo.ContentType;
162+
}
163+
else
164+
{
165+
base.SetContentType(requestInfo, response);
166+
}
167+
}
154168
}
155169

156170
public class LiteralDynamicResponseCreator : DynamicResponseCreatorBase

netmockery/ResponseCreator.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public interface IResponseCreatorWithFilename
8787
public class RequestInfo
8888
{
8989
public const int USE_CONFIGURED_STATUS_CODE = -1;
90+
public const string USE_CONFIGURED_CONTENT_TYPE = null;
9091

9192
private static object _locker = new object();
9293

@@ -96,6 +97,7 @@ public class RequestInfo
9697
public string QueryString;
9798
public string RequestBody;
9899
public int StatusCode = USE_CONFIGURED_STATUS_CODE;
100+
public string ContentType = USE_CONFIGURED_CONTENT_TYPE;
99101
public IHeaderDictionary Headers;
100102
public Endpoint Endpoint;
101103
public string EndpointDirectory => Endpoint.Directory;
@@ -213,16 +215,25 @@ public override async Task<byte[]> CreateResponseAsync(IHttpRequestWrapper reque
213215
Endpoint = endpoint
214216
};
215217
var responseBody = await GetBodyAndExecuteReplacementsAsync(requestInfo);
218+
219+
SetContentType(requestInfo, response);
220+
SetStatusCode(requestInfo, response);
221+
222+
await response.WriteAsync(responseBody, Encoding);
223+
return Encoding.GetBytes(responseBody);
224+
}
225+
226+
protected virtual void SetContentType(RequestInfo requestInfo, IHttpResponseWrapper response)
227+
{
228+
// extension point, override to add logic in inheritors.
229+
// currently used by DynamicResponseCreator in order to let script code override content type
230+
216231
if (ContentType != null)
217232
{
218233
var contenttype = ContentType;
219234
contenttype += $"; charset={Encoding.WebName}";
220235
response.ContentType = contenttype;
221236
}
222-
223-
SetStatusCode(requestInfo, response);
224-
await response.WriteAsync(responseBody, Encoding);
225-
return Encoding.GetBytes(responseBody);
226237
}
227238

228239
protected virtual void SetStatusCode(RequestInfo requestInfo, IHttpResponseWrapper response)

0 commit comments

Comments
 (0)