Skip to content

Commit faa1243

Browse files
author
Bernt Røskar Brenna
committed
Scripts: SetParam, GetScriptObject
1 parent 04c5641 commit faa1243

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

UnitTests/TestDynamicResponse.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,40 @@ public async Task UsingSystemDiagnosticsDebug()
126126
{
127127
Assert.Equal("OK", await EvalAsync("using System.Diagnostics; Debug.Assert(true); return \"OK\";"));
128128
}
129+
130+
[Fact]
131+
public async Task ScriptsCanSetParams()
132+
{
133+
var endpoint = new Endpoint("foo", "bar");
134+
endpoint.AddParameter(new EndpointParameter { Name = "param", Value = "abc" });
135+
136+
Assert.Equal("abc", await EvalAsync("return GetParam(\"param\");", new RequestInfo { Endpoint = endpoint }));
137+
138+
await EvalAsync("SetParam(\"param\", \"def\"); return \"\";", new RequestInfo { Endpoint = endpoint });
139+
140+
Assert.Equal("def", await EvalAsync("return GetParam(\"param\");", new RequestInfo { Endpoint = endpoint }));
141+
}
142+
143+
[Fact]
144+
public async Task ScriptsCanHandleEndpointObjects()
145+
{
146+
var endpoint = new Endpoint("foo", "bar");
147+
148+
var obj = (Dictionary <string, string>) endpoint.GetScriptObject("obj", () => new Dictionary<string, string>());
149+
150+
obj["a"] = "b";
151+
Assert.Equal(
152+
"b",
153+
await EvalAsync("using System.Collections.Generic; return ((Dictionary<string, string>)GetScriptObject(\"obj\", null))[\"a\"];", new RequestInfo { Endpoint = endpoint })
154+
);
155+
156+
obj["a"] = "c";
157+
Assert.Equal(
158+
"c",
159+
await EvalAsync("using System.Collections.Generic; return ((Dictionary<string, string>)GetScriptObject(\"obj\", null))[\"a\"];", new RequestInfo { Endpoint = endpoint })
160+
);
161+
162+
}
129163
}
130164

131165
public class TestCheckScriptModifications : IDisposable

netmockery/Endpoint.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Endpoint
2222
private string _pathregex;
2323
private List<Tuple<RequestMatcher, ResponseCreator>> _responses = new List<Tuple<RequestMatcher, ResponseCreator>>();
2424
private List<EndpointParameter> _parameters = new List<EndpointParameter>();
25+
private Dictionary<string, object> _scriptObjects = new Dictionary<string, object>();
2526
private bool _ruleThatCatchesEveryThingHasBeenAdded = false;
2627

2728
public Endpoint(string name, string pathregex)
@@ -95,6 +96,16 @@ public EndpointParameter GetParameter(int parameterIndex)
9596

9697
public int ParameterCount => _parameters.Count;
9798

99+
public object GetScriptObject(string name, Func<object> objectCreator)
100+
{
101+
if (!_scriptObjects.ContainsKey(name))
102+
{
103+
_scriptObjects[name] = objectCreator();
104+
}
105+
return _scriptObjects[name];
106+
107+
}
108+
98109
public void Add(RequestMatcher requestMatcher, ResponseCreator responseCreator)
99110
{
100111
Debug.Assert(requestMatcher != null);

netmockery/ResponseCreator.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public interface IResponseCreatorWithFilename
8686

8787
public class RequestInfo
8888
{
89+
private static object _locker = new object();
90+
8991
private DateTime _now = DateTime.MinValue;
9092

9193
public string RequestPath;
@@ -101,6 +103,16 @@ public string GetParam(string name)
101103
return Endpoint.GetParameter(name).Value;
102104
}
103105

106+
public void SetParam(string name, string value)
107+
{
108+
lock (_locker)
109+
{
110+
Endpoint.GetParameter(name).Value = value;
111+
}
112+
}
113+
114+
public object GetScriptObject(string name, Func<object> objectCreator) => Endpoint.GetScriptObject(name, objectCreator);
115+
104116
public void SetStaticNow(DateTime now)
105117
{
106118
_now = now;

0 commit comments

Comments
 (0)