forked from sbozzie/test-intel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionTests.cs
More file actions
232 lines (205 loc) · 9.51 KB
/
Copy pathExtensionTests.cs
File metadata and controls
232 lines (205 loc) · 9.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
namespace PleaseIgnore.IntelMap.Tests {
/// <summary>
/// Tests of the <see cref="IntelExtensions"/> members
/// </summary>
[TestClass]
public class ExtensionTests {
const string testString = "ABC αβγ";
const string testStringEncoded = "ABC+%CE%B1%CE%B2%CE%B3";
/// <summary>
/// Tests the <see cref="IntelExtensions.LastDowntime"/> member.
/// </summary>
[TestMethod]
public void LastDowntime() {
var now = DateTime.UtcNow;
var lastDowntime = IntelExtensions.LastDowntime;
Assert.IsTrue(lastDowntime <= now);
Assert.IsTrue(now < lastDowntime + new TimeSpan(24, 0, 0));
Assert.AreEqual(new TimeSpan(11, 0, 0), lastDowntime - lastDowntime.Date);
}
/// <summary>
/// Tests the <see cref="IntelExtensions.NextDowntime"/> member.
/// </summary>
[TestMethod]
public void NextDowntime() {
var now = DateTime.UtcNow;
var nextDowntime = IntelExtensions.NextDowntime;
Assert.IsTrue(now < nextDowntime);
Assert.IsTrue(nextDowntime <= now + new TimeSpan(24, 0, 0));
Assert.AreEqual(new TimeSpan(11, 0, 0), nextDowntime - nextDowntime.Date);
}
/// <summary>
/// Tests the <see cref="IntelExtensions.ToUnixTime()"/> member.
/// </summary>
[TestMethod]
public void ToUnixTime() {
// Example from Wikipedia
Assert.AreEqual(1095357343, new DateTime(2004, 9, 16, 17, 55, 43, DateTimeKind.Utc).ToUnixTime());
}
/// <summary>
/// Tests the <see cref="IntelExtensions.Combine()"/> member.
/// </summary>
[TestMethod]
public void Combine() {
Assert.AreEqual(IntelStatus.Active,
IntelExtensions.Combine(
IntelStatus.Waiting,
IntelStatus.Active));
Assert.AreEqual(IntelStatus.Active,
IntelExtensions.Combine(
IntelStatus.Active,
IntelStatus.Waiting));
Assert.AreEqual(IntelStatus.FatalError,
IntelExtensions.Combine(
IntelStatus.FatalError,
IntelStatus.Active));
Assert.AreEqual(IntelStatus.FatalError,
IntelExtensions.Combine(
IntelStatus.Active,
IntelStatus.FatalError));
Assert.AreEqual(IntelStatus.InvalidPath,
IntelExtensions.Combine(
IntelStatus.InvalidPath,
IntelStatus.Active));
Assert.AreEqual(IntelStatus.InvalidPath,
IntelExtensions.Combine(
IntelStatus.Active,
IntelStatus.InvalidPath));
}
/// <summary>
/// Tests the <see cref="IntelExtensions.ToInt32()"/> member.
/// </summary>
[TestMethod]
public void ToInt32() {
var match = new Regex(@"(\d*)").Match("12345");
Assert.AreEqual(12345, match.Groups[1].ToInt32());
}
/// <summary>
/// Tests the <see cref="IntelExtensions.ToLowerHexString()"/> member.
/// </summary>
[TestMethod]
public void ToLowerHexString() {
var bytes = new byte[] { 0x12, 0x34, 0x56, 0xFE, 0xDC, 0xBA };
Assert.AreEqual("123456fedcba", bytes.ToLowerHexString());
}
/// <summary>
/// Tests the <see cref="IntelExtensions.WriteUriEncoded()"/> member.
/// </summary>
[TestMethod]
public void WriteUriEncoded() {
using (var stream = new MemoryStream()) {
stream.WriteUriEncoded(testString);
// Answer came from doing a Google search in Chrome
Assert.AreEqual(testStringEncoded, Encoding.UTF8.GetString(stream.ToArray()));
}
}
/// <summary>
/// Tests the <see cref="IntelExtensions.Post(WebRequest, byte[])"/> member.
/// </summary>
[TestMethod]
public void Post() {
var data = Encoding.UTF8.GetBytes(testString);
// The stream should receive data and only data
var mockStream = new Mock<Stream>(MockBehavior.Strict);
mockStream.Setup(x => x.Write(data, 0, data.Length));
mockStream.Setup(x => x.Close());
// No calls should be made on WebResponse
var mockResponse = new Mock<WebResponse>(MockBehavior.Strict);
var response = mockResponse.Object;
// The operation should set some properties, write the query, and give us the response
var mockRequest = new Mock<WebRequest>(MockBehavior.Strict);
mockRequest.SetupProperty(x => x.ContentLength);
mockRequest.SetupProperty(x => x.ContentType);
mockRequest.SetupProperty(x => x.Method);
mockRequest.Setup(x => x.GetRequestStream())
.Returns(mockStream.Object);
mockRequest.Setup(x => x.GetResponse())
.Returns(response);
// Perform the actual operation
var request = mockRequest.Object;
Assert.AreEqual(response, request.Post(data));
// Make sure the properties were set correctly
Assert.AreEqual("POST", request.Method);
Assert.AreEqual(data.Length, request.ContentLength);
Assert.AreEqual("application/x-www-form-urlencoded", request.ContentType);
// Make sure certain methods were called appropriately
mockRequest.Verify(x => x.GetRequestStream(), Times.Once());
mockStream.Verify(x => x.Write(data, 0, data.Length), Times.Once());
mockStream.Verify(x => x.Close(), Times.Once());
mockRequest.Verify(x => x.GetResponse(), Times.Once());
}
/// <summary>
/// Tests the <see cref="IntelExtensions.Post(WebRequest, IDictionary{string, string})"/> member.
/// </summary>
[TestMethod]
public void PostVariables() {
// For the request stream, we need to reconstruct the string written to the server
StringBuilder builder = new StringBuilder();
var byteCount = 0;
var mockStream = new Mock<Stream>(MockBehavior.Strict);
mockStream.Setup(x => x.Write(It.IsAny<byte[]>(), It.IsAny<int>(), It.IsAny<int>()))
.Callback((byte[] array, int offset, int count) => {
builder.Append(Encoding.UTF8.GetString(array, offset, count));
byteCount += count;
});
mockStream.Setup(x => x.WriteByte(It.IsAny<byte>()))
.Callback((byte ch) => {
builder.Append((char)ch);
++byteCount;
});
mockStream.Setup(x => x.Close());
// No operations should be performed on the WebResponse
var mockResponse = new Mock<WebResponse>(MockBehavior.Strict);
var response = mockResponse.Object;
// The operation should set some properties, write the query, and give us the response
var mockRequest = new Mock<WebRequest>(MockBehavior.Strict);
mockRequest.SetupProperty(x => x.ContentLength);
mockRequest.SetupProperty(x => x.ContentType);
mockRequest.SetupProperty(x => x.Method);
mockRequest.Setup(x => x.GetRequestStream())
.Returns(mockStream.Object);
mockRequest.Setup(x => x.GetResponse())
.Returns(response);
// Perform the actual operation
var request = mockRequest.Object;
Assert.AreEqual(response, request.Post(new Dictionary<string, string> {
{ "Key 1", testString },
{ "Key 2", "DEF GHI" }
}));
// Make sure the properties were set correctly
Assert.AreEqual("POST", request.Method);
Assert.AreEqual(byteCount, request.ContentLength);
Assert.AreEqual("application/x-www-form-urlencoded", request.ContentType);
Assert.AreEqual("Key+1=" + testStringEncoded + "&Key+2=DEF+GHI", builder.ToString());
// Make sure certain methods were called appropriately
mockRequest.Verify(x => x.GetRequestStream(), Times.Once());
mockStream.Verify(x => x.Close(), Times.Once());
mockRequest.Verify(x => x.GetResponse(), Times.Once());
}
/// <summary>
/// Tests the <see cref="IntelExtensions.ReadContent"/> member.
/// </summary>
[TestMethod]
public void ReadContent() {
var stream = new MemoryStream(Encoding.UTF8.GetBytes(testString), false);
var mockResponse = new Mock<WebResponse>(MockBehavior.Strict);
mockResponse.Setup(x => x.GetResponseStream())
.Returns(stream);
mockResponse.Setup(x => x.Close());
// Perform the actual operation
var response = mockResponse.Object;
Assert.AreEqual(testString, response.ReadContent());
// Make sure certain methods were called appropriately
mockResponse.Verify(x => x.GetResponseStream(), Times.Once());
mockResponse.Verify(x => x.Close(), Times.Once());
}
}
}