-
Notifications
You must be signed in to change notification settings - Fork 780
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test suite to analyze ASP.NET Core routing scenarios
- Loading branch information
Showing
21 changed files
with
1,775 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...tation.AspNetCore.Tests/RouteTests/Areas/AnotherArea/Controllers/AnotherAreaController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// <copyright file="AnotherAreaController.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
#nullable disable | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace RouteTests.Controllers; | ||
|
||
[Area("AnotherArea")] | ||
public class AnotherAreaController : Controller | ||
{ | ||
public IActionResult Index() => this.Ok(); | ||
} |
29 changes: 29 additions & 0 deletions
29
...ion.AspNetCore.Tests/RouteTests/Areas/MyArea/Controllers/ControllerForMyAreaController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// <copyright file="ControllerForMyAreaController.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
#nullable disable | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace RouteTests.Controllers; | ||
|
||
[Area("MyArea")] | ||
public class ControllerForMyAreaController : Controller | ||
{ | ||
public IActionResult Default() => this.Ok(); | ||
|
||
public IActionResult NonDefault() => this.Ok(); | ||
} |
119 changes: 119 additions & 0 deletions
119
...OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/AspNetCoreDiagnosticObserver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// <copyright file="AspNetCoreDiagnosticObserver.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
#nullable enable | ||
|
||
using System.Diagnostics; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc.Diagnostics; | ||
|
||
namespace RouteTests; | ||
|
||
internal sealed class AspNetCoreDiagnosticObserver : IDisposable, IObserver<DiagnosticListener>, IObserver<KeyValuePair<string, object?>> | ||
{ | ||
internal const string OnStartEvent = "Microsoft.AspNetCore.Hosting.HttpRequestIn.Start"; | ||
internal const string OnStopEvent = "Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop"; | ||
internal const string OnMvcBeforeActionEvent = "Microsoft.AspNetCore.Mvc.BeforeAction"; | ||
|
||
private readonly List<IDisposable> listenerSubscriptions; | ||
private IDisposable? allSourcesSubscription; | ||
private long disposed; | ||
|
||
public AspNetCoreDiagnosticObserver() | ||
{ | ||
this.listenerSubscriptions = new List<IDisposable>(); | ||
this.allSourcesSubscription = DiagnosticListener.AllListeners.Subscribe(this); | ||
} | ||
|
||
public void OnNext(DiagnosticListener value) | ||
{ | ||
if (value.Name == "Microsoft.AspNetCore") | ||
{ | ||
var subscription = value.Subscribe(this); | ||
|
||
lock (this.listenerSubscriptions) | ||
{ | ||
this.listenerSubscriptions.Add(subscription); | ||
} | ||
} | ||
} | ||
|
||
public void OnNext(KeyValuePair<string, object?> value) | ||
{ | ||
HttpContext? context; | ||
BeforeActionEventData? actionMethodEventData; | ||
RouteInfo? info; | ||
|
||
switch (value.Key) | ||
{ | ||
case OnStartEvent: | ||
context = value.Value as HttpContext; | ||
Debug.Assert(context != null, "HttpContext was null"); | ||
info = new RouteInfo(); | ||
info.SetValues(context); | ||
context.Items["RouteInfo"] = info; | ||
break; | ||
case OnMvcBeforeActionEvent: | ||
actionMethodEventData = value.Value as BeforeActionEventData; | ||
Debug.Assert(actionMethodEventData != null, $"expected {nameof(BeforeActionEventData)}"); | ||
info = actionMethodEventData.HttpContext.Items["RouteInfo"] as RouteInfo; | ||
Debug.Assert(info != null, "RouteInfo object not present in context.Items"); | ||
info.SetValues(actionMethodEventData.HttpContext); | ||
info.SetValues(actionMethodEventData.ActionDescriptor); | ||
break; | ||
case OnStopEvent: | ||
// Can't update RouteInfo here because the response is already written. | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
public void OnCompleted() | ||
{ | ||
} | ||
|
||
public void OnError(Exception error) | ||
{ | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
private void Dispose(bool disposing) | ||
{ | ||
if (Interlocked.CompareExchange(ref this.disposed, 1, 0) == 1) | ||
{ | ||
return; | ||
} | ||
|
||
lock (this.listenerSubscriptions) | ||
{ | ||
foreach (var listenerSubscription in this.listenerSubscriptions) | ||
{ | ||
listenerSubscription?.Dispose(); | ||
} | ||
|
||
this.listenerSubscriptions.Clear(); | ||
} | ||
|
||
this.allSourcesSubscription?.Dispose(); | ||
this.allSourcesSubscription = null; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...metry.Instrumentation.AspNetCore.Tests/RouteTests/Controllers/AttributeRouteController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// <copyright file="AttributeRouteController.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
#nullable disable | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace RouteTests.Controllers; | ||
|
||
[ApiController] | ||
[Route("[controller]")] | ||
public class AttributeRouteController : ControllerBase | ||
{ | ||
[HttpGet] | ||
[HttpGet("[action]")] | ||
public IActionResult Get() => this.Ok(); | ||
|
||
[HttpGet("[action]/{id}")] | ||
public IActionResult Get(int id) => this.Ok(); | ||
|
||
[HttpGet("{id}/[action]")] | ||
public IActionResult GetWithActionNameInDifferentSpotInTemplate(int id) => this.Ok(); | ||
} |
30 changes: 30 additions & 0 deletions
30
...ry.Instrumentation.AspNetCore.Tests/RouteTests/Controllers/ConventionalRouteController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// <copyright file="ConventionalRouteController.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
#nullable disable | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace RouteTests.Controllers; | ||
|
||
public class ConventionalRouteController : Controller | ||
{ | ||
public IActionResult Default() => this.Ok(); | ||
|
||
public IActionResult ActionWithParameter(int id) => this.Ok(); | ||
|
||
public IActionResult ActionWithStringParameter(string id, int num) => this.Ok(); | ||
} |
40 changes: 40 additions & 0 deletions
40
test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/DebugInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// <copyright file="DebugInfo.cs" company="OpenTelemetry Authors"> | ||
// Copyright The OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// </copyright> | ||
|
||
#nullable enable | ||
|
||
namespace RouteTests; | ||
|
||
public class DebugInfo | ||
{ | ||
public string? RawText { get; set; } | ||
|
||
public string? RouteDiagnosticMetadata { get; set; } | ||
|
||
public IDictionary<string, string?>? RouteData { get; set; } | ||
|
||
public string? AttributeRouteInfo { get; set; } | ||
|
||
public IList<string>? ActionParameters { get; set; } | ||
|
||
public string? PageActionDescriptorRelativePath { get; set; } | ||
|
||
public string? PageActionDescriptorViewEnginePath { get; set; } | ||
|
||
public string? ControllerActionDescriptorControllerName { get; set; } | ||
|
||
public string? ControllerActionDescriptorActionName { get; set; } | ||
} |
2 changes: 2 additions & 0 deletions
2
test/OpenTelemetry.Instrumentation.AspNetCore.Tests/RouteTests/Pages/Index.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@page | ||
Hello, OpenTelemetry! |
4 changes: 4 additions & 0 deletions
4
...elemetry.Instrumentation.AspNetCore.Tests/RouteTests/Pages/PageThatThrowsException.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@page | ||
@{ | ||
throw new Exception("Oops."); | ||
} |
Oops, something went wrong.