// ========================================================================== // Squidex Headless CMS // ========================================================================== // Copyright (c) Squidex UG (haftungsbeschraenkt) // All rights reserved. Licensed under the MIT license. // ========================================================================== using Squidex.ClientLibrary; using TestSuite.Model; #pragma warning disable SA1300 // Element should begin with upper-case letter #pragma warning disable SA1507 // Code should not contain multiple blank lines in a row namespace TestSuite.ApiTests; public class ContentLanguageTests(ContentFixture fixture) : IClassFixture { public ContentFixture _ { get; } = fixture; [Fact] public async Task Should_filter_language() { // STEP 1: Create content. var content = await _.Contents.CreateAsync( new TestEntityData { Localized = new Dictionary { ["de"] = "Hallo", ["en"] = "Hello", }, }, ContentCreateOptions.AsPublish, QueryContext.Default.WithLanguages("de")); Assert.False(content.Data.Localized.ContainsKey("en")); Assert.Equal("Hallo", content.Data.Localized["de"]); } [Theory] [InlineData("de", "Hallo")] [InlineData("en", "Hello")] [InlineData("custom", "Custom")] public async Task Should_flatten_language(string code, string expected) { // STEP 1: Create content. var content = await _.Contents.CreateAsync( new TestEntityData { Localized = new Dictionary { ["de"] = "Hallo", ["en"] = "Hello", ["custom"] = "Custom", }, }, ContentCreateOptions.AsPublish); // STEP 2: Get content. var contents = _.Client.DynamicContents(_.Contents.SchemaName); var contentFlatten = await contents.GetAsync(content.Id, QueryContext.Default.Flatten().WithLanguages(code)); Assert.Equal(expected, (string?)contentFlatten.Data["localized"]); } [Fact] public async Task Should_provide_etag_based_on_headers() { // STEP 1: Create content. var content = await _.Contents.CreateAsync( new TestEntityData { Localized = new Dictionary { ["de"] = "Hallo", ["en"] = "Hello", }, }, ContentCreateOptions.AsPublish); // STEP 2: Get content. var (etag1, _) = await GetEtagAsync(content.Id, []); var (etag2, _) = await GetEtagAsync( content.Id, new Dictionary { ["X-Flatten"] = "1", }); var (etag3, _) = await GetEtagAsync( content.Id, new Dictionary { ["X-Languages"] = "en", }); var (etag4, _) = await GetEtagAsync( content.Id, new Dictionary { ["X-Languages"] = "en", ["X-Flatten"] = "1", }); static void AssertValue(string? value, string? not = null) { Assert.NotNull(value); Assert.NotEmpty(value); Assert.NotEqual(not, value); } AssertValue(etag1); AssertValue(etag2, etag1); AssertValue(etag3, etag1); AssertValue(etag4, etag1); } private async Task<(string?, string)> GetEtagAsync(string id, Dictionary headers) { var url = $"{_.Client.Options.Url}api/content/{_.AppName}/{_.SchemaName}/{id}"; var httpClient = _.Client.CreateHttpClient(); foreach (var (key, value) in headers) { httpClient.DefaultRequestHeaders.TryAddWithoutValidation(key, value); } var httpResponse = await httpClient.GetAsync(url); return (httpResponse.Headers.GetValues("ETag").FirstOrDefault(), httpResponse.Headers.Vary.ToString()); } }