예제 #1
0
        public async Task TestFilterOrderDesc(string keyField, int rangeStart, int rangeEnd)
        {
            int expectedCount = rangeEnd - rangeStart + 1;

            // Arrange
            var request = new HttpRequestMessage(HttpMethod.Get, $"{uri}?$count=true&$orderby={keyField} desc&$filter=({keyField} ge {rangeStart}) and ({keyField} le {rangeEnd})");

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

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

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

                // Assert - Values count
                Assert.Equal(res.value.Count, expectedCount);

                // Assert - Count
                Assert.Equal(res.odataCount, expectedCount);

                // Assert - first id
                Assert.Equal(res.value[0].id, rangeEnd);

                // Assert - last id
                Assert.Equal(res.value[res.value.Count - 1].id, rangeStart);
            }
        }
예제 #2
0
        public async Task TestFilterContains(string fieldName, string contains, int expectedCount)
        {
            // Arrange
            var request = new HttpRequestMessage(HttpMethod.Get, $"{uri}?$count=true&$filter=contains({fieldName},'{contains}')");

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

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

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

                // Assert - Values count
                Assert.Equal(res.value.Count, expectedCount);

                // Assert - Count
                Assert.Equal(res.odataCount, expectedCount);

                // Assert
                foreach (var val in res.value)
                {
                    Assert.Contains(contains, val.first);
                }
            }
        }
예제 #3
0
        public async Task TestCountTop(string method, int top)
        {
            // Arrange
            var request = new HttpRequestMessage(new HttpMethod(method), $"{uri}?$count=true&$top={top}");

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

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

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

                // Assert - Top
                Assert.Equal(res.value.Count, top);

                // Assert - Count +/- 1
                Assert.True((res.odataCount >= odataCount) && (res.odataCount <= (odataCount + 1)));
            }
        }
        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);
            }
        }
예제 #5
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);
            }
        }