async public Task TheEntityIsPlacedOnTheOutgoingStreamInJsonFormatDuringPutAsync() { // 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.PutAsync(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); }
async public void DuringPutRequest_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.PutAsync(request, new UserContract() { Name = "Paul" }); // assert Assert.AreEqual("PUT", 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"]); }
/// <exclude/> async protected Task <string> MakeApiPutRequestAsync <T>(Uri uri, T entity, ICompanyFileCredentials credentials, CancellationToken cancellationToken) where T : class { await RenewOAuthTokensAsync(cancellationToken); var api = new ApiRequestHandler(Configuration, credentials, _keyService.Maybe(_ => _.OAuthResponse)); var res = await api.PutAsync(WebRequestFactory.Create(uri), entity, cancellationToken); return(res); }
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 DuringPutRequest_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.PutAsync(request, new UserContract() { Name = "Paul" }); // assert Assert.AreEqual("PUT", request.Method); AssertStandardHeaders(supplyCredentials, supplyOAuth, request); }
async public Task 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); }
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); }
async public void TheEntityIsPlacedOnTheOutgoingStreamInJsonFormatDuringPutAsync() { // 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.PutAsync(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 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 void DuringPutRequest_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.PutAsync(request, new UserContract() { Name = "Paul" }); // assert Assert.AreEqual("PUT", request.Method); AssertStandardHeaders(supplyCredentials, supplyOAuth, request); }
async public void DuringPutRequest_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.PutAsync(request, new UserContract() { Name = "Paul" }); // assert Assert.AreEqual("PUT", 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"]); }