-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using FakeServer.Common; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.CodeAnalysis.CSharp.Scripting; | ||
using Microsoft.CodeAnalysis.Scripting; | ||
using Microsoft.Extensions.Options; | ||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace FakeServer.CustomResponse | ||
{ | ||
public class Globals | ||
{ | ||
public HttpContext _Context; | ||
public string _CollectionId; | ||
public string _Body; | ||
public string _Method; | ||
} | ||
|
||
public class CustomResponseMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly CustomResponseSettings _settings; | ||
private readonly List<Script<object>> _scripts; | ||
|
||
public CustomResponseMiddleware(RequestDelegate next, IOptions<CustomResponseSettings> settings) | ||
{ | ||
_next = next; | ||
_settings = settings.Value; | ||
_scripts = _settings.Scripts.Select(s => | ||
{ | ||
var script = CSharpScript.Create<object>(s.Script, | ||
ScriptOptions.Default | ||
.WithReferences(s.References) | ||
.WithImports(s.Usings), | ||
globalsType: typeof(Globals)); | ||
script.Compile(); | ||
return script; | ||
}).ToList(); | ||
} | ||
|
||
public async Task Invoke(HttpContext context) | ||
{ | ||
var scriptSettings = _settings.Scripts.LastOrDefault(s => | ||
s.Methods.Contains(context.Request.Method) && s.Paths.Any(context.Request.Path.Value.Contains)); | ||
|
||
if (scriptSettings == null) | ||
{ | ||
await _next(context); | ||
return; | ||
} | ||
|
||
var idx = _settings.Scripts.IndexOf(scriptSettings); | ||
var script = _scripts[idx]; | ||
|
||
var originalStream = context.Response.Body; | ||
|
||
using (var ms = new MemoryStream()) | ||
{ | ||
context.Response.Body = ms; | ||
|
||
await _next(context); | ||
|
||
var bodyString = Encoding.UTF8.GetString((context.Response.Body as MemoryStream).ToArray()); | ||
var globalObject = new Globals | ||
{ | ||
_Context = context, | ||
_CollectionId = ObjectHelper.GetCollectionFromPath(context.Request.Path.Value), | ||
_Body = ObjectHelper.RemoveLiterals(bodyString), | ||
_Method = context.Request.Method | ||
}; | ||
|
||
var scriptResult = await script.RunAsync(globalObject); | ||
|
||
// HACK: Remove string quotemarks from around the _Body | ||
// Original body is e.g. in an array: [{\"id\":1,\"name\":\"Jame\s\"}] | ||
// Script will return new { Data = _Body } | ||
// Script will set Data as a string { Data = "[{\"id\":1,\"name\":\"Jame\s\"}]" } | ||
|
||
var jsonCleared = ObjectHelper.RemoveLiterals(JsonConvert.SerializeObject(scriptResult.ReturnValue)); | ||
|
||
var bodyCleanStart = jsonCleared.IndexOf(globalObject._Body); | ||
|
||
if (bodyCleanStart != -1) | ||
{ | ||
var bodyCleanEnd = bodyCleanStart + globalObject._Body.Length; | ||
|
||
jsonCleared = jsonCleared | ||
.Remove(bodyCleanEnd, 1) | ||
.Remove(bodyCleanStart - 1, 1); | ||
} | ||
|
||
var byteArray = Encoding.ASCII.GetBytes(jsonCleared); | ||
var stream = new MemoryStream(byteArray); | ||
await stream.CopyToAsync(originalStream); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace FakeServer.CustomResponse | ||
{ | ||
public class CustomResponseSettings | ||
{ | ||
public bool Enabled { get; set; } | ||
public List<ScriptSettings> Scripts { get; set; } | ||
} | ||
|
||
public class ScriptSettings | ||
{ | ||
public string Script { get; set; } | ||
public List<string> Methods { get; set; } | ||
public List<string> Paths { get; set; } | ||
public List<string> Usings { get; set; } | ||
public List<string> References { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters