Esempio n. 1
0
        public async Task ExecuteAsync_SwaggerOnlyWithNoBase_ShowsWarning()
        {
            string swaggerAddress = "https://localhost/v2/swagger.json";
            string swaggerContent = @"{
  ""openapi"": ""3.0.0"",
  ""info"": {
    ""version"": ""v1""
  },
  ""paths"": {
    ""/pets"": {
    }
  }
}";

            ArrangeInputs(commandText: $"connect --swagger {swaggerAddress}",
                          baseAddress: null,
                          path: null,
                          urlsWithResponse: new Dictionary <string, string>()
            {
                { swaggerAddress, swaggerContent }
            },
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out _,
                          out IPreferences preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences);

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.Null(httpState.BaseAddress);
            Assert.Contains(Resources.Strings.ConnectCommand_Status_NoBase, shellState.Output, StringComparer.Ordinal);
        }
Esempio n. 2
0
        public async Task ExecuteAsync_WithOnlyRoot_SendsTelemetry()
        {
            string rootAddress = "https://localhost/";

            ArrangeInputs($"connect {rootAddress}",
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out IPreferences preferences,
                          fileContents: "");

            TelemetryCollector telemetry = new TelemetryCollector();

            ConnectCommand connectCommand = new ConnectCommand(preferences, telemetry);

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.Single(telemetry.Telemetry);
            TelemetryCollector.CollectedTelemetry collectedTelemetry = telemetry.Telemetry[0];
            Assert.Equal("connect", collectedTelemetry.EventName, ignoreCase: true);
            Assert.Equal("True", collectedTelemetry.Properties["RootSpecified"]);
            Assert.Equal("False", collectedTelemetry.Properties["BaseSpecified"]);
            Assert.Equal("False", collectedTelemetry.Properties["OpenApiSpecified"]);
            Assert.Equal("False", collectedTelemetry.Properties["OpenApiFound"]);
        }
Esempio n. 3
0
        public async Task ExecuteAsync_WithNoRootAndRelativeSwagger_ShowsError()
        {
            ArrangeInputs("connect --swagger /v1/swagger.json",
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out IPreferences preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences);

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.Equal(Resources.Strings.ConnectCommand_Error_NoRootNoAbsoluteSwagger, shellState.ErrorMessage);
        }
Esempio n. 4
0
        public async Task ExecuteAsync_WithInvalidRoot_ShowsError()
        {
            ArrangeInputs("connect example.com",
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out IPreferences preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences);

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.Equal(Resources.Strings.ConnectCommand_Error_RootAddressNotValid, shellState.ErrorMessage);
        }
Esempio n. 5
0
        public async Task ExecuteAsync_WithNothingSpecified_ShowsError()
        {
            ArrangeInputs("connect",
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out IPreferences preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences);

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.Equal(Resources.Strings.ConnectCommand_Error_NothingSpecified, shellState.ErrorMessage);
        }
Esempio n. 6
0
        public async Task ExecuteAsync_WithRootAndNoBase_SetsBaseToRoot()
        {
            string rootAddress = "https://localhost/";

            ArrangeInputs($"connect {rootAddress}",
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out IPreferences preferences,
                          fileContents: "");

            ConnectCommand connectCommand = new ConnectCommand(preferences);

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.Equal(rootAddress, httpState.BaseAddress?.ToString());
        }
Esempio n. 7
0
        public async Task ExecuteAsync_WithRootAndSwaggerWithBase_SetsBaseToSwaggerBase()
        {
            string rootAddress    = "https://localhost/";
            string baseAddress    = "https://localhost/v2/";
            string swaggerAddress = "https://localhost/v2/swagger.json";
            string swaggerContent = @"{
  ""openapi"": ""3.0.0"",
  ""info"": {
    ""title"": ""OpenAPI v3 Spec"",
    ""version"": ""v1""
  },
  ""servers"": [
    {
      ""url"": """ + baseAddress + @""",
      ""description"": ""First Server Address""
    }
  ],
  ""paths"": {
    ""/pets"": {
    }
  }
}";

            ArrangeInputs(commandText: $"connect {rootAddress} --swagger {swaggerAddress}",
                          baseAddress: null,
                          path: null,
                          urlsWithResponse: new Dictionary <string, string>()
            {
                { swaggerAddress, swaggerContent }
            },
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out _,
                          out IPreferences preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences, new NullTelemetry());

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.NotNull(httpState.BaseAddress);
            Assert.Equal(baseAddress, httpState.BaseAddress.ToString());
            Assert.NotNull(httpState.SwaggerEndpoint);
            Assert.Equal(swaggerAddress, httpState.SwaggerEndpoint.ToString());
            Assert.NotNull(httpState.ApiDefinition);
        }
Esempio n. 8
0
        public async Task ExecuteAsync_RootWithSwaggerSuffixAndOverride_DoesNotFixBase()
        {
            string rootAddress            = "https://*****:*****@"{
  ""openapi"": ""3.0.0"",
  ""info"": {
    ""title"": ""OpenAPI v3 Spec"",
    ""version"": ""v1""
  },
  ""paths"": {
    ""/WeatherForecast"": {
    }
  }
}";

            MockedFileSystem             fileSystem = new MockedFileSystem();
            UserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();
            IPreferences preferences = new UserFolderPreferences(fileSystem,
                                                                 userProfileDirectoryProvider,
                                                                 new Dictionary <string, string> {
                { WellKnownPreference.ConnectCommandSkipRootFix, "true" }
            });

            ArrangeInputsWithOptional(commandText: $"connect {rootAddress}",
                                      baseAddress: null,
                                      path: null,
                                      urlsWithResponse: new Dictionary <string, string>()
            {
                { expectedSwaggerAddress, swaggerContent }
            },
                                      out MockedShellState shellState,
                                      out HttpState httpState,
                                      out ICoreParseResult parseResult,
                                      ref fileSystem,
                                      ref preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences, new NullTelemetry());

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.Contains(string.Format(Resources.Strings.ConnectCommand_Status_Base, expectedBaseAddress), shellState.Output, StringComparer.Ordinal);
            Assert.Contains(string.Format(Resources.Strings.ConnectCommand_Status_Swagger, expectedSwaggerAddress), shellState.Output, StringComparer.Ordinal);
        }
