Beispiel #1
0
    private static void AddPathQueryAndHeaderParameters(CodeMethod method)
    {
        var stringType = new CodeType {
            Name = "string",
        };

        method.AddPathQueryOrHeaderParameter(new CodeParameter {
            Name         = "q",
            Kind         = CodeParameterKind.QueryParameter,
            Type         = stringType,
            DefaultValue = "test",
            Description  = "The q option",
            Optional     = true
        });
        method.AddPathQueryOrHeaderParameter(new CodeParameter {
            Name = "test-path",
            Kind = CodeParameterKind.Path,
            Type = stringType
        });
        method.AddPathQueryOrHeaderParameter(new CodeParameter {
            Name        = "Test-Header",
            Kind        = CodeParameterKind.Headers,
            Type        = stringType,
            Description = "The test header",
        });
    }
Beispiel #2
0
    public void WritesExecutableCommandForGetRequestPrimitive()
    {
        method.Kind        = CodeMethodKind.CommandBuilder;
        method.Description = "Test description";
        method.SimpleName  = "User";
        method.HttpMethod  = HttpMethod.Get;
        var stringType = new CodeType
        {
            Name = "string",
        };
        var generatorMethod = new CodeMethod
        {
            Kind       = CodeMethodKind.RequestGenerator,
            Name       = "CreateGetRequestInformation",
            HttpMethod = method.HttpMethod
        };

        method.OriginalMethod = new CodeMethod
        {
            Kind       = CodeMethodKind.RequestExecutor,
            HttpMethod = method.HttpMethod,
            ReturnType = stringType,
            Parent     = method.Parent
        };
        var codeClass = method.Parent as CodeClass;

        codeClass.AddMethod(generatorMethod);

        AddRequestProperties();
        AddRequestBodyParameters(method.OriginalMethod);
        AddPathQueryAndHeaderParameters(generatorMethod);
        generatorMethod.AddPathQueryOrHeaderParameter(new CodeParameter
        {
            Name = "count",
            Kind = CodeParameterKind.QueryParameter,
            Type = new CodeType
            {
                Name       = "boolean",
                IsNullable = true,
            },
        });

        writer.Write(method);
        var result = tw.ToString();

        Assert.Contains("var command = new Command(\"user\");", result);
        Assert.Contains("command.Description = \"Test description\";", result);
        Assert.Contains("var qOption = new Option<string>(\"-q\", getDefaultValue: ()=> \"test\", description: \"The q option\")", result);
        Assert.Contains("qOption.IsRequired = false;", result);
        Assert.Contains("command.AddOption(qOption);", result);
        Assert.Contains("var testHeaderOption = new Option<string>(\"--test-header\", description: \"The test header\")", result);
        Assert.Contains("testHeaderOption.IsRequired = true;", result);
        Assert.Contains("command.AddOption(testHeaderOption);", result);
        Assert.Contains("command.SetHandler(async (invocationContext) => {", result);
        Assert.Contains("var q = invocationContext.ParseResult.GetValueForOption(qOption);", result);
        Assert.Contains("var testHeader = invocationContext.ParseResult.GetValueForOption(testHeaderOption);", result);
        Assert.Contains("var requestInfo = CreateGetRequestInformation", result);
        Assert.Contains("requestInfo.PathParameters.Add(\"test%2Dpath\", testPath);", result);
        Assert.Contains("requestInfo.Headers[\"Test-Header\"] = testHeader;", result);
        Assert.Contains("var response = await RequestAdapter.SendPrimitiveAsync<Stream>(requestInfo, errorMapping: default, cancellationToken: cancellationToken);", result);
        Assert.Contains("var outputFormatterFactory = invocationContext.BindingContext.GetRequiredService<IOutputFormatterFactory>();", result);
        Assert.Contains("var formatter = outputFormatterFactory.GetFormatter(FormatterType.TEXT);", result);
        Assert.Contains("await formatter.WriteOutputAsync(response, null, cancellationToken);", result);
        Assert.Contains("});", result);
        Assert.Contains("return command;", result);
    }