/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file 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. */ using Amazon.Runtime; using Amazon.Runtime.Internal; using Amazon.Runtime.Internal.Auth; using Amazon.Runtime.Internal.Transform; using Amazon.Runtime.Internal.Util; using System; using System.Collections.Generic; namespace AWSSDK.UnitTests.Mocking { /// /// Generic class for testing utils /// public class TestUtils { /// /// Allows to mock pipeline to run tests using the default pipeline handlers /// /// Request to use /// Marshaller to use or null for none /// Unmarshaller to use or null for none /// ClientConfig to use /// Signer to use /// public static IRequest RunMockRequest(AmazonWebServiceRequest request, IMarshaller marshaller, ResponseUnmarshaller unmarshaller, ClientConfig config, AbstractAWSSigner signer) { var pipelineHandlers = new List { new NoopPipelineHandler(), new Signer(), new EndpointResolver(), new Marshaller() }; return RunMockRequest(pipelineHandlers, request, marshaller, unmarshaller, config, signer); } /// /// Allows to mock pipeline to run tests for custom pipeline handlers /// /// The custom pipeline handlers to test /// Request to use /// Marshaller to use or null for none /// Unmarshaller to use or null for none /// ClientConfig to use /// Signer to use /// public static IRequest RunMockRequest(List pipelineHandlers, AmazonWebServiceRequest request, IMarshaller marshaller, ResponseUnmarshaller unmarshaller, ClientConfig config, AbstractAWSSigner signer) { var pipeline = new RuntimePipeline(pipelineHandlers); var requestContext = new RequestContext(config.LogMetrics, signer) { ClientConfig = config, Marshaller = marshaller, OriginalRequest = request, Unmarshaller = unmarshaller, IsAsync = false, ImmutableCredentials = new ImmutableCredentials("access key", "secret", "token") }; var executionContext = new ExecutionContext( requestContext, new ResponseContext() ); pipeline.InvokeSync(executionContext); return requestContext.Request; } /// /// No op PipelineHandler, has to be on top of pipeline chain to enable mocking /// public class NoopPipelineHandler : IPipelineHandler { public ILogger Logger { get; set; } public IPipelineHandler InnerHandler { get; set; } public IPipelineHandler OuterHandler { get; set; } public void AsyncCallback(IAsyncExecutionContext executionContext) { } public IAsyncResult InvokeAsync(IAsyncExecutionContext executionContext) { return null; } public virtual System.Threading.Tasks.Task InvokeAsync(IExecutionContext executionContext) where T : AmazonWebServiceResponse, new() { return null; } public void InvokeSync(IExecutionContext executionContext) { } } } }