public async Task <int> RunAsync(IReadOnlyList <string> args)
        {
            const string defaultUrl = "http://localhost:4117/";

            var argsReader = new ArgsReader(args);

            if (argsReader.ReadFlag("?|h|help"))
            {
                throw new ArgsReaderException("");
            }

            var command = argsReader.ReadArgument();

            if (command == "host")
            {
                string url = argsReader.ReadOption("url") ?? defaultUrl;
                argsReader.VerifyComplete();

                new WebHostBuilder().UseKestrel().UseUrls(url).Configure(app => app.Run(HostAsync)).Build().Run();

                return(0);
            }
            else if (command == "test")
            {
                var baseUri   = new Uri(argsReader.ReadOption("url") ?? defaultUrl);
                var testNames = argsReader.ReadArguments();
                argsReader.VerifyComplete();

                var api = new HttpClientConformanceApi(
                    new HttpClientServiceSettings
                {
                    BaseUri = baseUri,
                });

                var tester = new ConformanceApiTester(m_tests, api, new HttpClient {
                    BaseAddress = baseUri
                });

                var results = new List <ConformanceTestResult>();

                if (testNames.Count == 0)
                {
                    results.AddRange((await tester.RunAllTestsAsync()).Results);
                }
                else
                {
                    foreach (var testName in testNames)
                    {
                        var testInfo = m_tests.SingleOrDefault(x => x.Test == testName);
                        if (testInfo == null)
                        {
                            Console.WriteLine($"Test not found: {testName}");
                            return(-1);
                        }

                        results.Add(await tester.RunTestAsync(testInfo));
                    }
                }

                var failureCount = 0;
                foreach (var result in results.Where(x => x.Status == ConformanceTestStatus.Fail))
                {
                    Console.WriteLine($"{result.TestName} fail: {result.Message}");
                    failureCount++;
                }

                Console.WriteLine($"{results.Count} tests: {results.Count - failureCount} passed, {failureCount} failed.");

                return(failureCount == 0 ? 0 : 1);
            }
            else if (command == "fsd")
            {
                var outputPath   = argsReader.ReadOption("output");
                var shouldVerify = argsReader.ReadFlag("verify");
                argsReader.VerifyComplete();

                return(WriteText(path: outputPath, contents: m_fsdText, shouldVerify: shouldVerify));
            }
            else if (command == "json")
            {
                var outputPath   = argsReader.ReadOption("output");
                var shouldVerify = argsReader.ReadFlag("verify");
                argsReader.VerifyComplete();

                return(WriteText(path: outputPath, contents: m_testsJson, shouldVerify: shouldVerify));
            }
            else if (command != null)
            {
                throw new ArgsReaderException($"Invalid command: {command}");
            }
            else
            {
                throw new ArgsReaderException("Missing command.");
            }
        }
Ejemplo n.º 2
0
    public async Task <int> RunAsync(IReadOnlyList <string> args)
    {
        const string defaultUrl = "http://localhost:4117/";

        var argsReader = new ArgsReader(args);

        if (argsReader.ReadFlag("?|h|help"))
        {
            throw new ArgsReaderException("");
        }

        var serializerName           = argsReader.ReadOption("serializer")?.ToLowerInvariant();
        ServiceSerializer serializer = serializerName switch
        {
            null or "systemtextjson" => SystemTextJsonServiceSerializer.Instance,
            "newtonsoftjson" or "obsoletejson" => NewtonsoftJsonServiceSerializer.Instance,
                          _ => throw new ArgsReaderException("Unsupported serializer."),
        };
        var contentSerializer = HttpContentSerializer.Create(serializer);

#pragma warning disable CS0618 // Type or member is obsolete
        if (serializerName is "obsoletejson")
        {
            contentSerializer = new JsonHttpContentSerializer(new JsonHttpContentSerializerSettings {
                ForceAsyncIO = true
            });
        }
#pragma warning restore CS0618 // Type or member is obsolete

        var jsonSerializer = serializer as JsonServiceSerializer ?? NewtonsoftJsonServiceSerializer.Instance;
        var tests          = ConformanceTestsInfo.FromJson(m_testsJson, jsonSerializer).Tests !;

        var command = argsReader.ReadArgument();

        if (command == "host")
        {
            var url = argsReader.ReadOption("url") ?? defaultUrl;
            argsReader.VerifyComplete();

            var service = new ConformanceApiService(
                new ConformanceApiServiceSettings
            {
                Tests          = tests,
                JsonSerializer = jsonSerializer,
            });

            await new WebHostBuilder()
            .UseKestrel(options => options.AllowSynchronousIO = serializerName is "newtonsoftjson")
            .UseUrls(url)
            .Configure(app => app.Run(httpContext => HostAsync(httpContext, service, contentSerializer)))
            .Build()
            .RunAsync();

            return(0);
        }

        if (command == "test")
        {
            var baseUri   = new Uri(argsReader.ReadOption("url") ?? defaultUrl);
            var testNames = argsReader.ReadArguments();
            argsReader.VerifyComplete();

            var api = new HttpClientConformanceApi(
                new HttpClientServiceSettings
            {
                BaseUri           = baseUri,
                ContentSerializer = contentSerializer,
            });

            var tester = new ConformanceApiTester(
                new ConformanceApiTesterSettings
            {
                Tests      = tests,
                Api        = api,
                HttpClient = new HttpClient {
                    BaseAddress = baseUri
                },
                JsonSerializer = jsonSerializer,
            });

            var results = new List <ConformanceTestResult>();

            if (testNames.Count == 0)
            {
                results.AddRange((await tester.RunAllTestsAsync()).Results);
            }
            else
            {
                foreach (var testName in testNames)
                {
                    var testInfo = tests.SingleOrDefault(x => x.Test == testName);
                    if (testInfo == null)
                    {
                        Console.WriteLine($"Test not found: {testName}");
                        return(-1);
                    }

                    results.Add(await tester.RunTestAsync(testInfo));
                }
            }

            var failureCount = 0;
            foreach (var result in results.Where(x => x.Status == ConformanceTestStatus.Fail))
            {
                Console.WriteLine($"{result.TestName} fail: {result.Message}");
                failureCount++;
            }

            Console.WriteLine($"{results.Count} tests: {results.Count - failureCount} passed, {failureCount} failed.");

            return(failureCount == 0 ? 0 : 1);
        }

        if (command == "fsd")
        {
            var outputPath   = argsReader.ReadOption("output");
            var shouldVerify = argsReader.ReadFlag("verify");
            argsReader.VerifyComplete();

            return(WriteText(path: outputPath, contents: m_fsdText, shouldVerify: shouldVerify));
        }

        if (command == "json")
        {
            var outputPath   = argsReader.ReadOption("output");
            var shouldVerify = argsReader.ReadFlag("verify");
            argsReader.VerifyComplete();

            return(WriteText(path: outputPath, contents: m_testsJson, shouldVerify: shouldVerify));
        }

        if (command != null)
        {
            throw new ArgsReaderException($"Invalid command: {command}");
        }

        throw new ArgsReaderException("Missing command.");
    }