/// <summary> /// Create a <see cref="WebException"/> with the specified content. /// </summary> /// <param name="status">The status code to use in the exception.</param> /// <param name="content">A <see cref="MemoryStream"/> of the exception content.</param> /// <param name="contextHandler">An action that adds extra info to the response.</param> /// <returns>An <see cref="WebException"/> with the specified content.</returns> public static WebException CreateWebException( HttpStatusCode status, MemoryStream content, Action <HttpListenerContext> contextHandler) { HttpListener server = null; try { // Create a mock server that always returns the response code and exception stream // specified in the parameter. using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { MockHttpServer mockServer = new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri); server = mockServer.CreateListener( (context) => { contextHandler(context); context.Response.StatusCode = (int)status; content.Position = 0; content.CopyTo(context.Response.OutputStream); context.Response.Close(); }, 1); } WebClient client = new WebClient(); try { client.OpenRead(new Uri(DefaultServerPrefixUri, "exception.htm")); } catch (WebException ex) { return(ex); } } finally { server.Stop(); } return(null); }
/// <summary> /// Create a <see cref="WebException"/> with the specified content. /// </summary> /// <param name="status">The status code to use in the exception.</param> /// <param name="content">A <see cref="MemoryStream"/> of the exception content.</param> /// <param name="contextHandler">An action that adds extra info to the response.</param> /// <returns>An <see cref="WebException"/> with the specified content.</returns> public static WebException CreateWebException( HttpStatusCode status, MemoryStream content, Action<HttpListenerContext> contextHandler) { HttpListener server = null; try { // Create a mock server that always returns the response code and exception stream // specified in the parameter. using (AsyncExceptionManager exceptionManager = new AsyncExceptionManager()) { MockHttpServer mockServer = new MockHttpServer( exceptionManager, MockHttpServer.DefaultServerPrefixUri); server = mockServer.CreateListener( (context) => { contextHandler(context); context.Response.StatusCode = (int)status; content.Position = 0; content.CopyTo(context.Response.OutputStream); context.Response.Close(); }, 1); } WebClient client = new WebClient(); try { client.OpenRead(new Uri(DefaultServerPrefixUri, "exception.htm")); } catch (WebException ex) { return ex; } } finally { server.Stop(); } return null; }
public void InitializeTest() { TestStartTime = DateTime.Now; // This test uses the https endpoint, setup the certificates. MockHttpServer.SetupCertificates(); PowerShell = PowerShell.Create(); Subscription = UnitTestHelper.SetupUnitTestSubscription(PowerShell); // Set names for the servers we'll use in PowerShell. PowerShell.Runspace.SessionStateProxy.SetVariable( "homeServerName", HomeServer); PowerShell.Runspace.SessionStateProxy.SetVariable( "partnerServerName", PartnerServer); // Create a new server HttpSession testSession = MockServerHelper.DefaultSessionCollection.GetSession( string.Format("UnitTest.{0}.{1}", TestContext.FullyQualifiedTestClassName, TestContext.TestName)); ServerTestHelper.SetDefaultTestSessionSettings(testSession); // When testing production use RDFE // testSession.ServiceBaseUri = new Uri("https://management.core.windows.net"); // When testing OneBox use Mock RDFE: if (IsRunningAgainstOneBox) { testSession.ServiceBaseUri = new Uri("https://management.dev.mscds.com:12346/"); } testSession.RequestValidator = new Action<HttpMessage, HttpMessage.Request>( (expected, actual) => { Assert.AreEqual(expected.RequestInfo.Method, actual.Method); Assert.IsTrue( actual.UserAgent.Contains(ApiConstants.UserAgentHeaderValue), "Missing proper UserAgent string."); Assert.IsTrue( UnitTestHelper.GetUnitTestClientCertificate().Equals(actual.Certificate), "Expected correct client certificate"); }); ExceptionManager = new AsyncExceptionManager(); MockHttpServer = new MockHttpServer(ExceptionManager, MockHttpServer.DefaultHttpsServerPrefixUri, testSession); }