HttpAbstractions 767 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. commit 066c5ce337f1d43e0ccc62edeb4258c368f35dbc
  2. Author: James Newton-King <[email protected]>
  3. Date: Wed Nov 7 12:57:31 2018 +1300
  4. Implicitly execute matched endpoint at the end of middleware pipeline (#1059)
  5. diff --git a/src/Microsoft.AspNetCore.Http/Internal/ApplicationBuilder.cs b/src/Microsoft.AspNetCore.Http/Internal/ApplicationBuilder.cs
  6. index d0b6b6f6bfc..291ccc38936 100644
  7. --- a/src/Microsoft.AspNetCore.Http/Internal/ApplicationBuilder.cs
  8. +++ b/src/Microsoft.AspNetCore.Http/Internal/ApplicationBuilder.cs
  9. @@ -81,6 +81,13 @@ namespace Microsoft.AspNetCore.Builder.Internal
  10. {
  11. RequestDelegate app = context =>
  12. {
  13. + // Implicitly execute matched endpoint at the end of the pipeline instead of returning 404
  14. + var endpointRequestDelegate = context.GetEndpoint()?.RequestDelegate;
  15. + if (endpointRequestDelegate != null)
  16. + {
  17. + return endpointRequestDelegate(context);
  18. + }
  19. +
  20. context.Response.StatusCode = 404;
  21. return Task.CompletedTask;
  22. };
  23. diff --git a/test/Microsoft.AspNetCore.Http.Tests/Internal/ApplicationBuilderTests.cs b/test/Microsoft.AspNetCore.Http.Tests/Internal/ApplicationBuilderTests.cs
  24. index e1336c82ba0..cee2042aae7 100644
  25. --- a/test/Microsoft.AspNetCore.Http.Tests/Internal/ApplicationBuilderTests.cs
  26. +++ b/test/Microsoft.AspNetCore.Http.Tests/Internal/ApplicationBuilderTests.cs
  27. @@ -1,7 +1,9 @@
  28. // Copyright (c) .NET Foundation. All rights reserved.
  29. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  30. +using System.Threading.Tasks;
  31. using Microsoft.AspNetCore.Http;
  32. +using Microsoft.AspNetCore.Http.Features;
  33. using Xunit;
  34. namespace Microsoft.AspNetCore.Builder.Internal
  35. @@ -20,6 +22,59 @@ namespace Microsoft.AspNetCore.Builder.Internal
  36. Assert.Equal(404, httpContext.Response.StatusCode);
  37. }
  38. + [Fact]
  39. + public void BuildImplicitlyCallsMatchedEndpointAsLastStep()
  40. + {
  41. + var builder = new ApplicationBuilder(null);
  42. + var app = builder.Build();
  43. +
  44. + var endpointCalled = false;
  45. + var endpoint = new Endpoint(
  46. + context =>
  47. + {
  48. + endpointCalled = true;
  49. + return Task.CompletedTask;
  50. + },
  51. + EndpointMetadataCollection.Empty,
  52. + "Test endpoint");
  53. +
  54. + var httpContext = new DefaultHttpContext();
  55. + httpContext.SetEndpoint(endpoint);
  56. +
  57. + app.Invoke(httpContext);
  58. +
  59. + Assert.True(endpointCalled);
  60. + }
  61. +
  62. + [Fact]
  63. + public void BuildDoesNotCallMatchedEndpointWhenTerminated()
  64. + {
  65. + var builder = new ApplicationBuilder(null);
  66. + builder.Use((context, next) =>
  67. + {
  68. + // Do not call next
  69. + return Task.CompletedTask;
  70. + });
  71. + var app = builder.Build();
  72. +
  73. + var endpointCalled = false;
  74. + var endpoint = new Endpoint(
  75. + context =>
  76. + {
  77. + endpointCalled = true;
  78. + return Task.CompletedTask;
  79. + },
  80. + EndpointMetadataCollection.Empty,
  81. + "Test endpoint");
  82. +
  83. + var httpContext = new DefaultHttpContext();
  84. + httpContext.SetEndpoint(endpoint);
  85. +
  86. + app.Invoke(httpContext);
  87. +
  88. + Assert.False(endpointCalled);
  89. + }
  90. +
  91. [Fact]
  92. public void PropertiesDictionaryIsDistinctAfterNew()
  93. {