Files
acceptance-tests
data
debian
docs
external
Newtonsoft.Json
api-doc-tools
api-snapshot
aspnetwebstack
packages
src
test
Microsoft.TestCommon
Microsoft.Web.Helpers.Test
Microsoft.Web.Http.Data.Test
Microsoft.Web.Mvc.Test
Microsoft.Web.WebPages.OAuth.Test
SPA.Test
System.Json.Test.Integration
System.Json.Test.Unit
System.Net.Http.Formatting.Test.Integration
System.Net.Http.Formatting.Test.Unit
System.Web.Helpers.Test
System.Web.Http.Integration.Test
System.Web.Http.SelfHost.Test
System.Web.Http.Test
System.Web.Http.WebHost.Test
System.Web.Mvc.Test
System.Web.Razor.Test
System.Web.WebPages.Administration.Test
System.Web.WebPages.Deployment.Test
System.Web.WebPages.Razor.Test
System.Web.WebPages.Test
ApplicationParts
Extensions
Helpers
Html
Instrumentation
InstrumentationServiceTest.cs
Mvc
Properties
ScopeStorage
TestFiles
Utils
Validation
WebPage
App.config
PreApplicationStartCodeTest.cs
System.Web.WebPages.Test.csproj
packages.config
WebMatrix.Data.Test
WebMatrix.WebData.Test
Settings.StyleCop
tools
.gitattributes
.gitignore
License.txt
README.md
Runtime.msbuild
Runtime.sln
Runtime.xunit
Settings.StyleCop
build.cmd
binary-reference-assemblies
bockbuild
boringssl
cecil
cecil-legacy
corefx
corert
helix-binaries
ikdasm
ikvm
illinker-test-assets
linker
llvm
nuget-buildtasks
nunit-lite
roslyn-binaries
rx
xunit-binaries
ikvm-native
libgc
llvm
m4
man
mcs
mk
mono
msvc
po
runtime
samples
scripts
support
tools
COPYING.LIB
LICENSE
Makefile.am
Makefile.in
NEWS
README.md
acinclude.m4
aclocal.m4
autogen.sh
code_of_conduct.md
compile
config.guess
config.h.in
config.rpath
config.sub
configure.REMOVED.git-id
configure.ac.REMOVED.git-id
depcomp
install-sh
ltmain.sh.REMOVED.git-id
missing
mkinstalldirs
mono-uninstalled.pc.in
test-driver
winconfig.h
linux-packaging-mono/external/aspnetwebstack/test/System.Web.WebPages.Test/Instrumentation/InstrumentationServiceTest.cs

100 lines
4.2 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Web.WebPages.Instrumentation;
using Xunit;
namespace System.Web.WebPages.Test.Instrumentation
{
public class InstrumentationServiceTest
{
[Fact]
public void BeginContextDelegatesToRegisteredListeners()
{
// Arrange
dynamic listener1 = CreateListener();
dynamic listener2 = CreateListener();
InstrumentationService inst = CreateInstrumentationService(listener1, listener2);
TextWriter mockWriter = new StringWriter();
// Act
inst.BeginContext(null, "Foo.cshtml", mockWriter, 42, 24, isLiteral: false);
// Assert
Assert.Equal(1, listener1.BeginContextCalls.Count);
Assert.Equal(0, listener1.EndContextCalls.Count);
Assert.Equal(1, listener2.BeginContextCalls.Count);
Assert.Equal(0, listener2.EndContextCalls.Count);
AssertContext("Foo.cshtml", mockWriter, 42, 24, false, listener1.BeginContextCalls[0]);
AssertContext("Foo.cshtml", mockWriter, 42, 24, false, listener2.BeginContextCalls[0]);
}
[Fact]
public void EndContextDelegatesToRegisteredListeners()
{
// Arrange
dynamic listener1 = CreateListener();
dynamic listener2 = CreateListener();
InstrumentationService inst = CreateInstrumentationService(listener1, listener2);
TextWriter mockWriter = new StringWriter();
// Act
inst.EndContext(null, "Foo.cshtml", mockWriter, 42, 24, isLiteral: false);
// Assert
Assert.Equal(1, listener1.EndContextCalls.Count);
Assert.Equal(0, listener1.BeginContextCalls.Count);
Assert.Equal(1, listener2.EndContextCalls.Count);
Assert.Equal(0, listener2.BeginContextCalls.Count);
AssertContext("Foo.cshtml", mockWriter, 42, 24, false, listener1.EndContextCalls[0]);
AssertContext("Foo.cshtml", mockWriter, 42, 24, false, listener2.EndContextCalls[0]);
}
private void AssertContext(string virtualPath, TextWriter writer, int startPosition, int length, bool isLiteral, dynamic context)
{
PageExecutionContextAdapter ctx = new PageExecutionContextAdapter(context);
Assert.Equal(virtualPath, ctx.VirtualPath);
Assert.Same(writer, ctx.TextWriter);
Assert.Equal(startPosition, ctx.StartPosition);
Assert.Equal(length, ctx.Length);
Assert.Equal(isLiteral, ctx.IsLiteral);
}
private InstrumentationService CreateInstrumentationService(params dynamic[] listeners)
{
dynamic service = new ExpandoObject();
service.ExecutionListeners = new List<dynamic>(listeners);
InstrumentationService inst = new InstrumentationService();
inst.IsAvailable = true;
inst.ExtractInstrumentationService = _ => new PageInstrumentationServiceAdapter(service);
inst.CreateContext = CreateExpandoContext;
return inst;
}
private dynamic CreateListener()
{
dynamic listener = new ExpandoObject();
listener.BeginContextCalls = new List<dynamic>();
listener.EndContextCalls = new List<dynamic>();
listener.BeginContext = (Action<dynamic>)(d => { listener.BeginContextCalls.Add(d); });
listener.EndContext = (Action<dynamic>)(d => { listener.EndContextCalls.Add(d); });
return listener;
}
private PageExecutionContextAdapter CreateExpandoContext(string virtualPath, TextWriter writer, int startPosition, int length, bool isLiteral)
{
dynamic ctx = new ExpandoObject();
ctx.VirtualPath = virtualPath;
ctx.TextWriter = writer;
ctx.StartPosition = startPosition;
ctx.Length = length;
ctx.IsLiteral = isLiteral;
return new PageExecutionContextAdapter(ctx);
}
}
}