Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Playback.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2003
VisualStudioVersion = 15.0.27130.2027
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApiGateway Sample", "tst\ApiGateway Sample\ApiGateway Sample.csproj", "{CCA7E1DD-CF4A-48F2-B4B5-48360B813066}"
EndProject
Expand All @@ -16,6 +16,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tst", "tst", "{E98DA495-144
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "pmilet.Playback", "src\pmilet.Playback\pmilet.Playback.csproj", "{62A32EB4-8F55-4644-9FC5-0F449D88176B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{203F4BB3-4B70-4EBB-A77D-339E3300428F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -36,6 +38,7 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{CCA7E1DD-CF4A-48F2-B4B5-48360B813066} = {E98DA495-144B-4E71-A48E-0CFDF8667175}
{62A32EB4-8F55-4644-9FC5-0F449D88176B} = {203F4BB3-4B70-4EBB-A77D-339E3300428F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2323340C-4725-4D0D-8A6E-F61F82BC5FC6}
Expand Down
13 changes: 8 additions & 5 deletions src/pmilet.Playback/FakeFactoryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using System.Reflection;
using Newtonsoft.Json.Serialization;

namespace pmilet.Playback
{
public abstract class FakeFactoryBase : IFakeFactory
{
protected bool GenerateFakeResponse<TRequest, TResponse>(HttpContext context, Func<TRequest, TResponse> func)
protected bool GenerateFakeResponse<TRequest, TResponse>(HttpContext context, Func<TRequest, TResponse> func, Encoding encoding = null)
{
dynamic request = context.Request.Method != "GET" ? Deserialize<TRequest>(context.Request.Body) : GetFromQueryString(context, typeof(TRequest));
var response = func(request);
Stream fakeResponseStream = Serialize<TResponse>(response);
Stream fakeResponseStream = Serialize<TResponse>(response, encoding);
fakeResponseStream.CopyToAsync(context.Response.Body);
return true;
}
Expand All @@ -33,10 +34,12 @@ protected T Deserialize<T>(Stream body)
}
}

protected Stream Serialize<T>(T body)
protected Stream Serialize<T>(T body, Encoding encoding)
{
string serializedBody = JsonConvert.SerializeObject(body);
var bytes = Encoding.UTF8.GetBytes(serializedBody);
var jsonSerializerSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
string serializedBody = JsonConvert.SerializeObject(body, jsonSerializerSettings);
encoding = encoding ?? Encoding.UTF8;
var bytes = encoding.GetBytes(serializedBody);
MemoryStream m = new MemoryStream(bytes);
return m;
}
Expand Down