示例#1
0
        public async Task ExecuteAsync_SetCommandNoValue_SetToDefault()
        {
            IFileSystem fileSystem = new MockedFileSystem();
            IUserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();
            UserFolderPreferences         preferences = new UserFolderPreferences(fileSystem, userProfileDirectoryProvider, TestDefaultPreferences.GetDefaultPreferences());
            HttpClient       httpClient = new HttpClient();
            HttpState        httpState  = new HttpState(fileSystem, preferences, httpClient);
            MockedShellState shellState = new MockedShellState();
            PrefCommand      command    = new PrefCommand(preferences);

            // First, set it to something other than the default and make sure that works.
            string           firstCommandExpectedValue = "BoldMagenta";
            string           firstCommandText          = $"pref set {WellKnownPreference.ProtocolColor} {firstCommandExpectedValue}";
            ICoreParseResult firstParseResult          = CoreParseResultHelper.Create(firstCommandText);

            await command.ExecuteAsync(shellState, httpState, firstParseResult, CancellationToken.None);

            Assert.Empty(shellState.Output);
            Assert.Equal(firstCommandExpectedValue, preferences.CurrentPreferences[WellKnownPreference.ProtocolColor]);

            // Then, set it to nothing and make sure it goes back to the default
            string           secondCommandText = $"pref set {WellKnownPreference.ProtocolColor}";
            ICoreParseResult secondParseResult = CoreParseResultHelper.Create(secondCommandText);

            await command.ExecuteAsync(shellState, httpState, secondParseResult, CancellationToken.None);

            Assert.Empty(shellState.Output);
            Assert.Equal(preferences.DefaultPreferences[WellKnownPreference.ProtocolColor], preferences.CurrentPreferences[WellKnownPreference.ProtocolColor]);
        }
示例#2
0
        private static HttpState SetupHttpState(string preferencesFileContent = null)
        {
            UserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();
            IFileSystem fileSystem;

            if (preferencesFileContent != null)
            {
                fileSystem = new MockedFileSystem();
            }
            else
            {
                fileSystem = new FileSystemStub();
            }
            UserFolderPreferences preferences = new UserFolderPreferences(fileSystem, userProfileDirectoryProvider, null);

            if (preferencesFileContent != null)
            {
                ((MockedFileSystem)fileSystem).AddFile(preferences.PreferencesFilePath, preferencesFileContent);
            }

            HttpClient client = new HttpClient();
            HttpState  state  = new HttpState(fileSystem, preferences, client);

            return(state);
        }
示例#3
0
        private static void ComposeDependencies(IConsoleManager consoleManager, out HttpState state, out Shell shell, out IPreferences preferences)
        {
            IFileSystem fileSystem = new RealFileSystem();
            IUserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();

            preferences = new Preferences.UserFolderPreferences(fileSystem, userProfileDirectoryProvider, CreateDefaultPreferences());
            HttpClient httpClient = new HttpClient();

            state = new HttpState(fileSystem, preferences, httpClient);

            var dispatcher = DefaultCommandDispatcher.Create(state.GetPrompt, state);

            dispatcher.AddCommand(new ChangeDirectoryCommand());
            dispatcher.AddCommand(new ClearCommand());
            dispatcher.AddCommand(new DeleteCommand(fileSystem, preferences));
            dispatcher.AddCommand(new EchoCommand());
            dispatcher.AddCommand(new ExitCommand());
            dispatcher.AddCommand(new HeadCommand(fileSystem, preferences));
            dispatcher.AddCommand(new HelpCommand(preferences));
            dispatcher.AddCommand(new GetCommand(fileSystem, preferences));
            dispatcher.AddCommand(new ListCommand(preferences));
            dispatcher.AddCommand(new OptionsCommand(fileSystem, preferences));
            dispatcher.AddCommand(new PatchCommand(fileSystem, preferences));
            dispatcher.AddCommand(new PrefCommand(preferences));
            dispatcher.AddCommand(new PostCommand(fileSystem, preferences));
            dispatcher.AddCommand(new PutCommand(fileSystem, preferences));
            dispatcher.AddCommand(new RunCommand(fileSystem));
            dispatcher.AddCommand(new SetBaseCommand());
            dispatcher.AddCommand(new SetHeaderCommand());
            dispatcher.AddCommand(new SetSwaggerCommand());
            dispatcher.AddCommand(new UICommand(new UriLauncher()));

            shell = new Shell(dispatcher, consoleManager: consoleManager);
        }
        public void WritePreferences_ChangeNonDefaultToDefault_RemovesDefaultValue()
        {
            string originalValue = "BoldMagenta";
            string defaultValue  = "Red";
            IDictionary <string, string> defaultPreferences = new Dictionary <string, string> {
                { WellKnownPreference.ProtocolColor, defaultValue }
            };

            MockedFileSystem fileSystem = new MockedFileSystem();
            IUserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();
            UserFolderPreferences         preferences = new UserFolderPreferences(fileSystem, userProfileDirectoryProvider, defaultPreferences);

            // Create a file with a non-default value, read it from the file system and
            // validate that it was read correctly
            fileSystem.AddFile(preferences.PreferencesFilePath, $"{WellKnownPreference.ProtocolColor}={originalValue}");

            Assert.Equal(originalValue, preferences.CurrentPreferences[WellKnownPreference.ProtocolColor]);

            // Now change it to the default value, write it back to the file system and
            // validate that it was removed from the file
            bool succeeded = preferences.SetValue(WellKnownPreference.ProtocolColor, defaultPreferences[WellKnownPreference.ProtocolColor]);

            Assert.True(succeeded);
            Assert.Equal(string.Empty, fileSystem.ReadFile(preferences.PreferencesFilePath));
        }
        private void SetupPreferences(out UserFolderPreferences preferences, out MockedFileSystem fileSystem)
        {
            fileSystem = new MockedFileSystem();
            IUserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();

            preferences = new UserFolderPreferences(fileSystem, userProfileDirectoryProvider, TestDefaultPreferences.GetDefaultPreferences());
        }
示例#6
0
        private static HttpState SetupHttpState()
        {
            IFileSystem fileSystem = new FileSystemStub();
            IUserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();
            IPreferences preferences = new UserFolderPreferences(fileSystem, userProfileDirectoryProvider, null);
            HttpClient   httpClient  = new HttpClient();

            return(new HttpState(fileSystem, preferences, httpClient));
        }
        private void Setup(string commandText, out MockedShellState mockedShellState, out HttpState httpState, out ICoreParseResult parseResult)
        {
            mockedShellState = new MockedShellState();
            IFileSystem fileSystem = new RealFileSystem();
            IUserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();
            IPreferences preferences = new UserFolderPreferences(fileSystem, userProfileDirectoryProvider, null);

            parseResult = CoreParseResultHelper.Create(commandText);
            HttpClient httpClient = new HttpClient();

            httpState = new HttpState(preferences, httpClient);
        }
示例#8
0
        private static void Arrange(string commandText, out HttpState httpState, out MockedShellState shellState, out ICoreParseResult parseResult, out PrefCommand command, out UserFolderPreferences preferences)
        {
            IFileSystem fileSystem = new MockedFileSystem();
            IUserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();

            preferences = new UserFolderPreferences(fileSystem, userProfileDirectoryProvider, TestDefaultPreferences.GetDefaultPreferences());
            HttpClient httpClient = new HttpClient();

            httpState   = new HttpState(fileSystem, preferences, httpClient);
            shellState  = new MockedShellState();
            parseResult = CoreParseResultHelper.Create(commandText);
            command     = new PrefCommand(preferences);
        }
示例#9
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);
        }
        private static HttpState SetupHttpState()
        {
            IFileSystem fileSystem = new FileSystemStub();
            IUserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();
            IPreferences preferences = new UserFolderPreferences(fileSystem, userProfileDirectoryProvider, null);
            HttpClient   httpClient  = new HttpClient();

            HttpState httpState = new HttpState(fileSystem, preferences, httpClient);

            DirectoryStructure structure = new DirectoryStructure(null);
            DirectoryStructure child1    = structure.DeclareDirectory("child1");

            structure.DeclareDirectory("child2");
            child1.DeclareDirectory("grandchild1");
            child1.DeclareDirectory("grandchild2");

            httpState.Structure = structure;

            return(httpState);
        }
示例#11
0
        public async Task ExecuteAsync_WithEditorDoesNotExist_VerifyResponse()
        {
            // Arrange
            string editorPath  = "FileThatDoesNotExist.exe";
            string commandText = "POST https://localhost/";

            MockedShellState shellState = new MockedShellState();
            IFileSystem      fileSystem = new MockedFileSystem();
            IUserProfileDirectoryProvider userProfileDirectoryProvider = new UserProfileDirectoryProvider();
            IPreferences     preferences = new UserFolderPreferences(fileSystem, userProfileDirectoryProvider, null);
            ICoreParseResult parseResult = CoreParseResultHelper.Create(commandText);
            HttpClient       httpClient  = new HttpClient();
            HttpState        httpState   = new HttpState(fileSystem, preferences, httpClient);
            PostCommand      postCommand = new PostCommand(fileSystem, preferences);

            preferences.SetValue(WellKnownPreference.DefaultEditorCommand, editorPath);

            // Act
            await postCommand.ExecuteAsync(shellState, httpState, parseResult, CancellationToken.None);

            // Execute
            Assert.Empty(shellState.Output);
            Assert.Contains(string.Format(Strings.BaseHttpCommand_Error_DefaultEditorDoesNotExist, editorPath), shellState.ErrorMessage);
        }