internal MockResults SetupHttpAbstractionMocking() { var results = new MockResults(); // Locate the test manager so we can override the lower layer. var manager = ServiceLocator.Instance.Locate <IServiceLocationIndividualTestManager>(); // Create a Mock of the HTTP layer response. var moqResponse = new Moq.Mock <IHttpResponseMessageAbstraction>(MockBehavior.Loose); // Always return 200 OK moqResponse.SetupGet(res => res.StatusCode) .Returns(HttpStatusCode.OK); // Create a mock of the Request client. var moqClient = new Moq.Mock <IHttpClientAbstraction>(MockBehavior.Loose); // Mock the return to set the request headers. moqClient.SetupGet(client => client.RequestHeaders) .Returns(() => results.Headers); // Mock the return to set the request uri. moqClient.SetupGet(client => client.RequestUri) .Returns(() => results.RequestUri); // Mock the return to set the request uri. moqClient.SetupSet(abstraction => abstraction.RequestUri = It.IsAny <Uri>()).Callback <Uri>(uri => results.RequestUri = uri); // Mock the return to set the http method. moqClient.SetupGet(client => client.Method) .Returns(() => results.Method); // Mock the return to set the http method. moqClient.SetupSet(abstraction => abstraction.Method = It.IsAny <HttpMethod>()).Callback <HttpMethod>(method => results.Method = method); // Mock the return to set the content. moqClient.SetupGet(client => client.Content) .Returns(() => results.Content); moqClient.SetupSet(abstraction => abstraction.Content = It.IsAny <HttpContent>()).Callback <HttpContent>(content => results.Content = content); // Mock the SendAsync method (to just return the response object previously created). moqClient.Setup(c => c.SendAsync()) .Returns(() => Task.Run(() => { results.SendAsyncCalled = true; return(moqResponse.Object); })); // Mock the factory to return our mock client. var moqFactory = new Moq.Mock <IHttpClientAbstractionFactory>(); // Overload both create methods. moqFactory.Setup(fac => fac.Create(It.IsAny <X509Certificate2>(), It.IsAny <HDInsight.IAbstractionContext>(), false)) .Returns(() => moqClient.Object); moqFactory.Setup(fac => fac.Create(It.IsAny <HDInsight.IAbstractionContext>(), false)) .Returns(() => moqClient.Object); // Override the factory in the Service Locator (for this test only). manager.Override <IHttpClientAbstractionFactory>(moqFactory.Object); return(results); }
internal MockResults SetupHttpAbstractionMocking() { var results = new MockResults(); // Locate the test manager so we can override the lower layer. var manager = ServiceLocator.Instance.Locate<IServiceLocationIndividualTestManager>(); // Create a Mock of the HTTP layer response. var moqResponse = new Moq.Mock<IHttpResponseMessageAbstraction>(MockBehavior.Loose); // Always return 200 OK moqResponse.SetupGet(res => res.StatusCode) .Returns(HttpStatusCode.OK); // Create a mock of the Request client. var moqClient = new Moq.Mock<IHttpClientAbstraction>(MockBehavior.Loose); // Mock the return to set the request headers. moqClient.SetupGet(client => client.RequestHeaders) .Returns(() => results.Headers); // Mock the return to set the request uri. moqClient.SetupGet(client => client.RequestUri) .Returns(() => results.RequestUri); // Mock the return to set the request uri. moqClient.SetupSet(abstraction => abstraction.RequestUri = It.IsAny<Uri>()).Callback<Uri>(uri => results.RequestUri = uri); // Mock the return to set the http method. moqClient.SetupGet(client => client.Method) .Returns(() => results.Method); // Mock the return to set the http method. moqClient.SetupSet(abstraction => abstraction.Method = It.IsAny<HttpMethod>()).Callback<HttpMethod>(method => results.Method = method); // Mock the return to set the content. moqClient.SetupGet(client => client.Content) .Returns(() => results.Content); moqClient.SetupSet(abstraction => abstraction.Content = It.IsAny<HttpContent>()).Callback<HttpContent>(content => results.Content = content); // Mock the SendAsync method (to just return the response object previously created). moqClient.Setup(c => c.SendAsync()) .Returns(() => Task.Run(() => { results.SendAsyncCalled = true; return moqResponse.Object; })); // Mock the factory to return our mock client. var moqFactory = new Moq.Mock<IHttpClientAbstractionFactory>(); // Overload both create methods. moqFactory.Setup(fac => fac.Create(It.IsAny<X509Certificate2>(), It.IsAny<HDInsight.IAbstractionContext>(), false)) .Returns(() => moqClient.Object); moqFactory.Setup(fac => fac.Create(It.IsAny<HDInsight.IAbstractionContext>(), false)) .Returns(() => moqClient.Object); // Override the factory in the Service Locator (for this test only). manager.Override<IHttpClientAbstractionFactory>(moqFactory.Object); return results; }