/// <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;
   }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="MockWebRequest"/> class.
 /// </summary>
 /// <param name="webResponse">The web response.</param>
 /// <param name="requestUri">The request URI.</param>
 /// <param name="onBeforeSendResponse">Callback to be called before sending
 /// response.</param>
 public MockWebRequest(MockWebResponse webResponse, Uri requestUri,
     WebRequestInterceptor.OnBeforeSendResponse onBeforeSendResponse, bool raiseException) {
   this.webResponse = webResponse;
   this.requestUri = requestUri;
   this.onBeforeSendResponse = onBeforeSendResponse;
   this.raiseException = raiseException;
 }
 /// <summary>
 /// Initializes the <see cref="AdWordsRequestInterceptor"/> class.
 /// </summary>
 static ClientLoginRequestInterceptor()
 {
     instance = new ClientLoginRequestInterceptor();
     WebRequest.RegisterPrefix(AuthToken.Url.AbsoluteUri, instance);
 }
 /// <summary>
 /// Initializes the <see cref="AdWordsRequestInterceptor"/> class.
 /// </summary>
 static ClientLoginRequestInterceptor() {
   instance = new ClientLoginRequestInterceptor();
   WebRequest.RegisterPrefix(AuthToken.Url.AbsoluteUri, instance);
 }
 /// <summary>
 /// Initializes the <see cref="OAuth2RequestInterceptor"/> class.
 /// </summary>
 static OAuth2RequestInterceptor()
 {
     instance = new OAuth2RequestInterceptor();
     WebRequest.RegisterPrefix(OAUTH2_URL_PREFIX, instance);
 }
 /// <summary>
 /// Initializes the <see cref="OAuth2RequestInterceptor"/> class.
 /// </summary>
 static OAuth2RequestInterceptor() {
   instance = new OAuth2RequestInterceptor();
   WebRequest.RegisterPrefix(OAUTH2_URL_PREFIX, instance);
 }
 /// <summary>
 /// Initializes the <see cref="AdWordsRequestInterceptor"/> class.
 /// </summary>
 static AdWordsRequestInterceptor() {
   AdWordsAppConfig config = new AdWordsAppConfig();
   instance = new AdWordsRequestInterceptor();
   WebRequest.RegisterPrefix(config.AdWordsApiServer, instance);
 }