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); }
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"]); }
/// <exclude/> async protected Task <string> MakeApiPostRequestAsync <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.PostAsync(this.WebRequestFactory.Create(uri), entity, cancellationToken); return(res); }
public void PostAsyncRequest_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.PostAsync <Parameter, ExpectedResult>(request, new Parameter()).Result; // assert Assert.AreEqual(expected.UID, receivedEntity.UID); }
async public Task 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); }
async public Task TheLocationIsReturnedAfterASuccesfulPostAsync() { // 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.PostAsync(request, new UserContract() { Name = "Paul" }); // assert Assert.AreEqual("http://localhost/ABC", res); }
async public void TheLocationIsReturnedAfterASuccesfulPostAsync() { // 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.PostAsync(request, new UserContract() { Name = "Paul" }); // assert Assert.AreEqual("http://localhost/ABC", res); }
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 PostAsyncRequest_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.PostAsync<Parameter, ExpectedResult>(request, new Parameter()).Result; // assert Assert.AreEqual(expected.UID, receivedEntity.UID); }
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); }