public async Task TestThrottleDelaySecs(int throttleDelaySecs)
        {
            // Set throttleDelaySecs
            var request = new HttpRequestMessage(HttpMethod.Post, $"{uriApi}throttle/{throttleDelaySecs}");

            using (var response = await _client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            }

            // Check if value was assigned properly
            request = new HttpRequestMessage(HttpMethod.Get, $"{uriApi}");

            using (var response = await _client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                var res = HttpContentHelper.DerializeJsonFromStream <Dictionary <string, int> >(await response.Content.ReadAsStreamAsync());

                // Assert
                Assert.True(res.ContainsKey("throttleDelaySecs"));

                // Assert
                Assert.True(res["throttleDelaySecs"] == throttleDelaySecs);
            }

            // Verify is throttling is indeed working as expected
            request = new HttpRequestMessage(HttpMethod.Get, $"{uriOdata}?$count=true&$top=1");

            Stopwatch stopwatch = Stopwatch.StartNew();

            using (var response = await _client.SendAsync(request))
            {
                stopwatch.Stop();

                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                var res = HttpContentHelper.DerializeJsonFromStream <ODataResponse <Person> >(await response.Content.ReadAsStreamAsync());

                // Assert - Only one record returned
                Assert.Single(res.value);

                // Assert - Executed at least as long as throttleDelaySecs
                Assert.True(stopwatch.ElapsedMilliseconds > throttleDelaySecs * 1000);
            }

            // Reset throttleDelaySecs
            request         = new HttpRequestMessage(HttpMethod.Post, $"{uriApi}throttle/0");
            request.Content = HttpContentHelper.CreateHttpContent(0);

            using (var response = await _client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);
            }
        }
Exemplo n.º 2
0
        public async Task TestCRUD()
        {
            var entity = DbContextHelper.InitObject(new Person(), idCRUD);

            // GET record which is not exists yet
            var request = new HttpRequestMessage(HttpMethod.Get, $"{uri}{entity.id}");

            using (var response = await _client.SendAsync(request))
            {
                // Assert
                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            }

            // POST new record
            request         = new HttpRequestMessage(HttpMethod.Post, uri);
            request.Content = HttpContentHelper.CreateHttpContent(entity);

            using (var response = await _client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            }

            // GET record created above and compare with original
            request = new HttpRequestMessage(HttpMethod.Get, $"{uri}{entity.id}");

            using (var response = await _client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                var res = HttpContentHelper.DerializeJsonFromStream <Person>(await response.Content.ReadAsStreamAsync());

                // Assert
                Assert.Equal(HttpContentHelper.ObjectToString(res), HttpContentHelper.ObjectToString(entity));
            }

            // PUT updated record
            request = new HttpRequestMessage(HttpMethod.Put, $"{uri}{entity.id}");

            entity    = DbContextHelper.InitObject(new Person(), idCRUD + 1);
            entity.id = idCRUD;

            request.Content = HttpContentHelper.CreateHttpContent(entity);

            using (var response = await _client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
            }

            // GET updated record and compare with original
            request = new HttpRequestMessage(HttpMethod.Get, $"{uri}{entity.id}");

            using (var response = await _client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                var res = HttpContentHelper.DerializeJsonFromStream <Person>(await response.Content.ReadAsStreamAsync());

                // Assert
                Assert.Equal(HttpContentHelper.ObjectToString(res), HttpContentHelper.ObjectToString(entity));
            }

            // PATCH record
            request = new HttpRequestMessage(HttpMethod.Patch, $"{uri}{entity.id}");

            entity    = DbContextHelper.InitObject(new Person(), idCRUD + 2);
            entity.id = idCRUD;

            request.Content = HttpContentHelper.CreateHttpContent(entity);

            using (var response = await _client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
            }

            // GET updated record and compare with original
            request = new HttpRequestMessage(HttpMethod.Get, $"{uri}{entity.id}");

            using (var response = await _client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.OK, response.StatusCode);

                var res = HttpContentHelper.DerializeJsonFromStream <Person>(await response.Content.ReadAsStreamAsync());

                // Assert
                Assert.Equal(HttpContentHelper.ObjectToString(res), HttpContentHelper.ObjectToString(entity));
            }

            // Attempt to POST same record again
            request         = new HttpRequestMessage(HttpMethod.Post, uri);
            request.Content = HttpContentHelper.CreateHttpContent(entity);

            using (var response = await _client.SendAsync(request))
            {
                // Assert
                Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode);
            }

            // DELETE record
            request = new HttpRequestMessage(HttpMethod.Delete, $"{uri}{entity.id}");

            using (var response = await _client.SendAsync(request))
            {
                response.EnsureSuccessStatusCode();

                // Assert
                Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);
            }

            // Attempt to GET deleted record
            request = new HttpRequestMessage(HttpMethod.Get, $"{uri}{entity.id}");

            using (var response = await _client.SendAsync(request))
            {
                // Assert
                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            }
        }