public async Task Patch_Returns_PreconditionFailed_WithOriginalValue_IfVersionMismatch() { TestEntity entity = CreateTestEntity(); string stringValue = entity.StringValue; int intValue = entity.IntValue; Uri location; // create a new entity to update using (HttpResponseMessage postResponse = await this.testClient.PostAsJsonAsync(Address, entity)) { Assert.Equal(HttpStatusCode.Created, postResponse.StatusCode); location = postResponse.Headers.Location; } // Update some properties TestEntitySimple patchEntity = new TestEntitySimple { Id = entity.Id, StringValue = "Updated", IntValue = 84 }; HttpRequestMessage patchRequest = new HttpRequestMessage { RequestUri = location, Method = new HttpMethod("PATCH"), Content = (HttpContent) new ObjectContent <TestEntitySimple>(patchEntity, this.config.Formatters.JsonFormatter) }; patchRequest.Headers.IfMatch.Add(EntityTagHeaderValue.Parse("\"QUJDREVG\"")); using (HttpResponseMessage patchResponse = await this.testClient.SendAsync(patchRequest)) { TestEntity conflict = await patchResponse.Content.ReadAsAsync <TestEntity>(); Assert.Equal(HttpStatusCode.PreconditionFailed, patchResponse.StatusCode); Assert.NotNull(conflict.CreatedAt); Assert.NotNull(conflict.UpdatedAt); } // Query the entity back using location header value to ensure it was *not* updated UriBuilder queryUri = new UriBuilder(location) { }; using (HttpResponseMessage response = await this.testClient.GetAsync(queryUri.Uri)) { Assert.Equal(HttpStatusCode.OK, response.StatusCode); TestEntity result = await response.Content.ReadAsAsync <TestEntity>(); Assert.Equal(stringValue, result.StringValue); Assert.Equal(intValue, result.IntValue); Assert.NotNull(result.CreatedAt); Assert.NotNull(result.UpdatedAt); } }
public async Task Post_EntityIsCreated_ReturnsCreatedWithLocationHeader() { TestEntitySimple entity = new TestEntitySimple { StringValue = "Apple", IntValue = 1 }; Uri location; // Post a new entity and verify entity returned using (HttpResponseMessage postResponse = await this.testClient.PostAsJsonAsync(Address, entity)) { Assert.Equal(HttpStatusCode.Created, postResponse.StatusCode); location = postResponse.Headers.Location; Assert.NotNull(location); TestEntity result = await postResponse.Content.ReadAsAsync <TestEntity>(); this.VerifySimpleEntitiesEqual(entity, result); } // Query the entity back using location header value to ensure it was inserted to the db UriBuilder queryUri = new UriBuilder(location) { }; using (HttpResponseMessage queryResponse = await this.testClient.GetAsync(queryUri.Uri)) { Assert.Equal(HttpStatusCode.OK, queryResponse.StatusCode); TestEntity result = await queryResponse.Content.ReadAsAsync <TestEntity>(); this.VerifySimpleEntitiesEqual(entity, result); Assert.NotNull(result.CreatedAt); Assert.NotNull(result.UpdatedAt); Assert.NotNull(result.Version); } }
public async Task Patch_EntityIsUpdated_ReturnsOk() { TestEntity entity = CreateTestEntity(); DateTimeOffset?createdAt; DateTimeOffset?updatedAt; string id; Uri location; // Create a new entity to update using (HttpResponseMessage postResponse = await this.testClient.PostAsJsonAsync(Address, entity)) { Assert.Equal(HttpStatusCode.Created, postResponse.StatusCode); location = postResponse.Headers.Location; TestEntity result = await postResponse.Content.ReadAsAsync <TestEntity>(); this.VerifyEntitiesEqual(entity, result); Assert.NotNull(result.CreatedAt); Assert.NotNull(result.UpdatedAt); Assert.NotNull(result.Version); createdAt = result.CreatedAt; updatedAt = result.UpdatedAt; id = result.Id; } // Delay slightly to avoid resolution conflict of less than 1 ms. await Task.Delay(50); // Update some properties var patchEntity = new TestEntitySimple { Id = entity.Id, StringValue = "Updated", IntValue = 84 }; HttpRequestMessage request = new HttpRequestMessage { RequestUri = location, Method = new HttpMethod("PATCH"), Content = (HttpContent) new ObjectContent <TestEntitySimple>(patchEntity, this.config.Formatters.JsonFormatter) }; using (HttpResponseMessage patchResponse = await this.testClient.SendAsync(request)) { Assert.Equal(HttpStatusCode.OK, patchResponse.StatusCode); TestEntity result = await patchResponse.Content.ReadAsAsync <TestEntity>(); this.VerifySimpleEntitiesEqual(patchEntity, result); Assert.Equal(createdAt, result.CreatedAt); Assert.NotEqual(updatedAt, result.UpdatedAt); Assert.NotNull(result.Version); } // Query the entity back using location header value to ensure it was updated UriBuilder queryUri = new UriBuilder(location); using (HttpResponseMessage queryResponse = await this.testClient.GetAsync(queryUri.Uri)) { Assert.Equal(HttpStatusCode.OK, queryResponse.StatusCode); TestEntity result = await queryResponse.Content.ReadAsAsync <TestEntity>(); this.VerifySimpleEntitiesEqual(patchEntity, result); Assert.NotNull(result.CreatedAt); Assert.NotNull(result.UpdatedAt); Assert.NotNull(result.Version); } }
private void VerifySimpleEntitiesEqual(TestEntitySimple expected, TestEntity result) { Assert.Equal(expected.StringValue, result.StringValue); Assert.Equal(expected.IntValue, result.IntValue); Assert.Equal(expected.BooleanValue, result.BooleanValue); }