Example #1
0
        public async Task Should_RequestWithHeaderAndParams_When_ConfigureServeralOptions()
        {
            // Given
            var content = new { A = "b" };
            var request = MockHttp.When(HttpMethod.Post, "http://localhost:8080/api/index")
                          .WithHeaders(HttpRequestHeader.Authorization.ToString(), "Bearer AccessToken")
                          .WithQueryString("a", "b")
                          .WithQueryString("c", "d")
                          .WithContent(JsonConvert.SerializeObject(content))
                          .Respond(HttpStatusCode.OK, new StringContent("Hello"));

            // When
            var message = await Client.WithUri("http://localhost:8080/api/index")
                          .WithHeader(HttpRequestHeader.Authorization, "Bearer AccessToken")
                          .WithQuery("a", "b")
                          .WithQuery("c", "d")
                          .WithJsonContent(content)
                          .PostAsync();

            // Then
            message.StatusCode.Should().Be(HttpStatusCode.OK);
            var response = await message.Content.ReadAsStringAsync();

            response.Should().Be("Hello");
            MockHttp.GetMatchCount(request).Should().Be(1);
        }
        public async Task ThenItShouldUpdateCertificatesInChunksOf100(int batchSize, int chunksSent)
        {
            Setup();

            var request = MockHttp.When(HttpMethod.Put, "http://localhost:59022/api/v1/certificates/*")
                          .Respond(System.Net.HttpStatusCode.OK, "application/json", "{'status' : 'Boo'}");

            var certificateResponses = Builder <CertificateResponse> .CreateListOfSize(batchSize).Build();

            await AssessorServiceApi.ChangeStatusToPrinted(1, certificateResponses);

            Assert.AreEqual(chunksSent, MockHttp.GetMatchCount(request));
        }
        public async Task DoNotRequestNewTokenWhenRefreshedTokenIsNotExpired()
        {
            MockHttp.Clear();

            var getToken = MockHttp.When(AuthUrl)
                           .WithFormData("grant_type", "password")
                           .Respond(HttpStatusCode.OK, "application/json",
                                    @"{
                        'access_token': 'old_token',
                        'token_type': 'token_type',
                        'refresh_token': 'refresh_token',
                        'expires_in': 0,
                        'scope': 'scope',
                        'jti': '00000000-0000-0000-0000-000000000000'
                    }");

            var refreshToken = MockHttp.When(AuthUrl)
                               .WithFormData("grant_type", "refresh_token")
                               .Respond(HttpStatusCode.OK, "application/json",
                                        @"{
                        'access_token': 'new_token',
                        'token_type': 'token_type',
                        'refresh_token': 'refresh_token',
                        'expires_in': 1000,
                        'scope': 'scope',
                        'jti': '00000000-0000-0000-0000-000000000000'
                    }");

            var provider = CreateTokenProvider();

            await provider.GetAuthorizationHeader(false);

            await provider.GetAuthorizationHeader(false);

            var header = await provider.GetAuthorizationHeader(false);

            Assert.Equal("Authorization", header.Key);
            Assert.Equal("token_type new_token", header.Value);

            Assert.Equal(1, MockHttp.GetMatchCount(getToken));
            Assert.Equal(1, MockHttp.GetMatchCount(refreshToken));
        }
        public async Task SendAdditionalRequestIfTokenExpired()
        {
            var json = @"[
                    'name1',
                    'name2',
                    'name3'
                ]".QuotesToDoubleQuotes();

            var expectedList = new List <string>()
            {
                "name1",
                "name2",
                "name3"
            };

            var url = BaseUrl + $"/v1/o/{OrgName}/apiproducts?count={EntitiesLimit}";

            MockHttp.Clear();

            var getProductsUnauthorized = MockHttp.When(url)
                                          .WithHeaders("Authorization", "token_type access_token")
                                          .Respond(HttpStatusCode.Unauthorized);

            var getToken = MockHttp.When(AuthUrl)
                           .WithFormData("grant_type", "password")
                           .Respond(HttpStatusCode.OK, "application/json",
                                    @"{
                        'access_token': 'access_token',
                        'token_type': 'token_type',
                        'refresh_token': 'refresh_token',
                        'expires_in': 1000,
                        'scope': 'scope',
                        'jti': '00000000-0000-0000-0000-000000000000'
                    }");

            var refreshToken = MockHttp.When(AuthUrl)
                               .WithFormData("grant_type", "refresh_token")
                               .Respond(HttpStatusCode.OK, "application/json",
                                        @"{
                        'access_token': 'new_token',
                        'token_type': 'token_type',
                        'refresh_token': 'refresh_token',
                        'expires_in': 1000,
                        'scope': 'scope',
                        'jti': '00000000-0000-0000-0000-000000000000'
                    }");

            var getProductsSuccessful = MockHttp.When(url)
                                        .WithHeaders(@"Authorization: token_type new_token")
                                        .Respond("application/json", json);

            var apiProductNames = await GetApigeeClient().GetApiProductNames();

            Assert.Equal(expectedList.Count, apiProductNames.Count);
            Assert.Equal(expectedList[0], apiProductNames[0]);
            Assert.Equal(expectedList[1], apiProductNames[1]);
            Assert.Equal(expectedList[2], apiProductNames[2]);

            Assert.Equal(1, MockHttp.GetMatchCount(getProductsUnauthorized));
            Assert.Equal(1, MockHttp.GetMatchCount(getProductsSuccessful));
            Assert.Equal(1, MockHttp.GetMatchCount(getToken));
            Assert.Equal(1, MockHttp.GetMatchCount(refreshToken));
        }