public async Task IsAuthorized_is_false_after_expiration()
        {
            // arrange
            var options = GetMockOptions();
            var uri     = ApiPathUtility.Build(options.BaseUrl, "/users/getToken");
            var mock    = new Mock <HttpClient>();

            mock.Setup(client => client.SendAsync(
                           It.Is <HttpRequestMessage>(
                               message => message.RequestUri.Equals(uri)),
                           It.IsAny <CancellationToken>()))
            .ReturnsAsync(new HttpResponseMessage
            {
                Content = new JsonContent(JsonConvert.SerializeObject(FastExpiredTokenResponse))
            });
            var sut = new IamportHttpClient(options, mock.Object);

            // act
            await sut.AuthorizeAsync();

            await Task.Delay(15);

            // assert
            Assert.False(sut.IsAuthorized);
        }
        public async Task RequestIamportRequest_calls_Authorize_after_expired()
        {
            // arrange
            var options = GetMockOptions();
            var uri     = ApiPathUtility.Build(options.BaseUrl, "/users/getToken");
            var mock    = new Mock <HttpClient>();

            mock.Setup(client => client.SendAsync(
                           It.Is <HttpRequestMessage>(
                               message => message.RequestUri.Equals(uri)),
                           It.IsAny <CancellationToken>()))
            .ReturnsAsync(new HttpResponseMessage
            {
                Content = new JsonContent(JsonConvert.SerializeObject(FastExpiredTokenResponse))
            });
            var somethingUri = ApiPathUtility.Build(options.BaseUrl, "/something");

            mock.Setup(client => client.SendAsync(
                           It.Is <HttpRequestMessage>(
                               message => message.RequestUri.Equals(somethingUri)),
                           It.IsAny <CancellationToken>()))
            .ReturnsAsync(new HttpResponseMessage
            {
                Content = new JsonContent(JsonConvert.SerializeObject(EmptyObjectResponse))
            });
            var sut     = new IamportHttpClient(options, mock.Object);
            var request = new IamportRequest <object>
            {
                ApiPathAndQueryString = "/something",
                RequireAuthorization  = true
            };

            // act; call twice
            await sut.RequestAsync <object, object>(request);

            await Task.Delay(15);

            await sut.RequestAsync <object, object>(request);

            // assert
            mock.Verify(client => client.SendAsync(
                            It.Is <HttpRequestMessage>(
                                message => message.RequestUri.Equals(uri)),
                            It.IsAny <CancellationToken>()), Times.Exactly(2));
        }
        public async Task Failed_Authorize_doesnot_change_IsAuthorized()
        {
            // arrange
            var options = GetMockOptions();
            var uri     = ApiPathUtility.Build(options.BaseUrl, "/users/getToken");
            var mock    = new Mock <HttpClient>();

            mock.Setup(client => client.SendAsync(
                           It.Is <HttpRequestMessage>(
                               message => message.RequestUri.Equals(uri)),
                           It.IsAny <CancellationToken>()))
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = System.Net.HttpStatusCode.Unauthorized,
                Content    = new JsonContent(JsonConvert.SerializeObject(FailedTokenResponse))
            });
            var sut = new IamportHttpClient(options, mock.Object);

            // act/assert
            await Assert.ThrowsAsync <UnauthorizedAccessException>(
                () => sut.AuthorizeAsync());

            Assert.False(sut.IsAuthorized);
        }
        public async Task Authorize_makes_IsAuthorized_true()
        {
            // arrange
            var options = GetMockOptions();
            var uri     = ApiPathUtility.Build(options.BaseUrl, "/users/getToken");
            var mock    = new Mock <HttpClient>();

            mock.Setup(client => client.SendAsync(
                           It.Is <HttpRequestMessage>(
                               message => message.RequestUri.Equals(uri)),
                           It.IsAny <CancellationToken>()))
            .ReturnsAsync(new HttpResponseMessage
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new JsonContent(JsonConvert.SerializeObject(SucceedTokenResponse))
            });
            var sut = new IamportHttpClient(options, mock.Object);

            // act
            await sut.AuthorizeAsync();

            // assert
            Assert.True(sut.IsAuthorized);
        }
        public void Returns_secondary_when_base_is_null_or_empty(string basePath, string secondPath)
        {
            var actual = ApiPathUtility.Build(basePath, secondPath);

            Assert.Equal(secondPath, actual);
        }
        public void Returns_concated_path(string basePath, string secondPath)
        {
            var actual = ApiPathUtility.Build(basePath, secondPath);

            Assert.Equal("base/api/second", actual);
        }
        public void Returns_aggregated_path_when_secondary_starts_with_trail_slash(string basePath, string secondPath)
        {
            var actual = ApiPathUtility.Build(basePath, secondPath);

            Assert.Equal("base/second", actual);
        }
        public void Returns_secondary_when_secondary_contains_scheme(string basePath, string secondPath)
        {
            var actual = ApiPathUtility.Build(basePath, secondPath);

            Assert.Equal(secondPath, actual);
        }
 private string GetPathAndQuerystring(string pathAndQuerystring)
 {
     return(ApiPathUtility.Build(BasePath, pathAndQuerystring));
 }
예제 #10
0
 private string GetCustomersPath(string customerId)
 {
     return(ApiPathUtility.Build(CustomersPath, WebUtility.UrlEncode(customerId)));
 }