Skip to content

Commit

Permalink
Scripts can override content type
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernt Røskar Brenna committed Aug 22, 2017
1 parent 6ac27af commit 39113ae
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
23 changes: 23 additions & 0 deletions UnitTests/TestDynamicResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,29 @@ public async Task ScriptsCanSetResponseStatusCode()

Assert.Equal(102, (int) response.HttpStatusCode);
}

[Fact]
public async Task ScriptsCanSetContentType()
{
var endpoint = new Endpoint("foo", "bar");
var responseCreator = new LiteralDynamicResponseCreator("ContentType = \"image/png\"; return \"\";", endpoint);
var response = new TestableHttpResponse();
await responseCreator.CreateResponseAsync(new TestableHttpRequest("/", null), new byte[0], response, endpoint);

Assert.Equal("image/png", response.ContentType);
}

[Fact]
public async Task ScriptWithoutReturnValueThrowsException()
{
var endpoint = new Endpoint("foo", "bar");
var responseCreator = new LiteralDynamicResponseCreator("ContentType = \"image/png\";", endpoint);
var response = new TestableHttpResponse();
await Assert.ThrowsAsync(
typeof(ArgumentNullException),
async () => await responseCreator.CreateResponseAsync(new TestableHttpRequest("/", null), new byte[0], response, endpoint)
);
}
}

public class TestCheckScriptModifications : IDisposable
Expand Down
14 changes: 14 additions & 0 deletions netmockery/DynamicResponseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ protected override void SetStatusCode(RequestInfo requestInfo, IHttpResponseWrap
base.SetStatusCode(requestInfo, response);
}
}

protected override void SetContentType(RequestInfo requestInfo, IHttpResponseWrapper response)
{
Debug.Assert(requestInfo != null);
Debug.Assert(response != null);
if (requestInfo.ContentType != RequestInfo.USE_CONFIGURED_CONTENT_TYPE)
{
response.ContentType = requestInfo.ContentType;
}
else
{
base.SetContentType(requestInfo, response);
}
}
}

public class LiteralDynamicResponseCreator : DynamicResponseCreatorBase
Expand Down
19 changes: 15 additions & 4 deletions netmockery/ResponseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public interface IResponseCreatorWithFilename
public class RequestInfo
{
public const int USE_CONFIGURED_STATUS_CODE = -1;
public const string USE_CONFIGURED_CONTENT_TYPE = null;

private static object _locker = new object();

Expand All @@ -96,6 +97,7 @@ public class RequestInfo
public string QueryString;
public string RequestBody;
public int StatusCode = USE_CONFIGURED_STATUS_CODE;
public string ContentType = USE_CONFIGURED_CONTENT_TYPE;
public IHeaderDictionary Headers;
public Endpoint Endpoint;
public string EndpointDirectory => Endpoint.Directory;
Expand Down Expand Up @@ -213,16 +215,25 @@ public override async Task<byte[]> CreateResponseAsync(IHttpRequestWrapper reque
Endpoint = endpoint
};
var responseBody = await GetBodyAndExecuteReplacementsAsync(requestInfo);

SetContentType(requestInfo, response);
SetStatusCode(requestInfo, response);

await response.WriteAsync(responseBody, Encoding);
return Encoding.GetBytes(responseBody);
}

protected virtual void SetContentType(RequestInfo requestInfo, IHttpResponseWrapper response)
{
// extension point, override to add logic in inheritors.
// currently used by DynamicResponseCreator in order to let script code override content type

if (ContentType != null)
{
var contenttype = ContentType;
contenttype += $"; charset={Encoding.WebName}";
response.ContentType = contenttype;
}

SetStatusCode(requestInfo, response);
await response.WriteAsync(responseBody, Encoding);
return Encoding.GetBytes(responseBody);
}

protected virtual void SetStatusCode(RequestInfo requestInfo, IHttpResponseWrapper response)
Expand Down

0 comments on commit 39113ae

Please sign in to comment.