diff --git a/tests/Kiota.Builder.Tests/Writers/HTTP/CodeClassDeclarationWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/HTTP/CodeClassDeclarationWriterTests.cs index 836f3e5a9b..adb79e9105 100644 --- a/tests/Kiota.Builder.Tests/Writers/HTTP/CodeClassDeclarationWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/HTTP/CodeClassDeclarationWriterTests.cs @@ -39,7 +39,7 @@ public void Dispose() } [Fact] - public async Task TestWriteTypeDeclaration() + public async Task WritesBaseUrlProperty() { var codeClass = new CodeClass { @@ -59,7 +59,7 @@ public async Task TestWriteTypeDeclaration() }; codeClass.AddProperty(urlTemplateProperty); - // Add a new property named BaseUrl and set its value to the baseUrl string + // Add base url property var baseUrlProperty = new CodeProperty { Name = "BaseUrl", @@ -90,4 +90,138 @@ public async Task TestWriteTypeDeclaration() Assert.Contains("# Base url for the server/host", result); Assert.Contains("@url = https://example.com", result); } + + [Fact] + public async Task WritesRequestExecutorMethods() + { + var codeClass = new CodeClass + { + Name = "TestClass", + Kind = CodeClassKind.RequestBuilder + }; + var urlTemplateProperty = new CodeProperty + { + Name = "urlTemplate", + Kind = CodePropertyKind.UrlTemplate, + DefaultValue = "\"{+baseurl}/posts\"", + Type = new CodeType + { + Name = "string", + IsExternal = true + }, + Documentation = new CodeDocumentation + { + DescriptionTemplate = "The URL template for the request." + } + }; + codeClass.AddProperty(urlTemplateProperty); + + // Add base url property + var baseUrlProperty = new CodeProperty + { + Name = "BaseUrl", + Kind = CodePropertyKind.Custom, + Access = AccessModifier.Private, + DefaultValue = "https://example.com", + Type = new CodeType { Name = "string", IsExternal = true } + }; + codeClass.AddProperty(baseUrlProperty); + + var method = new CodeMethod + { + Name = "get", + Kind = CodeMethodKind.RequestExecutor, + Documentation = new CodeDocumentation { DescriptionTemplate = "GET method" }, + ReturnType = new CodeType { Name = "void" } + }; + codeClass.AddMethod(method); + + var postMethod = new CodeMethod + { + Name = "post", + Kind = CodeMethodKind.RequestExecutor, + Documentation = new CodeDocumentation { DescriptionTemplate = "Post method" }, + ReturnType = new CodeType { Name = "void" }, + RequestBodyContentType = "application/json" + }; + + + var typeDefinition = new CodeClass + { + Name = "PostParameter", + }; + + var properties = new List + { + new() { + Name = "body", + Kind = CodePropertyKind.Custom, + Type = new CodeType { Name = "string", IsExternal = true } + }, + new() { + Name = "id", + Kind = CodePropertyKind.Custom, + Type = new CodeType { Name = "int", IsExternal = true } + }, + new() { + Name = "title", + Kind = CodePropertyKind.Custom, + Type = new CodeType { Name = "string", IsExternal = true } + }, + new() { + Name = "userId", + Kind = CodePropertyKind.Custom, + Type = new CodeType { Name = "int", IsExternal = true } + } + }; + + typeDefinition.AddProperty(properties.ToArray()); + + // Define the parameter with the specified properties + var postParameter = new CodeParameter + { + Name = "postParameter", + Kind = CodeParameterKind.RequestBody, + Type = new CodeType + { + Name = "PostParameter", + TypeDefinition = typeDefinition + } + }; + + // Add the parameter to the post method + postMethod.AddParameter(postParameter); + + codeClass.AddMethod(postMethod); + + var patchMethod = new CodeMethod + { + Name = "patch", + Kind = CodeMethodKind.RequestExecutor, + Documentation = new CodeDocumentation { DescriptionTemplate = "Patch method" }, + ReturnType = new CodeType { Name = "void" } + }; + codeClass.AddMethod(patchMethod); + + root.AddClass(codeClass); + + await ILanguageRefiner.RefineAsync(new GenerationConfiguration { Language = GenerationLanguage.HTTP }, root); + + writer.Write(codeClass.StartBlock); + var result = tw.ToString(); + + // Check HTTP operations + Assert.Contains("GET {{url}}/posts HTTP/1.1", result); + Assert.Contains("PATCH {{url}}/posts HTTP/1.1", result); + Assert.Contains("POST {{url}}/posts HTTP/1.1", result); + + // Check content type + Assert.Contains("Content-Type: application/json", result); + + // check the request body + Assert.Contains("\"body\": \"string\"", result); + Assert.Contains("\"id\": 0", result); + Assert.Contains("\"title\": \"string\"", result); + Assert.Contains("\"userId\": 0", result); + } }