public void TestRefreshAccessToken() { TestUtils.ValidateRequiredParameters(provider, new string[] { "RefreshToken" }, delegate() { provider.RefreshAccessToken(); }); provider.IsOffline = false; Assert.Throws <ArgumentException>(delegate() { provider.RefreshAccessToken(); }); provider.IsOffline = true; oauth2RequestInterceptor.RequestType = OAuth2RequestInterceptor.OAuth2RequestType.RefreshAccessToken; WebRequestInterceptor.OnBeforeSendResponse callback = delegate(Uri uri, WebHeaderCollection headers, String body) { Assert.AreEqual(REFRESH_ACCESS_TOKEN_REQUEST, body); }; try { provider.RefreshToken = TEST_REFRESH_TOKEN; oauth2RequestInterceptor.BeforeSendResponse += callback; provider.RefreshAccessToken(); Assert.AreEqual(provider.RefreshToken, TEST_REFRESH_TOKEN); Assert.AreEqual(provider.TokenType, OAuth2RequestInterceptor.ACCESS_TOKEN_TYPE); Assert.AreEqual(provider.ExpiresIn.ToString(), OAuth2RequestInterceptor.EXPIRES_IN); } finally { oauth2RequestInterceptor.BeforeSendResponse -= callback; } }
public void TestGenerateAccessTokenForServiceAccountsWithSecretsFile() { MockAppConfig config = new Mocks.MockAppConfig(); config.MockReadSettings(tblSettingsWithSecretJson); provider = new OAuth2ProviderForServiceAccounts(config); TestUtils.ValidateRequiredParameters(provider, new string[] { "ServiceAccountEmail", "Scope", "JwtPrivateKey" }, delegate() { provider.GenerateAccessTokenForServiceAccount(); } ); oauth2RequestInterceptor.RequestType = OAuth2RequestInterceptor.OAuth2RequestType.FetchAccessTokenForServiceAccount; WebRequestInterceptor.OnBeforeSendResponse callback = delegate(Uri uri, WebHeaderCollection headers, String body) { Assert.AreEqual(SERVICE_ACCOUNT_REQUEST_WITH_SECRETS_FILE, body); }; try { oauth2RequestInterceptor.BeforeSendResponse += callback; provider.GenerateAccessTokenForServiceAccount(); Assert.AreEqual(provider.AccessToken, OAuth2RequestInterceptor.TEST_ACCESS_TOKEN); Assert.AreEqual(provider.TokenType, OAuth2RequestInterceptor.ACCESS_TOKEN_TYPE); Assert.AreEqual(provider.ExpiresIn.ToString(), OAuth2RequestInterceptor.EXPIRES_IN); } finally { oauth2RequestInterceptor.BeforeSendResponse -= callback; } }
public void TestFetchAccessAndRefreshTokens() { TestUtils.ValidateRequiredParameters(provider, new string[] { "ClientId", "ClientSecret" }, delegate() { provider.RefreshAccessToken(); } ); oauth2RequestInterceptor.RequestType = OAuth2RequestInterceptor.OAuth2RequestType.FetchAccessAndRefreshToken; WebRequestInterceptor.OnBeforeSendResponse callback = delegate(Uri uri, WebHeaderCollection headers, String body) { Assert.AreEqual(FETCH_ACCESS_TOKEN_REQUEST, body); }; try { oauth2RequestInterceptor.BeforeSendResponse += callback; provider.FetchAccessAndRefreshTokens(AUTHORIZATION_URL); Assert.AreEqual(provider.AccessToken, OAuth2RequestInterceptor.TEST_ACCESS_TOKEN); Assert.AreEqual(provider.RefreshToken, OAuth2RequestInterceptor.TEST_REFRESH_TOKEN); Assert.AreEqual(provider.TokenType, OAuth2RequestInterceptor.ACCESS_TOKEN_TYPE); Assert.AreEqual(provider.ExpiresIn.ToString(), OAuth2RequestInterceptor.EXPIRES_IN); } finally { oauth2RequestInterceptor.BeforeSendResponse -= callback; } }
/// <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; }
public void TestRevokeRefreshToken() { TestUtils.ValidateRequiredParameters(provider, new string[] { "RefreshToken" }, delegate() { provider.RevokeRefreshToken(); }); oauth2RequestInterceptor.RequestType = OAuth2RequestInterceptor.OAuth2RequestType.RefreshAccessToken; WebRequestInterceptor.OnBeforeSendResponse callback = delegate(Uri uri, WebHeaderCollection headers, String body) { Assert.AreEqual(uri.AbsoluteUri, REVOKE_REFRESH_TOKEN_URL); }; try { provider.RefreshToken = OAuth2RequestInterceptor.TEST_REFRESH_TOKEN; oauth2RequestInterceptor.BeforeSendResponse += callback; provider.RevokeRefreshToken(); } finally { oauth2RequestInterceptor.BeforeSendResponse -= callback; } }
/// <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; } }
public void TestGenerateAccessTokenForServiceAccounts() { TestUtils.ValidateRequiredParameters(provider, new string[] { "ServiceAccountEmail", "Scope", "PrnEmail", "JwtCertificatePath", "JwtCertificatePassword" }, delegate() { provider.GenerateAccessTokenForServiceAccount(); } ); oauth2RequestInterceptor.RequestType = OAuth2RequestInterceptor.OAuth2RequestType.FetchAccessTokenForServiceAccount; WebRequestInterceptor.OnBeforeSendResponse callback = delegate(Uri uri, WebHeaderCollection headers, String body) { Assert.AreEqual(SERVICE_ACCOUNT_REQUEST, body); }; try { oauth2RequestInterceptor.BeforeSendResponse += callback; provider.GenerateAccessTokenForServiceAccount(); Assert.AreEqual(provider.AccessToken, OAuth2RequestInterceptor.TEST_ACCESS_TOKEN); Assert.AreEqual(provider.TokenType, OAuth2RequestInterceptor.ACCESS_TOKEN_TYPE); Assert.AreEqual(provider.ExpiresIn.ToString(), OAuth2RequestInterceptor.EXPIRES_IN); } finally { oauth2RequestInterceptor.BeforeSendResponse -= callback; } }
/// <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; }