Esempio n. 9
0
        public async Task ExecuteAsync_SwaggerOnlyWithBase_ShowsBaseAndSwaggerResult()
        {
            string baseAddress    = "https://localhost/v2/";
            string swaggerAddress = "https://localhost/v2/swagger.json";
            string swaggerContent = @"{
  ""openapi"": ""3.0.0"",
  ""info"": {
    ""title"": ""OpenAPI v3 Spec"",
    ""version"": ""v1""
  },
  ""servers"": [
    {
      ""url"": """ + baseAddress + @""",
      ""description"": ""First Server Address""
    }
  ],
  ""paths"": {
    ""/pets"": {
    }
  }
}";

            ArrangeInputs(commandText: $"connect --swagger {swaggerAddress}",
                          baseAddress: null,
                          path: null,
                          urlsWithResponse: new Dictionary <string, string>()
            {
                { swaggerAddress, swaggerContent }
            },
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out _,
                          out IPreferences preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences, new NullTelemetry());

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.NotNull(httpState.BaseAddress);
            Assert.Contains(string.Format(Resources.Strings.ConnectCommand_Status_Base, httpState.BaseAddress), shellState.Output, StringComparer.Ordinal);
            Assert.NotNull(httpState.SwaggerEndpoint);
            Assert.Contains(string.Format(Resources.Strings.ConnectCommand_Status_Swagger, httpState.SwaggerEndpoint), shellState.Output, StringComparer.Ordinal);
        }
Esempio n. 10
0
        public async Task ExecuteAsync_SwaggerOnlyWithoutVerbose_OutputContainsAttempts()
        {
            string openApiDescriptionUrl = "https://localhost/v2/swagger.json";

            ArrangeInputs(commandText: $"connect --openapi {openApiDescriptionUrl}",
                          baseAddress: null,
                          path: null,
                          urlsWithResponse: new Dictionary <string, string>(),
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out _,
                          out IPreferences preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences, new NullTelemetry());

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.Contains(Resources.Strings.ApiConnection_Logging_Parsing + Resources.Strings.ApiConnection_Logging_Failed, shellState.Output, StringComparer.Ordinal);
        }
Esempio n. 11
0
        public async Task ExecuteAsync_WithJustBase_FindsSwaggerSetsDefinition()
        {
            string baseAddress    = "https://localhost/";
            string swaggerAddress = "https://localhost/swagger.json";
            string swaggerContent = @"{
  ""swagger"": ""2.0"",
  ""info"": {
    ""title"": ""OpenAPI v2 Spec"",
    ""version"": ""v1""
  },
  ""paths"": {
    ""/api/Values"": {
      ""post"": {
      }
    }
  }
}";

            ArrangeInputs(commandText: $"connect --base {baseAddress}",
                          baseAddress: null,
                          path: null,
                          urlsWithResponse: new Dictionary <string, string>()
            {
                { swaggerAddress, swaggerContent }
            },
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out _,
                          out IPreferences preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences, new NullTelemetry());

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.NotNull(httpState.BaseAddress);
            Assert.Equal(baseAddress, httpState.BaseAddress.ToString());
            Assert.NotNull(httpState.SwaggerEndpoint);
            Assert.Equal(swaggerAddress, httpState.SwaggerEndpoint.ToString());
            Assert.NotNull(httpState.ApiDefinition);
        }
Esempio n. 12
0
        public async Task ExecuteAsync_RootOnlyNoSwaggerFound_ShowsWarning()
        {
            string rootAddress = "https://localhost/";

            ArrangeInputs(commandText: $"connect {rootAddress}",
                          baseAddress: null,
                          path: null,
                          urlsWithResponse: null,
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out _,
                          out IPreferences preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences);

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.Null(httpState.SwaggerEndpoint);
            Assert.Contains(Resources.Strings.ConnectCommand_Status_NoSwagger, shellState.Output, StringComparer.Ordinal);
        }
Esempio n. 13
0
        public async Task ExecuteAsync_RootWithSwaggerSuffix_FixesBase()
        {
            string rootAddress            = "https://*****:*****@"{
  ""openapi"": ""3.0.0"",
  ""info"": {
    ""title"": ""OpenAPI v3 Spec"",
    ""version"": ""v1""
  },
  ""paths"": {
    ""/WeatherForecast"": {
    }
  }
}";

            ArrangeInputs(commandText: $"connect {rootAddress}",
                          baseAddress: null,
                          path: null,
                          urlsWithResponse: new Dictionary <string, string>()
            {
                { expectedSwaggerAddress, swaggerContent }
            },
                          out MockedShellState shellState,
                          out HttpState httpState,
                          out ICoreParseResult parseResult,
                          out _,
                          out IPreferences preferences);

            ConnectCommand connectCommand = new ConnectCommand(preferences, new NullTelemetry());

            await connectCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            Assert.Contains(string.Format(Resources.Strings.ConnectCommand_Status_Base, expectedBaseAddress), shellState.Output, StringComparer.Ordinal);
            Assert.Contains(string.Format(Resources.Strings.ConnectCommand_Status_Swagger, expectedSwaggerAddress), shellState.Output, StringComparer.Ordinal);
        }