|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System.Net; |
| 6 | +using System.Net.Http.Json; |
| 7 | + |
| 8 | +namespace CommunityToolkit.Datasync.Client.Test.Live; |
| 9 | + |
| 10 | +[ExcludeFromCodeCoverage] |
| 11 | +public class SampleServerTests |
| 12 | +{ |
| 13 | + private readonly bool liveTestsAreEnabled = Environment.GetEnvironmentVariable("DATASYNC_SERVICE_ENDPOINT") is not null; |
| 14 | + private readonly string serviceEndpoint = Environment.GetEnvironmentVariable("DATASYNC_SERVICE_ENDPOINT"); |
| 15 | + |
| 16 | + [SkippableFact] |
| 17 | + public async Task Metadata_GetsSetByServer() |
| 18 | + { |
| 19 | + Skip.IfNot(this.liveTestsAreEnabled); |
| 20 | + |
| 21 | + DateTimeOffset now = DateTimeOffset.UtcNow; |
| 22 | + HttpClient client = new(); |
| 23 | + TodoItem source = new() { Title = "Test item" }; |
| 24 | + HttpResponseMessage response = await client.PostAsJsonAsync($"{this.serviceEndpoint}/tables/TodoItem", source); |
| 25 | + |
| 26 | + response.Should().HaveHttpStatusCode(HttpStatusCode.Created); |
| 27 | + |
| 28 | + TodoItem result = await response.Content.ReadFromJsonAsync<TodoItem>(); |
| 29 | + result.Id.Should().NotBeNullOrEmpty(); |
| 30 | + result.UpdatedAt.Should().NotBeNull().And.BeAfter(now); |
| 31 | + result.Version.Should().NotBeNullOrEmpty(); |
| 32 | + result.Deleted.Should().BeFalse(); |
| 33 | + result.Title.Should().Be("Test item"); |
| 34 | + result.IsComplete.Should().BeFalse(); |
| 35 | + |
| 36 | + response.Headers.Location.Should().NotBeNull().And.BeEquivalentTo(new Uri($"{this.serviceEndpoint}/tables/TodoItem/{result.Id}")); |
| 37 | + response.Headers.ETag.ToString().Should().Be($"\"{result.Version}\""); |
| 38 | + } |
| 39 | + |
| 40 | + // This must match the TodoItem class in the server project. |
| 41 | + public class TodoItem |
| 42 | + { |
| 43 | + public string Id { get; set; } |
| 44 | + public DateTimeOffset? UpdatedAt { get; set; } |
| 45 | + public string Version { get; set; } |
| 46 | + public bool Deleted { get; set; } |
| 47 | + public string Title { get; set; } |
| 48 | + public bool IsComplete { get; set; } |
| 49 | + } |
| 50 | +} |
0 commit comments