Skip to content

Commit ec30bb4

Browse files
committed
Parameter validation
1 parent 6e5f4e7 commit ec30bb4

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

UnitTests/TestEndpointWithParams.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,55 @@ private EndpointCollection CreateEndpointWithScript()
2929
return EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName);
3030
}
3131

32+
private EndpointCollection CreateEndpointWithParam(JSONParam jsonParam)
33+
{
34+
dc.AddFile("endpoint\\endpoint.json", JsonConvert.SerializeObject(DataUtils.CreateScriptEndpoint("endpoint", "script.csscript")));
35+
dc.AddFile("endpoint\\script.csscript", "return GetParam(\"greeting\") + \" \" + GetParam(\"subject\");");
36+
dc.AddFile("endpoint\\params.json", JsonConvert.SerializeObject(new[] {
37+
jsonParam
38+
}));
39+
40+
return EndpointCollectionReader.ReadFromDirectory(dc.DirectoryName);
41+
}
42+
43+
[Fact]
44+
public void MissingNameGivesError()
45+
{
46+
var ae = Assert.Throws<ArgumentException>(() => {
47+
CreateEndpointWithParam(new JSONParam { @default = "foo", description = "bar" });
48+
});
49+
Assert.Equal("Parameter missing name", ae.Message);
50+
}
51+
52+
[Fact]
53+
public void InvalidNameGivesError()
54+
{
55+
var ae = Assert.Throws<ArgumentException>(() => {
56+
CreateEndpointWithParam(new JSONParam { name = "æøå", @default = "foo", description = "bar" });
57+
});
58+
Assert.Equal("Invalid parameter name: 'æøå'", ae.Message);
59+
}
60+
61+
[Fact]
62+
public void MissingDefaultGivesError()
63+
{
64+
var ae = Assert.Throws<ArgumentException>(() => {
65+
CreateEndpointWithParam(new JSONParam { name = "abc", description = "bar" });
66+
});
67+
68+
Assert.Equal("Missing default value for parameter 'abc'", ae.Message);
69+
}
70+
71+
[Fact]
72+
public void MissingDescriptionGivesError()
73+
{
74+
var ae = Assert.Throws<ArgumentException>(() => {
75+
CreateEndpointWithParam(new JSONParam { name = "abc", @default="bar" });
76+
});
77+
Assert.Equal("Missing description for parameter 'abc'", ae.Message);
78+
}
79+
80+
3281
[Fact]
3382
public void ValueAndDefaultWorksAsExpected()
3483
{
@@ -41,6 +90,11 @@ public void ValueAndDefaultWorksAsExpected()
4190
Assert.False(ep.ValueIsDefault);
4291
}
4392

93+
public void RequireNameDefaultAndDescription()
94+
{
95+
96+
}
97+
4498
[Fact]
4599
public void CanReadParams()
46100
{

netmockery/JSONReader.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
using System.Diagnostics;
77
using System.IO;
88
using System.Text;
9+
using System.Text.RegularExpressions;
910
using System.Net;
1011

12+
1113
namespace netmockery
1214
{
1315
public static class JSONReader
@@ -23,6 +25,31 @@ public class JSONParam
2325
public string name;
2426
public string @default;
2527
public string description;
28+
29+
public JSONParam Validated()
30+
{
31+
if (name == null)
32+
{
33+
throw new ArgumentException($"Parameter missing name");
34+
}
35+
36+
if (! Regex.IsMatch(name, "^[a-zA-Z_]+$"))
37+
{
38+
throw new ArgumentException($"Invalid parameter name: '{name}'");
39+
}
40+
41+
if (@default == null)
42+
{
43+
throw new ArgumentException($"Missing default value for parameter '{name}'");
44+
}
45+
46+
if (description == null)
47+
{
48+
throw new ArgumentException($"Missing description for parameter '{name}'");
49+
}
50+
51+
return this;
52+
}
2653
}
2754

2855
public class JSONTest
@@ -282,7 +309,7 @@ public Endpoint CreateEndpoint(string rootDir, JSONDefaults globalDefaults)
282309
var paramsFile = Path.Combine(rootDir, "params.json");
283310
if (File.Exists(paramsFile))
284311
{
285-
var jsonParams = JsonConvert.DeserializeObject<JSONParam[]>(File.ReadAllText(paramsFile));
312+
var jsonParams = from item in JsonConvert.DeserializeObject<JSONParam[]>(File.ReadAllText(paramsFile)) select item.Validated();
286313
foreach (var jsonParam in jsonParams)
287314
{
288315
endpoint.AddParameter(new EndpointParameter

0 commit comments

Comments
 (0)