private static void AppendTest400BadRequestInHeader( StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, ResponseTypeNameAndItemSchema contractReturnTypeName) { var headerRequiredParameters = endpointMethodMetadata.GetHeaderRequiredParameters(); var testForParameters = headerRequiredParameters .Where(x => x.Schema.GetDataType() != OpenApiDataTypeConstants.String) .ToList(); if (headerRequiredParameters.Count == 0) { return; } var relativeRef = RenderRelativeRef(endpointMethodMetadata); foreach (var testForParameter in testForParameters) { sb.AppendLine(); sb.AppendLine(8, "[Theory]"); sb.AppendLine(8, $"[InlineData(\"{relativeRef}\")]"); sb.AppendLine(8, $"public async Task {endpointMethodMetadata.MethodName}_BadRequest_InHeader_{testForParameter.Name.EnsureFirstCharacterToUpper()}(string relativeRef)"); sb.AppendLine(8, "{"); sb.AppendLine(12, "// Arrange"); if (headerRequiredParameters.Count > 0) { foreach (var headerParameter in headerRequiredParameters) { var useInvalidData = headerParameter.Name == testForParameter.Name; var propertyValueGenerated = PropertyValueGenerator(headerParameter, endpointMethodMetadata.ComponentsSchemas, useInvalidData, customValue: null); sb.AppendLine( 12, $"HttpClient.DefaultRequestHeaders.Add(\"{headerParameter.Name}\", \"{propertyValueGenerated}\");"); } sb.AppendLine(); } AppendNewRequestModel(12, sb, endpointMethodMetadata, contractReturnTypeName.StatusCode); sb.AppendLine(); AppendActHttpClientOperation(12, sb, endpointMethodMetadata.HttpOperation, useData: true); sb.AppendLine(); sb.AppendLine(12, "// Assert"); sb.AppendLine(12, "response.Should().NotBeNull();"); sb.AppendLine(12, $"response.StatusCode.Should().Be(HttpStatusCode.{HttpStatusCode.BadRequest});"); sb.AppendLine(8, "}"); } }
private static void AppendTest400BadRequestInBody(StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, Tuple <HttpStatusCode, string, OpenApiSchema> contractReturnTypeName) { if (!endpointMethodMetadata.HasContractParameterRequestBody()) { return; } var schema = endpointMethodMetadata.ContractParameter?.ApiOperation.RequestBody?.Content.GetSchema(); if (schema == null) { return; } var modelSchema = endpointMethodMetadata.ComponentsSchemas.First(x => x.Key == schema.GetModelName()).Value; var headerRequiredParameters = endpointMethodMetadata.GetHeaderRequiredParameters(); var relevantSchemas = new List <KeyValuePair <string, OpenApiSchema> >(); foreach (var schemaProperty in modelSchema.Properties) { if (modelSchema.Required.Contains(schemaProperty.Key) || schemaProperty.Value.IsFormatTypeOfEmail() || schemaProperty.Value.IsFormatTypeOfDate() || schemaProperty.Value.IsFormatTypeOfDateTime() || schemaProperty.Value.IsFormatTypeOfTime() || schemaProperty.Value.IsFormatTypeOfTimestamp()) { relevantSchemas.Add(schemaProperty); } } var relativeRef = RenderRelativeRef(endpointMethodMetadata); foreach (var testForSchema in relevantSchemas) { sb.AppendLine(); sb.AppendLine(8, "[Theory]"); sb.AppendLine(8, $"[InlineData(\"{relativeRef}\")]"); sb.AppendLine(8, $"public async Task {endpointMethodMetadata.MethodName}_BadRequest_InBody_{testForSchema.Key.EnsureFirstCharacterToUpper()}(string relativeRef)"); sb.AppendLine(8, "{"); sb.AppendLine(12, "// Arrange"); if (headerRequiredParameters.Count > 0) { foreach (var headerParameter in headerRequiredParameters) { string propertyValueGenerated = PropertyValueGenerator(headerParameter, endpointMethodMetadata.ComponentsSchemas, false, null); sb.AppendLine( 12, $"HttpClient.DefaultRequestHeaders.Add(\"{headerParameter.Name}\", \"{propertyValueGenerated}\");"); } sb.AppendLine(); } AppendNewRequestModelForBadRequest(12, sb, endpointMethodMetadata, contractReturnTypeName.Item1, testForSchema); sb.AppendLine(); AppendActHttpClientOperation(12, sb, endpointMethodMetadata.HttpOperation, true, true); sb.AppendLine(); sb.AppendLine(12, "// Assert"); sb.AppendLine(12, "response.Should().NotBeNull();"); sb.AppendLine(12, $"response.StatusCode.Should().Be(HttpStatusCode.{HttpStatusCode.BadRequest});"); sb.AppendLine(8, "}"); } }
private static void AppendTest400BadRequestInBody( StringBuilder sb, EndpointMethodMetadata endpointMethodMetadata, ResponseTypeNameAndItemSchema contractReturnTypeName) { if (!endpointMethodMetadata.HasContractParameterRequestBody()) { return; } if (endpointMethodMetadata.IsContractParameterRequestBodyUsedAsMultipartFormData()) { return; } var schema = endpointMethodMetadata.ContractParameter?.ApiOperation.RequestBody?.Content.GetSchemaByFirstMediaType(); if (schema is null) { return; } var modelName = schema.GetModelName(); if (string.IsNullOrEmpty(modelName)) { return; } var modelSchema = endpointMethodMetadata.ComponentsSchemas.GetSchemaByModelName(modelName); var relevantSchemas = endpointMethodMetadata.GetRelevantSchemasForBadRequestBodyParameters(modelSchema); var headerRequiredParameters = endpointMethodMetadata.GetHeaderRequiredParameters(); var relativeRef = RenderRelativeRef(endpointMethodMetadata); foreach (var testForSchema in relevantSchemas) { sb.AppendLine(); sb.AppendLine(8, "[Theory]"); sb.AppendLine(8, $"[InlineData(\"{relativeRef}\")]"); sb.AppendLine(8, $"public async Task {endpointMethodMetadata.MethodName}_BadRequest_InBody_{testForSchema.Key.EnsureFirstCharacterToUpper()}(string relativeRef)"); sb.AppendLine(8, "{"); sb.AppendLine(12, "// Arrange"); if (headerRequiredParameters.Count > 0) { foreach (var headerParameter in headerRequiredParameters) { var propertyValueGenerated = PropertyValueGenerator(headerParameter, endpointMethodMetadata.ComponentsSchemas, useForBadRequest: false, customValue: null); sb.AppendLine( 12, $"HttpClient.DefaultRequestHeaders.Add(\"{headerParameter.Name}\", \"{propertyValueGenerated}\");"); } sb.AppendLine(); } AppendNewRequestModelForBadRequest(12, sb, endpointMethodMetadata, contractReturnTypeName.StatusCode, testForSchema); sb.AppendLine(); AppendActHttpClientOperation(12, sb, endpointMethodMetadata.HttpOperation, useData: true, isDataJson: true); sb.AppendLine(); sb.AppendLine(12, "// Assert"); sb.AppendLine(12, "response.Should().NotBeNull();"); sb.AppendLine(12, $"response.StatusCode.Should().Be(HttpStatusCode.{HttpStatusCode.BadRequest});"); sb.AppendLine(8, "}"); } }