예제 #1
0
        private static void AppendTextContent(
            StringBuilder sb,
            EndpointMethodMetadata endpointMethodMetadata,
            HttpStatusCode testExpectedHttpStatusCode,
            Tuple <HttpStatusCode, string, OpenApiSchema> contractReturnTypeName)
        {
            sb.AppendLine(8, "{");
            if (endpointMethodMetadata.HasContractParameterRequestBody())
            {
                sb.AppendLine(12, "// Arrange");
                var headerParameters = endpointMethodMetadata.GetHeaderParameters();
                if (headerParameters.Count > 0)
                {
                    foreach (var headerParameter in headerParameters)
                    {
                        string propertyValueGenerated = PropertyValueGenerator(headerParameter, endpointMethodMetadata.ComponentsSchemas, false, null);
                        sb.AppendLine(
                            12,
                            $"HttpClient.DefaultRequestHeaders.Add(\"{headerParameter.Name}\", \"{propertyValueGenerated}\");");
                    }

                    sb.AppendLine();
                }

                AppendNewRequestModel(12, sb, endpointMethodMetadata, contractReturnTypeName.Item1);
                sb.AppendLine();
                AppendActHttpClientOperation(12, sb, endpointMethodMetadata.HttpOperation, true);
            }
            else
            {
                AppendActHttpClientOperation(12, sb, endpointMethodMetadata.HttpOperation);
            }

            sb.AppendLine();
            sb.AppendLine(12, "// Assert");
            sb.AppendLine(12, "response.Should().NotBeNull();");
            sb.AppendLine(12, $"response.StatusCode.Should().Be(HttpStatusCode.{testExpectedHttpStatusCode});");

            if (testExpectedHttpStatusCode == HttpStatusCode.OK &&
                !string.IsNullOrEmpty(contractReturnTypeName.Item2) &&
                contractReturnTypeName.Item3 != null && !contractReturnTypeName.Item3.IsSimpleDataType())
            {
                var modelName = OpenApiDocumentSchemaModelNameHelper.EnsureModelNameWithNamespaceIfNeeded(endpointMethodMetadata, contractReturnTypeName.Item2);

                sb.AppendLine();
                sb.AppendLine(12, $"var responseData = await response.DeserializeAsync<{modelName}>(JsonSerializerOptions);");
                sb.AppendLine(12, "responseData.Should().NotBeNull();");
            }

            sb.AppendLine(8, "}");
        }
예제 #2
0
        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, "}");
        }
    }