/// <summary> /// Runs a code example in mocked mode. /// </summary> /// <param name="mockData">The mock data for mocking SOAP request and /// responses for API calls.</param> /// <param name="exampleDelegate">The delegate that initializes and runs the /// code example.</param> /// <param name="callback">The callback to be called before mocked responses /// are sent. You could use this callback to verify if the request was /// serialized correctly.</param> /// <remarks>This method is not thread safe, but since NUnit can run tests /// only in a single threaded mode, thread safety is not a requirement. /// </remarks> protected void RunMockedExample(ExamplesMockData mockData, TestDelegate exampleDelegate, WebRequestInterceptor.OnBeforeSendResponse callback) { TextWriter oldWriter = Console.Out; try { clientLoginInterceptor.Intercept = true; clientLoginInterceptor.RaiseException = false; awapiInterceptor.Intercept = true; AuthToken.Cache.Clear(); awapiInterceptor.LoadMessages(mockData.MockMessages, delegate(Uri requestUri, WebHeaderCollection headers, String body) { VerifySoapHeaders(requestUri, body); callback(requestUri, headers, body); } ); StringWriter newWriter = new StringWriter(); Console.SetOut(newWriter); AdWordsAppConfig config = (user.Config as AdWordsAppConfig); exampleDelegate.Invoke(); Assert.AreEqual(newWriter.ToString().Trim(), mockData.ExpectedOutput.Trim()); } finally { Console.SetOut(oldWriter); clientLoginInterceptor.Intercept = false; awapiInterceptor.Intercept = false; } }
/// <summary> /// Runs a code example in mocked mode. /// </summary> /// <param name="mockData">The mock data for mocking SOAP request and /// responses for API calls.</param> /// <param name="exampleDelegate">The delegate that initializes and runs the /// code example.</param> /// <param name="callback">The callback to be called before mocked responses /// are sent. You could use this callback to verify if the request was /// serialized correctly.</param> /// <remarks>This method is not thread safe, but since NUnit can run tests /// only in a single threaded mode, thread safety is not a requirement. /// </remarks> protected void RunMockedExample(ExamplesMockData mockData, TestDelegate exampleDelegate, WebRequestInterceptor.OnBeforeSendResponse callback) { TextWriter oldWriter = Console.Out; try { awapiInterceptor.Intercept = true; awapiInterceptor.LoadMessages(mockData.MockMessages, delegate(Uri requestUri, WebHeaderCollection headers, String body) { VerifyHttpHeaders(headers); VerifySoapHeaders(requestUri, body); callback(requestUri, headers, body); } ); StringWriter newWriter = new StringWriter(); Console.SetOut(newWriter); exampleDelegate.Invoke(); Assert.AreEqual(newWriter.ToString().Trim(), mockData.ExpectedOutput.Trim()); } finally { Console.SetOut(oldWriter); awapiInterceptor.Intercept = false; } }