public void TheLocationIsReturnedAfterASuccesfulPut() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", "", HttpStatusCode.OK, "http://localhost/ABC"); var request = factory.Create(new Uri("http://localhost")); var stream = new MemoryStream(); request.EndGetRequestStream(Arg.Any <IAsyncResult>()).Returns(c => stream); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), null); string savedLocation = null; // act handler.Put(request, new UserContract() { Name = "Paul" }, (code, location) => { savedLocation = location; }, (uri, exception) => Assert.Fail(exception.Message)); // assert Assert.AreEqual("http://localhost/ABC", savedLocation); }
public void SetUp() { _configuration = Substitute.For<IApiConfiguration>(); _webFactory = new TestWebRequestFactory(); _service = new CustomerPaymentRecordWithDiscountsAndFeesService(_configuration, _webFactory); _configuration.ApiBaseUrl.Returns(ApiRequestHandler.ApiRequestUri.AbsoluteUri); }
public void OperationCorrectlyHandlesWebExceptionErrors([ValueSource("_operations")] Tuple <Action <TestServiceBase, Uri>, string, bool> operation) { if (operation.Item3 == false) { Assert.Inconclusive("Not applicable"); } ; // arrange var apiUri = ApiRequestHandler.ApiRequestUri; var keyService = new SimpleOAuthKeyService() { OAuthResponse = new OAuthTokens() { AccessToken = "<<accesstoken>>", RefreshToken = "<<refreshtoken>>", ExpiresIn = 10 } }; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(OAuthRequestHandler.OAuthRequestUri.AbsoluteUri, new OAuthTokens() { AccessToken = "<<newaccesstoken>>" }.ToJson()); factory.RegisterExceptionForUri <WebException>(apiUri.AbsoluteUri); var service = new TestServiceBase(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), factory, keyService); // act var ex = Assert.Throws <ApiCommunicationException>(() => operation.Item1(service, apiUri)); // assert Assert.AreEqual(apiUri, ex.URI); }
public void TheEntityIsPlacedOnTheOutgoingStreamInJsonFormatDuringPost() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var stream = new MemoryStream(); request.EndGetRequestStream(Arg.Any <IAsyncResult>()).Returns(c => stream); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), null); // act handler.Post(request, new UserContract() { Name = "Paul" }, (code, location) => { }, (uri, exception) => Assert.Fail(exception.Message)); // assert var reader = new StreamReader(new MemoryStream(stream.ToArray())); var data = reader.ReadToEnd().FromJson <UserContract>(); Assert.AreEqual("Paul", data.Name); }
async public Task TheEntityIsPlacedOnTheOutgoingStreamInJsonFormatDuringPostAsync() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var stream = new MemoryStream(); #pragma warning disable 1998 request.GetRequestStreamAsync().Returns(async c => (Stream)stream); #pragma warning restore 1998 var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), null); // act await handler.PostAsync(request, new UserContract() { Name = "Paul" }); // assert var reader = new StreamReader(new MemoryStream(stream.ToArray())); var data = reader.ReadToEnd().FromJson <UserContract>(); Assert.AreEqual("Paul", data.Name); }
public void OperationUpdatesKeyServiceWithNewTokensWhenCurrentTokensHaveExpired([ValueSource("_operations")] Tuple <Action <TestServiceBase, Uri>, string, bool> operation) { // arrange var apiUri = ApiRequestHandler.ApiRequestUri; var keyService = new SimpleOAuthKeyService() { OAuthResponse = new OAuthTokens() { AccessToken = "<<accesstoken>>", RefreshToken = "<<refreshtoken>>", ExpiresIn = 10 } }; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(OAuthRequestHandler.OAuthRequestUri.AbsoluteUri, new OAuthTokens() { AccessToken = "<<newaccesstoken>>" }.ToJson()); factory.RegisterResultForUri(apiUri.AbsoluteUri, operation.Item2); var service = new TestServiceBase(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), factory, keyService); // act operation.Item1(service, apiUri); // assert Assert.AreEqual("<<newaccesstoken>>", keyService.OAuthResponse.AccessToken); }
public void SetUp() { _configuration = Substitute.For<IApiConfiguration>(); _webFactory = new TestWebRequestFactory(); _service = new CalculateDiscountsFeesService(_configuration, _webFactory); _configuration.ApiBaseUrl.Returns(ApiRequestHandler.ApiRequestUri.AbsoluteUri); }
public void DuringGetRequestExpectedHeadersAreAttached(bool supplyCredentials, bool supplyOAuth) { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", new UserContract() { Name = "David" }.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), supplyCredentials ? new CompanyFileCredentials("user", "pass") : null, supplyOAuth ? new OAuthTokens() : null); // act handler.Get <UserContract>(request, (code, response) => { }, (uri, exception) => Assert.Fail(exception.Message), null); // assert AssertStandardHeaders(supplyCredentials, supplyOAuth, request); var version = typeof(ApiRequestHandler).Assembly.GetName().Version.ToString(3); var userAgent = request.Headers[HttpRequestHeader.UserAgent]; Assert.IsTrue(userAgent.Contains(string.Format("MYOB-ARL-SDK/{0}", version))); Assert.IsNull(request.Headers[HttpRequestHeader.IfNoneMatch]); }
public void SetUp() { _configuration = Substitute.For <IApiConfiguration>(); _webFactory = new TestWebRequestFactory(); _service = new CalculateDiscountsFeesService(_configuration, _webFactory); _configuration.ApiBaseUrl.Returns(ApiRequestHandler.ApiRequestUri.AbsoluteUri); }
public void SetUp() { _configuration = Substitute.For <IApiConfiguration>(); _webFactory = new TestWebRequestFactory(); _service = new CustomerPaymentRecordWithDiscountsAndFeesService(_configuration, _webFactory); _configuration.ApiBaseUrl.Returns(ApiRequestHandler.ApiRequestUri.AbsoluteUri); }
async public void DuringPostRequest_Async_ExpectedHeadersAreAttached() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act await handler.PostAsync(request, new UserContract() { Name = "Paul" }); // assert Assert.AreEqual("POST", request.Method); Assert.IsTrue(request.Headers[HttpRequestHeader.Authorization].StartsWith("Bearer")); Assert.IsTrue(request.Headers[HttpRequestHeader.AcceptEncoding].Split(new[] { ',' }).Contains("gzip")); Assert.AreEqual("<<clientid>>", request.Headers["x-myobapi-key"]); Assert.AreEqual("v2", request.Headers["x-myobapi-version"]); Assert.AreEqual(Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass")), request.Headers["x-myobapi-cftoken"]); }
public void SetUp() { _configuration = Substitute.For <IApiConfiguration>(); _webFactory = new TestWebRequestFactory(); _service = new TestPhotoService(_configuration, _webFactory, null); _configuration.ApiBaseUrl.Returns(ApiRequestHandler.ApiRequestUri.AbsoluteUri); _uid = Guid.NewGuid(); }
public void SetUp() { _configuration = Substitute.For<IApiConfiguration>(); _webFactory = new TestWebRequestFactory(); _service = new ContactService(_configuration, _webFactory, null); _configuration.ApiBaseUrl.Returns(ApiRequestHandler.ApiRequestUri.AbsoluteUri); _uid = Guid.NewGuid(); }
public void SetUp() { _configuration = Substitute.For <IApiConfiguration>(); _webFactory = new TestWebRequestFactory(); _service = new CompanyPreferencesService(_configuration, _webFactory); _configuration.ApiBaseUrl.Returns(ApiRequestHandler.ApiRequestUri.AbsoluteUri); _cf = new CompanyFile { Uri = new Uri("https://dc1.api.myob.com/accountright/7D5F5516-AF68-4C5B-844A-3F054E00DF10") }; _getRangeUri = string.Format("{0}/{1}/", _cf.Uri.AbsoluteUri, _service.Route); }
public void SetUp() { _configuration = Substitute.For<IApiConfiguration>(); _webFactory = new TestWebRequestFactory(); _service = new AccountBudgetService(_configuration, _webFactory); _configuration.ApiBaseUrl.Returns(ApiRequestHandler.ApiRequestUri.AbsoluteUri); _financialYear = 2014; _cf = new CompanyFile { Uri = new Uri("https://dc1.api.myob.com/accountright/7D5F5516-AF68-4C5B-844A-3F054E00DF10") }; _getRangeUri = string.Format("{0}/{1}/?", _cf.Uri.AbsoluteUri, _service.Route); }
public void SetUp() { var uri = OAuthRequestHandler.OAuthRequestUri; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(uri.AbsoluteUri, "null"); _request = factory.Create(uri); _stream = new MemoryStream(); _request.EndGetRequestStream(Arg.Any<IAsyncResult>()).Returns(c => _stream); _request.GetRequestStreamAsync().Returns(async c => (Stream)_stream); _handler = new OAuthRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "http://desktop/")); }
public void WhenIGetTokensAsyncIReceiveDeserializedTokens() { var uriOauth = OAuthRequestHandler.OAuthRequestUri; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(uriOauth.AbsoluteUri, "{\"access_token\": \"<<token>>\"}"); var service = new OAuthService(_configuration, factory); OAuthTokens received = service.GetTokensAsync("code").Result; Assert.IsNotNull(received); Assert.AreEqual("<<token>>", received.AccessToken); }
public void SetUp() { var uri = OAuthRequestHandler.OAuthRequestUri; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(uri.AbsoluteUri, "null"); _request = factory.Create(uri); _stream = new MemoryStream(); _request.EndGetRequestStream(Arg.Any <IAsyncResult>()).Returns(c => _stream); _request.GetRequestStreamAsync().Returns(async c => (Stream)_stream); _handler = new OAuthRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "http://desktop/")); }
public void WhenIRenewTokensSyncIReceiveDeserializedTokens() { var uriOauth = OAuthRequestHandler.OAuthRequestUri; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(uriOauth.AbsoluteUri, "{\"access_token\": \"<<token>>\"}"); var service = new OAuthService(_configuration, factory); OAuthTokens received = service.RenewTokens(new OAuthTokens()); Assert.IsNotNull(received); Assert.AreEqual("<<token>>", received.AccessToken); }
public void TranslatesWebExceptionsWhenThrown() { // arrange var uri = "http://localhost/"; var factory = new TestWebRequestFactory(); factory.RegisterExceptionForUri <WebException>(uri); var handler = new TestBaseRequestHandler(factory); // act Assert.ThrowsAsync <ApiCommunicationException>(async() => await handler.MakeRequestAsync(uri)); // assert }
public void SetUp() { _configuration = Substitute.For<IApiConfiguration>(); _webFactory = new TestWebRequestFactory(); _service = new TimesheetService(_configuration, _webFactory); _configuration.ApiBaseUrl.Returns(ApiRequestHandler.ApiRequestUri.AbsoluteUri); _newGuid = Guid.NewGuid(); _cf = new CompanyFile { Uri = new Uri("https://dc1.api.myob.com/accountright/7D5F5516-AF68-4C5B-844A-3F054E00DF10") }; _getRangeUri = string.Format("{0}/{1}/?StartDate={2}&EndDate={3}", _cf.Uri.AbsoluteUri, _service.Route, ((DateTime?)new DateTime(2014, 10, 22)).Value.Date.ToString("s"), ((DateTime?)new DateTime(2014, 10, 29)).Value.Date.ToString("s")); }
public void SetUp() { _configuration = Substitute.For <IApiConfiguration>(); _webFactory = new TestWebRequestFactory(); _service = new TimesheetService(_configuration, _webFactory); _configuration.ApiBaseUrl.Returns(ApiRequestHandler.ApiRequestUri.AbsoluteUri); _newGuid = Guid.NewGuid(); _cf = new CompanyFile { Uri = new Uri("https://dc1.api.myob.com/accountright/7D5F5516-AF68-4C5B-844A-3F054E00DF10") }; _getRangeUri = string.Format("{0}/{1}/?StartDate={2}&EndDate={3}", _cf.Uri.AbsoluteUri, _service.Route, ((DateTime?)new DateTime(2014, 10, 22)).Value.Date.ToString("s"), ((DateTime?)new DateTime(2014, 10, 29)).Value.Date.ToString("s")); }
public void WhenIRenewTokensDelegateIReceiveDeserializedTokens() { var uriOauth = OAuthRequestHandler.OAuthRequestUri; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(uriOauth.AbsoluteUri, "{\"access_token\": \"<<token>>\"}"); var service = new OAuthService(_configuration, factory); OAuthTokens received = null; service.RenewTokens(new OAuthTokens(), (code, tokens) => { received = tokens; }, (uri, exception) => { Assert.Fail(); }); Assert.IsNotNull(received); Assert.AreEqual("<<token>>", received.AccessToken); }
public void CallOnErrorIfError() { // arrange var uri = "http://localhost/"; var factory = new TestWebRequestFactory(); factory.RegisterExceptionForUri<WebException>(uri); var handler = new TestBaseRequestHandler(factory); // act var data = new object(); handler.MakeRequest(uri, null, (exuri, exception) => data = exception); // assert Assert.IsNotNull(data); }
public void CallOnSuccessIfNoError() { // arrange var uri = "http://localhost/"; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(uri, "null"); var handler = new TestBaseRequestHandler(factory); // act var data = new object(); handler.MakeRequest(uri, (code, location, response) => data = response, null); // assert Assert.IsNull(data); }
public void WhenIRenewTokensDelegateIReceiveDeserializedTokens() { var uriOauth = OAuthRequestHandler.OAuthRequestUri; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(uriOauth.AbsoluteUri, "{\"access_token\": \"<<token>>\"}"); var service = new OAuthService(_configuration, factory); OAuthTokens received = null; service.RenewTokens(new OAuthTokens(), (code, tokens) => { received = tokens; }, (uri, exception) => { Assert.Fail(); }); Assert.IsNotNull(received); Assert.AreEqual("<<token>>", received.AccessToken); }
public void DuringGetRequest_IfNoneMatchHeaderAttached_IfSupplyETag() { // arrange var eTag = "123456789"; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", new UserContract() { Name = "David" }.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act handler.Get<UserContract>(request, (code, response) => { }, (uri, exception) => Assert.Fail(exception.Message), eTag); // assert Assert.AreEqual(eTag, request.Headers[HttpRequestHeader.IfNoneMatch]); }
async public Task CanExtractCompressedJsonEntityAsync() { // arrange var uri = "http://localhost/"; var factory = new TestWebRequestFactory(); factory.RegisterCompressedResultForUri(uri, "{ \"Name\": \"Paul\" }"); var handler = new TestBaseRequestHandler(factory); // act var res = await handler.MakeRequestAsync(uri); // assert Assert.IsNotNull(res.Item2); Assert.AreEqual("Paul", res.Item2.Name); }
public void GetNonWebExceptionsAreWrappedIfThrown() { // arrange var apiUri = ApiRequestHandler.ApiRequestUri; var factory = new TestWebRequestFactory(); var service = new TestServiceBase(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), factory, null); factory.RegisterExceptionForUri <InvalidOperationException>(apiUri.AbsoluteUri); // act var ex = Assert.Throws <ApiOperationException>(() => service.Get(apiUri)); // assert Assert.IsInstanceOf <InvalidOperationException>(ex.InnerException); }
public void CanExtractJsonEntity() { // arrange var uri = "http://localhost/"; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(uri, "{ \"Name\": \"Paul\" }"); var handler = new TestBaseRequestHandler(factory); // act UserContract data = null; handler.MakeRequest(uri, (code, location, response) => data = response, null); // assert Assert.IsNotNull(data); Assert.AreEqual("Paul", data.Name); }
public void CallOnSuccessIfNoError() { // arrange var uri = "http://localhost/"; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(uri, "null"); var handler = new TestBaseRequestHandler(factory); // act var data = new object(); handler.MakeRequest(uri, (code, location, response) => data = response, null); // assert Assert.IsNull(data); }
public void CallOnErrorIfError() { // arrange var uri = "http://localhost/"; var factory = new TestWebRequestFactory(); factory.RegisterExceptionForUri <WebException>(uri); var handler = new TestBaseRequestHandler(factory); // act var data = new object(); handler.MakeRequest(uri, null, (exuri, exception) => data = exception); // assert Assert.IsNotNull(data); }
async public void DuringGetRequest_Async_ExpectedHeadersAreAttached(bool supplyCredentials, bool supplyOAuth) { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", new UserContract() { Name = "David" }.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), supplyCredentials ? new CompanyFileCredentials("user", "pass") : null, supplyOAuth ? new OAuthTokens() : null); // act var res = await handler.GetAsync<UserContract>(request, null); // assert AssertStandardHeaders(supplyCredentials, supplyOAuth, request); Assert.IsNull(request.Headers[HttpRequestHeader.IfNoneMatch]); }
public void RenewTokensCorrectlyHandlesWebExceptionErrors() { // arrange var apiUri = ApiRequestHandler.ApiRequestUri; var factory = new TestWebRequestFactory(); factory.RegisterExceptionForUri <WebException>(OAuthRequestHandler.OAuthRequestUri.AbsoluteUri); var service = new OAuthService(_configuration, factory); // act var ex = Assert.Throws <ApiCommunicationException>(() => service.RenewTokens(new OAuthTokens())); // assert Assert.AreEqual(OAuthRequestHandler.OAuthRequestUri, ex.URI); }
async public Task DuringDeleteRequest_Async_ExpectedHeadersAreAttached(bool supplyCredentials, bool supplyOAuth) { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), supplyCredentials ? new CompanyFileCredentials("user", "pass") : null, supplyOAuth ? new OAuthTokens() : null); // act await handler.DeleteAsync(request); // assert Assert.AreEqual("DELETE", request.Method); AssertStandardHeaders(supplyCredentials, supplyOAuth, request); }
public void CanExtractCompressedJsonEntity() { // arrange var uri = "http://localhost/"; var factory = new TestWebRequestFactory(); factory.RegisterCompressedResultForUri(uri, "{ \"Name\": \"Paul\" }"); var handler = new TestBaseRequestHandler(factory); // act UserContract data = null; handler.MakeRequest(uri, (code, location, response) => data = response, null); // assert Assert.IsNotNull(data); Assert.AreEqual("Paul", data.Name); }
public void PutAsyncRequest_Return_Response() { // arrange var expected = new ExpectedResult { UID = Guid.NewGuid() }; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", expected.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act var receivedEntity = handler.PutAsync <Parameter, ExpectedResult>(request, new Parameter()).Result; // assert Assert.AreEqual(expected.UID, receivedEntity.UID); }
async public Task DuringGetRequest_Async_IfNoneMatchHeaderAttached_IfSupplyETag() { // arrange var eTag = "123456789"; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", new UserContract() { Name = "David" }.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act var res = await handler.GetAsync <UserContract>(request, eTag); // assert Assert.AreEqual(eTag, request.Headers[HttpRequestHeader.IfNoneMatch]); }
async public void DuringGetRequest_Async_ExpectedHeadersAreAttached() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", new UserContract() { Name = "David" }.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act var res = await handler.GetAsync<UserContract>(request); // assert Assert.IsTrue(request.Headers[HttpRequestHeader.Authorization].StartsWith("Bearer")); Assert.IsTrue(request.Headers[HttpRequestHeader.AcceptEncoding].Split(new[] { ',' }).Contains("gzip")); Assert.AreEqual("<<clientid>>", request.Headers["x-myobapi-key"]); Assert.AreEqual("v2", request.Headers["x-myobapi-version"]); Assert.AreEqual(Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass")), request.Headers["x-myobapi-cftoken"]); }
async public Task DuringGetRequest_Async_ExpectedHeadersAreAttached(bool supplyCredentials, bool supplyOAuth) { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", new UserContract() { Name = "David" }.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), supplyCredentials ? new CompanyFileCredentials("user", "pass") : null, supplyOAuth ? new OAuthTokens() : null); // act var res = await handler.GetAsync <UserContract>(request, null); // assert AssertStandardHeaders(supplyCredentials, supplyOAuth, request); Assert.IsNull(request.Headers[HttpRequestHeader.IfNoneMatch]); }
public void AuthorizationHeaderNotAppliedWhenClientCertificateAttached() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", new UserContract() { Name = "David" }.ToJson()); var request = factory.Create(new Uri("http://localhost")); (request as HttpWebRequest).ClientCertificates.Add(new X509Certificate()); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act handler.Get <UserContract>(request, (code, response) => { }, (uri, exception) => Assert.Fail(exception.Message), null); // assert Assert.IsNull(request.Headers[HttpRequestHeader.Authorization]); }
public void DuringDeleteRequestExpectedHeadersAreAttached() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act handler.Delete(request, (code) => { }, (uri, exception) => Assert.Fail(exception.Message)); // assert Assert.AreEqual("DELETE", request.Method); Assert.IsTrue(request.Headers[HttpRequestHeader.Authorization].StartsWith("Bearer")); Assert.IsTrue(request.Headers[HttpRequestHeader.AcceptEncoding].Split(new[] { ',' }).Contains("gzip")); Assert.AreEqual("<<clientid>>", request.Headers["x-myobapi-key"]); Assert.AreEqual("v2", request.Headers["x-myobapi-version"]); Assert.AreEqual(Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass")), request.Headers["x-myobapi-cftoken"]); }
public void PostRequest_Return_Response() { // arrange var expected = new ExpectedResult { UID = Guid.NewGuid() }; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", expected.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act ExpectedResult receivedEntity = null; handler.Post <Parameter, ExpectedResult>(request, new Parameter(), (code, location, response) => { receivedEntity = response; }, (uri, exception) => Assert.Fail(exception.Message)); // assert Assert.AreEqual(expected.UID, receivedEntity.UID); }
public void DuringGetRequestExpectedHeadersAreAttached(bool supplyCredentials, bool supplyOAuth) { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", new UserContract(){Name = "David"}.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), supplyCredentials ? new CompanyFileCredentials("user", "pass") : null, supplyOAuth ? new OAuthTokens() : null); // act handler.Get<UserContract>(request, (code, response) => { }, (uri, exception) => Assert.Fail(exception.Message), null); // assert AssertStandardHeaders(supplyCredentials, supplyOAuth, request); var version = typeof (ApiRequestHandler).Assembly.GetName().Version.ToString(3); var userAgent = request.Headers[HttpRequestHeader.UserAgent]; Assert.IsTrue(userAgent.Contains(string.Format("MYOB-ARL-SDK/{0}", version))); Assert.IsNull(request.Headers[HttpRequestHeader.IfNoneMatch]); }
public void DuringPostRequestExpectedHeadersAreAttached(bool supplyCredentials, bool supplyOAuth) { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), supplyCredentials ? new CompanyFileCredentials("user", "pass") : null, supplyOAuth ? new OAuthTokens() : null); // act handler.Post(request, new UserContract() { Name = "Paul" }, (code, location) => { }, (uri, exception) => Assert.Fail(exception.Message)); // assert Assert.AreEqual("POST", request.Method); AssertStandardHeaders(supplyCredentials, supplyOAuth, request); }
public void DuringDeleteRequestExpectedHeadersAreAttached() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act handler.Delete(request, (code) => { }, (uri, exception) => Assert.Fail(exception.Message)); // assert Assert.AreEqual("DELETE", request.Method); Assert.IsTrue(request.Headers[HttpRequestHeader.Authorization].StartsWith("Bearer")); Assert.IsTrue(request.Headers[HttpRequestHeader.AcceptEncoding].Split(new[] { ',' }).Contains("gzip")); Assert.AreEqual("<<clientid>>", request.Headers["x-myobapi-key"]); Assert.AreEqual("v2", request.Headers["x-myobapi-version"]); Assert.AreEqual(Convert.ToBase64String(Encoding.UTF8.GetBytes("user:pass")), request.Headers["x-myobapi-cftoken"]); }
async public void TheLocationIsReturnedAfterASuccesfulPutAsync() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", "", HttpStatusCode.OK, "http://localhost/ABC"); var request = factory.Create(new Uri("http://localhost")); var stream = new MemoryStream(); request.EndGetRequestStream(Arg.Any<IAsyncResult>()).Returns(c => stream); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), null); // act var res = await handler.PutAsync(request, new UserContract() { Name = "Paul" }); // assert Assert.AreEqual("http://localhost/ABC", res); }
public void RenewTokensCorrectlyHandlesWebExceptionErrors() { // arrange var apiUri = ApiRequestHandler.ApiRequestUri; var factory = new TestWebRequestFactory(); factory.RegisterExceptionForUri<WebException>(OAuthRequestHandler.OAuthRequestUri.AbsoluteUri); var service = new OAuthService(_configuration, factory); // act var ex = Assert.Throws<ApiCommunicationException>(() => service.RenewTokens(new OAuthTokens())); // assert Assert.AreEqual(OAuthRequestHandler.OAuthRequestUri, ex.URI); }
public void TheLocationIsReturnedAfterASuccesfulPut() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", "", HttpStatusCode.OK, "http://localhost/ABC"); var request = factory.Create(new Uri("http://localhost")); var stream = new MemoryStream(); request.EndGetRequestStream(Arg.Any<IAsyncResult>()).Returns(c => stream); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), null); string savedLocation = null; // act handler.Put(request, new UserContract() { Name = "Paul" }, (code, location) => { savedLocation = location; }, (uri, exception) => Assert.Fail(exception.Message)); // assert Assert.AreEqual("http://localhost/ABC", savedLocation); }
async public void TheEntityIsPlacedOnTheOutgoingStreamInJsonFormatDuringPostAsync() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var stream = new MemoryStream(); request.GetRequestStreamAsync().Returns(async c => (Stream)stream); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), null); // act await handler.PostAsync(request, new UserContract() { Name = "Paul" }); // assert var reader = new StreamReader(new MemoryStream(stream.ToArray())); var data = reader.ReadToEnd().FromJson<UserContract>(); Assert.AreEqual("Paul", data.Name); }
public void TheEntityIsPlacedOnTheOutgoingStreamInJsonFormatDuringPost() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var stream = new MemoryStream(); request.EndGetRequestStream(Arg.Any<IAsyncResult>()).Returns(c => stream); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), null); // act handler.Post(request, new UserContract() { Name = "Paul" }, (code, location) => { }, (uri, exception) => Assert.Fail(exception.Message)); // assert var reader = new StreamReader(new MemoryStream(stream.ToArray())); var data = reader.ReadToEnd().FromJson<UserContract>(); Assert.AreEqual("Paul", data.Name); }
public void PutAsyncRequest_Return_Response() { // arrange var expected = new ExpectedResult { UID = Guid.NewGuid() }; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", expected.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act var receivedEntity = handler.PutAsync<Parameter, ExpectedResult>(request, new Parameter()).Result; // assert Assert.AreEqual(expected.UID, receivedEntity.UID); }
public void PostRequest_Return_Response() { // arrange var expected = new ExpectedResult { UID = Guid.NewGuid() }; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", expected.ToJson()); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act ExpectedResult receivedEntity = null; handler.Post<Parameter, ExpectedResult>(request, new Parameter(), (code, location, response) => { receivedEntity = response; }, (uri, exception) => Assert.Fail(exception.Message)); // assert Assert.AreEqual(expected.UID, receivedEntity.UID); }
public void DuringDeleteRequestExpectedHeadersAreAttached(bool supplyCredentials, bool supplyOAuth) { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), supplyCredentials ? new CompanyFileCredentials("user", "pass"): null, supplyOAuth ? new OAuthTokens() : null); // act handler.Delete(request, (code) => { }, (uri, exception) => Assert.Fail(exception.Message)); // assert Assert.AreEqual("DELETE", request.Method); AssertStandardHeaders(supplyCredentials, supplyOAuth, request); }
async public void CanExtractCompressedJsonEntityAsync() { // arrange var uri = "http://localhost/"; var factory = new TestWebRequestFactory(); factory.RegisterCompressedResultForUri(uri, "{ \"Name\": \"Paul\" }"); var handler = new TestBaseRequestHandler(factory); // act var res = await handler.MakeRequestAsync(uri); // assert Assert.IsNotNull(res.Item2); Assert.AreEqual("Paul", res.Item2.Name); }
public void OperationUpdatesKeyServiceWithNewTokensWhenCurrentTokensHaveExpired([ValueSource("_operations")] Tuple<Action<TestServiceBase, Uri>, string, bool> operation) { // arrange var apiUri = ApiRequestHandler.ApiRequestUri; var keyService = new SimpleOAuthKeyService() { OAuthResponse = new OAuthTokens() { AccessToken = "<<accesstoken>>", RefreshToken = "<<refreshtoken>>", ExpiresIn = 10 } }; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(OAuthRequestHandler.OAuthRequestUri.AbsoluteUri, new OAuthTokens() { AccessToken = "<<newaccesstoken>>" }.ToJson()); factory.RegisterResultForUri(apiUri.AbsoluteUri, operation.Item2); var service = new TestServiceBase(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), factory, keyService); // act operation.Item1(service, apiUri); // assert Assert.AreEqual("<<newaccesstoken>>", keyService.OAuthResponse.AccessToken); }
public void OperationCorrectlyHandlesWebExceptionErrors([ValueSource("_operations")] Tuple<Action<TestServiceBase, Uri>, string, bool> operation) { if (operation.Item3 == false) { Assert.Inconclusive("Not applicable"); }; // arrange var apiUri = ApiRequestHandler.ApiRequestUri; var keyService = new SimpleOAuthKeyService() { OAuthResponse = new OAuthTokens() { AccessToken = "<<accesstoken>>", RefreshToken = "<<refreshtoken>>", ExpiresIn = 10 } }; var factory = new TestWebRequestFactory(); factory.RegisterResultForUri(OAuthRequestHandler.OAuthRequestUri.AbsoluteUri, new OAuthTokens() { AccessToken = "<<newaccesstoken>>" }.ToJson()); factory.RegisterExceptionForUri<WebException>(apiUri.AbsoluteUri); var service = new TestServiceBase(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), factory, keyService); // act var ex = Assert.Throws<ApiCommunicationException>(() => operation.Item1(service, apiUri)); // assert Assert.AreEqual(apiUri, ex.URI); }
public void AuthorizationHeaderNotAppliedWhenClientCertificateAttached() { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", new UserContract() { Name = "David" }.ToJson()); var request = factory.Create(new Uri("http://localhost")); (request as HttpWebRequest).ClientCertificates.Add(new X509Certificate()); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), new CompanyFileCredentials("user", "pass")); // act handler.Get<UserContract>(request, (code, response) => { }, (uri, exception) => Assert.Fail(exception.Message), null); // assert Assert.IsNull(request.Headers[HttpRequestHeader.Authorization]); }
async public void DuringPostRequest_Async_ExpectedHeadersAreAttached(bool supplyCredentials, bool supplyOAuth) { // arrange var factory = new TestWebRequestFactory(); factory.RegisterResultForUri("http://localhost", ""); var request = factory.Create(new Uri("http://localhost")); var handler = new ApiRequestHandler(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), supplyCredentials ? new CompanyFileCredentials("user", "pass") : null, supplyOAuth ? new OAuthTokens() : null); // act await handler.PostAsync(request, new UserContract() { Name = "Paul" }); // assert Assert.AreEqual("POST", request.Method); AssertStandardHeaders(supplyCredentials, supplyOAuth, request); }
public void GetNonWebExceptionsAreWrappedIfThrown() { // arrange var apiUri = ApiRequestHandler.ApiRequestUri; var factory = new TestWebRequestFactory(); var service = new TestServiceBase(new ApiConfiguration("<<clientid>>", "<<clientsecret>>", "<<redirecturl>>"), factory, null); factory.RegisterExceptionForUri<InvalidOperationException>(apiUri.AbsoluteUri); // act var ex = Assert.Throws<ApiOperationException>(() => service.Get(apiUri)); // assert Assert.IsInstanceOf<InvalidOperationException>(ex.InnerException); }