Skip to content

Commit b39ab12

Browse files
committed
Minor refactoring: Moved some classes to own files
1 parent bca06e8 commit b39ab12

File tree

4 files changed

+254
-224
lines changed

4 files changed

+254
-224
lines changed

UnitTests/CheckParametersUsage.cs

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using netmockery;
6+
using Xunit;
7+
using System.IO;
8+
9+
namespace UnitTests
10+
{
11+
public class CheckParametersUsage : IDisposable
12+
{
13+
private Endpoint endpoint;
14+
private EndpointParameter filenameParam;
15+
private EndpointParameter contenttypeParam;
16+
private EndpointParameter fooParam;
17+
private EndpointParameter delayParam;
18+
private EndpointParameter statusCodeParam;
19+
private DirectoryCreator dc = new DirectoryCreator();
20+
21+
public CheckParametersUsage()
22+
{
23+
endpoint = new Endpoint("foo", "bar");
24+
endpoint.Directory = dc.DirectoryName;
25+
filenameParam = new EndpointParameter
26+
{
27+
Name = "filename",
28+
DefaultValue = "file.txt"
29+
};
30+
contenttypeParam = new EndpointParameter
31+
{
32+
Name = "contenttype",
33+
DefaultValue = "text/plain"
34+
};
35+
fooParam = new EndpointParameter
36+
{
37+
Name = "foo",
38+
DefaultValue = "FOO"
39+
};
40+
delayParam = new EndpointParameter
41+
{
42+
Name = "delay",
43+
DefaultValue = "0"
44+
};
45+
statusCodeParam = new EndpointParameter
46+
{
47+
Name = "statuscode",
48+
DefaultValue = "200"
49+
};
50+
endpoint.AddParameter(filenameParam);
51+
endpoint.AddParameter(contenttypeParam);
52+
endpoint.AddParameter(fooParam);
53+
endpoint.AddParameter(delayParam);
54+
endpoint.AddParameter(statusCodeParam);
55+
}
56+
57+
public void Dispose()
58+
{
59+
dc.Dispose();
60+
}
61+
62+
[Fact]
63+
public void ParamNamesMustBeUnique()
64+
{
65+
Assert.Throws<ArgumentException>(() => { endpoint.AddParameter(filenameParam); });
66+
}
67+
68+
[Fact]
69+
public void LookupOfMissingParameterGivesError()
70+
{
71+
var ae = Assert.Throws<ArgumentException>(() => { endpoint.GetParameter("foobar"); });
72+
Assert.Equal("Endpoint parameter 'foobar' not found", ae.Message);
73+
}
74+
75+
[Fact]
76+
public void GetParameterByWrongIndexGivesError()
77+
{
78+
var ae = Assert.Throws<ArgumentException>(() => { endpoint.GetParameter(-1); });
79+
Assert.Equal("Invalid parameter index -1", ae.Message);
80+
81+
ae = Assert.Throws<ArgumentException>(() => { endpoint.GetParameter(endpoint.ParameterCount); });
82+
Assert.Equal("Invalid parameter index 5", ae.Message);
83+
}
84+
85+
[Fact]
86+
public void LookupsWorkAsExpected()
87+
{
88+
Assert.Equal("filename", endpoint.GetParameter("filename").Name);
89+
Assert.Equal("filename", endpoint.GetParameter(0).Name);
90+
91+
Assert.Equal("statuscode", endpoint.GetParameter(endpoint.ParameterCount - 1).Name);
92+
}
93+
94+
[Fact]
95+
public void CanUseParamForFilename()
96+
{
97+
dc.AddFile("file.txt", "CONTENTS0");
98+
dc.AddFile("otherfile.txt", "CONTENTS1");
99+
100+
var responseCreator = new FileResponse("$filename", endpoint);
101+
Assert.Equal(Path.Combine(endpoint.Directory, "file.txt"), responseCreator.Filename);
102+
Assert.Equal("CONTENTS0", GetResponse(responseCreator).WrittenContent);
103+
104+
filenameParam.Value = "otherfile.txt";
105+
Assert.Equal(Path.Combine(endpoint.Directory, "otherfile.txt"), responseCreator.Filename);
106+
Assert.Equal("CONTENTS1", GetResponse(responseCreator).WrittenContent);
107+
}
108+
109+
[Fact]
110+
public void CanUseParamForContenttype()
111+
{
112+
dc.AddFile("file.txt", "Heisann");
113+
var responseCreator = new FileResponse("file.txt", endpoint);
114+
responseCreator.ContentType = "$contenttype";
115+
116+
Assert.Equal("text/plain", responseCreator.ContentType);
117+
Assert.Equal("text/plain; charset=utf-8", GetResponse(responseCreator).ContentType);
118+
119+
contenttypeParam.Value = "application/xml";
120+
Assert.Equal("application/xml; charset=utf-8", GetResponse(responseCreator).ContentType);
121+
}
122+
123+
[Fact]
124+
public void CanUseParamForLiteral()
125+
{
126+
var responseCreator = new LiteralResponse("$foo", endpoint);
127+
Assert.Equal("FOO", responseCreator.GetBody(null));
128+
129+
fooParam.Value = "BAR";
130+
Assert.Equal("BAR", responseCreator.GetBody(null));
131+
Assert.Equal("BAR", GetResponse(responseCreator).WrittenContent);
132+
}
133+
134+
TestableHttpResponse GetResponse(ResponseCreator responseCreator)
135+
{
136+
var request = new TestableHttpRequest(null, null);
137+
var retval = new TestableHttpResponse();
138+
var bytesWritten = responseCreator.CreateResponseAsync(request, new byte[0], retval, endpoint).Result;
139+
return retval;
140+
}
141+
142+
[Fact]
143+
public void CanUseParamForScriptFilename()
144+
{
145+
dc.AddFile("file.txt", "return \"I am file.txt: \" + GetParam(\"filename\");");
146+
dc.AddFile("another.txt", "return \"I am another.txt: \" + GetParam(\"filename\");");
147+
148+
var responseCreator = new FileDynamicResponseCreator("$filename", endpoint);
149+
Assert.Equal(Path.Combine(endpoint.Directory, "file.txt"), responseCreator.Filename);
150+
Assert.Equal("I am file.txt: file.txt", GetResponse(responseCreator).WrittenContent);
151+
152+
filenameParam.Value = "another.txt";
153+
Assert.Equal(Path.Combine(endpoint.Directory, "another.txt"), responseCreator.Filename);
154+
Assert.Equal("I am another.txt: another.txt", GetResponse(responseCreator).WrittenContent);
155+
}
156+
157+
[Fact]
158+
public void CanUseParamForDelay()
159+
{
160+
var responseCreator = new FileDynamicResponseCreator("file.txt", endpoint);
161+
responseCreator.SetDelayFromString("$delay");
162+
163+
Assert.Equal(0, responseCreator.Delay);
164+
delayParam.Value = "1000";
165+
Assert.Equal(1000, responseCreator.Delay);
166+
}
167+
}
168+
}

0 commit comments

Comments
 (0)