public async Task InvokeAsync_ResourceInspectors_AreInvokedInSequence() { int order = 0; var syncInspector = new Mock <IHalResourceInspector>(); syncInspector.Setup(i => i.OnInspectingResource(It.IsAny <HalResourceInspectingContext>())) .Callback(() => { Assert.AreEqual(0, order++); }) .Verifiable(); syncInspector.Setup(i => i.OnInspectedResource(It.IsAny <HalResourceInspectedContext>())) .Callback(() => { Assert.AreEqual(2, order++); }) .Verifiable(); var asyncInspector = new Mock <IAsyncHalResourceInspector>(); asyncInspector.Setup(i => i.OnResourceInspectionAsync(It.IsAny <HalResourceInspectingContext>(), It.IsAny <HalResourceInspectionDelegate>())) .Callback(() => { Assert.AreEqual(1, order++); }) .Returns <HalResourceInspectingContext, HalResourceInspectionDelegate>((context, n) => Task.FromResult(new HalResourceInspectedContext(context))) .Verifiable(); var inspectors = new IHalResourceInspectorMetadata[] { syncInspector.Object, asyncInspector.Object }; var invoker = new HalResourceInspectorInvoker(inspectors, NullLogger <HalResourceInspectorInvoker> .Instance); await invoker.InspectAsync( new HalResourceInspectingContext( Mock.Of <IResource>(), Mock.Of <ActionContext>(), true, Mock.Of <ResourcePipeline>(), new MvcPipeline((HttpContext context) => Task.CompletedTask), Mock.Of <object>())); syncInspector.Verify(); asyncInspector.Verify(); }
private async Task <HalResourceInspectedContext> Next() { // TODO: Validate the inspectingcontext here to make sure the last pipeline component did not do something illegal. // If the last inspector was invoked, create the inspectedcontext. if (currentIndex >= inspectorsLength) { return(new HalResourceInspectedContext(inspectingContext)); } HalResourceInspectedContext result = null; currentInspector = this.inspectors[currentIndex++]; if (currentInspector is IAsyncHalResourceInspector asyncInspector) { result = await asyncInspector.OnResourceInspectionAsync(inspectingContext, Next); } else if (currentInspector is IHalResourceInspector syncInspector) { syncInspector.OnInspectingResource(inspectingContext); result = await Next(); syncInspector.OnInspectedResource(result); } else { throw new InvalidOperationException( $"inspector of type '{currentInspector.GetType().FullName}' is not an IHalResourceInspector or IAsyncHalResourceInspector."); } // TODO: Validate the inspectedcontext here to make sure the last pipeline component did not do something illegal. if (result == null) { throw new InvalidOperationException($"HalResourceInspectedContext was null after invoking '{currentInspector.GetType().FullName}'."); } return(result); }