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
|
//-----------------------------------------------------------------------
// <copyright file="TestBase.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test {
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using DotNetOpenAuth.Logging;
using DotNetOpenAuth.Messaging.Reflection;
using DotNetOpenAuth.OAuth.Messages;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.Test.Performance;
using NUnit.Framework;
/// <summary>
/// The base class that all test classes inherit from.
/// </summary>
public class TestBase {
private MessageDescriptionCollection messageDescriptions = new MessageDescriptionCollection();
/// <summary>
/// Gets the logger that tests should use.
/// </summary>
internal static ILog TestLogger {
get { return TestUtilities.TestLogger; }
}
/// <summary>
/// Gets the full path to the directory that contains the test ASP.NET site.
/// </summary>
internal string TestWebDirectory {
get {
// System.IO.Path.GetDirectoryName(new System.Uri(basePath).LocalPath)
string basePath = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
string relativePath = @"src\DotNetOpenAuth.TestWeb";
for (int i = 0; !Directory.Exists(Path.Combine(basePath, relativePath)) && i < 4; i++) {
relativePath = "..\\" + relativePath;
}
return Path.GetFullPath(relativePath);
}
}
internal MessageDescriptionCollection MessageDescriptions {
get { return this.messageDescriptions; }
}
internal MockingHostFactories HostFactories { get; set; }
/// <summary>
/// The TestInitialize method for the test cases.
/// </summary>
[SetUp]
public virtual void SetUp() {
MessageBase.LowSecurityMode = true;
this.messageDescriptions = new MessageDescriptionCollection();
this.HostFactories = new MockingHostFactories();
SetMockHttpContext();
}
/// <summary>
/// The TestCleanup method for the test cases.
/// </summary>
[TearDown]
public virtual void Cleanup() {
}
internal static Stats MeasurePerformance(Func<Task> action, float maximumAllowedUnitTime, int samples = 10, int iterations = 100, string name = null) {
if (!PerformanceTestUtilities.IsOptimized(typeof(OpenIdRelyingParty).Assembly)) {
Assert.Inconclusive("Unoptimized code.");
}
var timer = new MultiSampleCodeTimer(samples, iterations);
Stats stats;
using (new HighPerformance()) {
stats = timer.Measure(name ?? TestContext.CurrentContext.Test.FullName, () => action().Wait());
}
stats.AdjustForScale(PerformanceTestUtilities.Baseline.Median);
TestUtilities.TestLogger.InfoFormat(
"Performance counters: median {0}, mean {1}, min {2}, max {3}, stddev {4} ({5}%).",
stats.Median,
stats.Mean,
stats.Minimum,
stats.Maximum,
stats.StandardDeviation,
stats.StandardDeviation / stats.Median * 100);
Assert.IsTrue(stats.Mean < maximumAllowedUnitTime, "The mean time of {0} exceeded the maximum allowable of {1}.", stats.Mean, maximumAllowedUnitTime);
TestUtilities.TestLogger.InfoFormat("Within {0}% of the maximum allowed time of {1}.", Math.Round((maximumAllowedUnitTime - stats.Mean) / maximumAllowedUnitTime * 100, 1), maximumAllowedUnitTime);
return stats;
}
/// <summary>
/// Sets HttpContext.Current to some empty (but non-null!) value.
/// </summary>
protected internal static void SetMockHttpContext() {
HttpContext.Current = new HttpContext(
new HttpRequest("mock", "http://mock", "mock"),
new HttpResponse(new StringWriter()));
}
protected internal Handler Handle(string uri) {
return new Handler(this, new Uri(uri));
}
protected internal Handler Handle(Uri uri) {
return new Handler(this, uri);
}
protected internal struct Handler {
private TestBase test;
internal Handler(TestBase test, Uri uri)
: this() {
this.test = test;
this.Uri = uri;
}
internal Uri Uri { get; private set; }
internal Func<HttpRequestMessage, Task<HttpResponseMessage>> MessageHandler { get; private set; }
internal void By(Func<HttpRequestMessage, CancellationToken, Task<HttpResponseMessage>> handler) {
this.test.HostFactories.Handlers[this.Uri] = req => handler(req, CancellationToken.None);
}
internal void By(Func<HttpRequestMessage, Task<HttpResponseMessage>> handler) {
this.test.HostFactories.Handlers[this.Uri] = handler;
}
internal void By(Func<HttpRequestMessage, HttpResponseMessage> handler) {
this.By(req => Task.FromResult(handler(req)));
}
internal void By(string responseContent, string contentType, HttpStatusCode statusCode = HttpStatusCode.OK) {
this.By(
req => {
var response = new HttpResponseMessage(statusCode);
response.Content = new StringContent(responseContent);
response.Content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
return response;
});
}
}
}
}
|