Example #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]);
        }
Example #2
0
        protected void ArrangeInputs(string commandText,
                                     string baseAddress,
                                     string path,
                                     IDictionary <string, string> urlsWithResponse,
                                     out MockedShellState shellState,
                                     out HttpState httpState,
                                     out ICoreParseResult parseResult,
                                     out MockedFileSystem fileSystem,
                                     out IPreferences preferences,
                                     string header         = "",
                                     bool readBodyFromFile = false,
                                     string fileContents   = "",
                                     string contentType    = "")
        {
            parseResult = CoreParseResultHelper.Create(commandText);
            shellState  = new MockedShellState();

            httpState = GetHttpState(out fileSystem,
                                     out preferences,
                                     baseAddress,
                                     header,
                                     path,
                                     urlsWithResponse,
                                     readBodyFromFile,
                                     fileContents,
                                     contentType);
        }
        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);
        }
Example #4
0
        public void CanHandle(string commandText, bool?expected)
        {
            HttpState        httpState   = GetHttpState(out _, out _);
            ICoreParseResult parseResult = CreateCoreParseResult(commandText);
            IShellState      shellState  = new MockedShellState();

            HelpCommand helpCommand = new HelpCommand();

            bool?result = helpCommand.CanHandle(shellState, httpState, parseResult);

            Assert.Equal(expected, result);
        }
Example #5
0
        protected async Task VerifyHeaders(string commandText, string baseAddress, string path, int expectedResponseLines, string expectedHeader)
        {
            MockedShellState shellState = new MockedShellState();

            HttpState httpState = GetHttpState(baseAddress, path);

            ICoreParseResult parseResult = CoreParseResultHelper.Create(commandText);

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

            Assert.Equal(expectedResponseLines, shellState.Output.Count);
            Assert.Equal(expectedHeader, shellState.Output[expectedResponseLines - 2]);
        }
Example #6
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);
        }
Example #7
0
 private void ArrangeInputs(string commandText, out MockedShellState shellState, out HttpState httpState, out ICoreParseResult parseResult, out IPreferences preferences, string fileContents = null)
 {
     ArrangeInputs(commandText,
                   baseAddress: null,
                   path: null,
                   urlsWithResponse: null,
                   out shellState,
                   out httpState,
                   out parseResult,
                   out _,
                   out preferences,
                   readBodyFromFile: fileContents is object,
                   fileContents: fileContents);
 }
Example #8
0
        private async Task <IDirectoryStructure> GetDirectoryStructure(string response, string parseResultSections, string baseAddress)
        {
            MockedShellState             shellState       = new MockedShellState();
            IDictionary <string, string> urlsWithResponse = new Dictionary <string, string>();

            urlsWithResponse.Add(baseAddress, response);
            HttpState         httpState         = GetHttpState(out _, out _, urlsWithResponse: urlsWithResponse);
            ICoreParseResult  parseResult       = CoreParseResultHelper.Create(parseResultSections);
            SetSwaggerCommand setSwaggerCommand = new SetSwaggerCommand();

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

            return(httpState.Structure);
        }
Example #9
0
        protected async Task VerifyErrorMessage(string commandText, string baseAddress, string path, string expectedErrorMessage)
        {
            HttpState httpState = GetHttpState(baseAddress, path);

            expectedErrorMessage = expectedErrorMessage.SetColor(httpState.ErrorColor);

            MockedShellState shellState = new MockedShellState();

            ICoreParseResult parseResult = CoreParseResultHelper.Create(commandText);

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

            Assert.Equal(expectedErrorMessage, shellState.ErrorMessage);
        }
Example #10
0
        public async Task ExecuteAsync_WithHttpStateBaseAddressSetToNull_WritesToConsoleManagerError()
        {
            MockedShellState shellState  = new MockedShellState();
            ICoreParseResult parseResult = CoreParseResultHelper.Create("ui");
            HttpState        httpState   = GetHttpState(out _, out _);

            httpState.BaseAddress = null;

            Mock <IUriLauncher> mockLauncher = new Mock <IUriLauncher>();
            UICommand           uiCommand    = new UICommand(mockLauncher.Object);

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

            VerifyErrorMessageWasWrittenToConsoleManagerError(shellState);
        }
Example #11
0
        protected static void ArrangeInputs(string parseResultSections,
                                            out MockedShellState shellState,
                                            out HttpState httpState,
                                            out ICoreParseResult parseResult,
                                            string baseAddress     = "",
                                            int caretPosition      = -1,
                                            string responseContent = "")
        {
            parseResult = CoreParseResultHelper.Create(parseResultSections, caretPosition);
            shellState  = new MockedShellState();
            IDictionary <string, string> urlsWithResponse = new Dictionary <string, string>();

            urlsWithResponse.Add(baseAddress, responseContent);

            httpState = GetHttpState(out _, out _, urlsWithResponse: urlsWithResponse);
        }
Example #12
0
        public async Task ExecuteAsync_WithNoParameterAndAbsolutePreference_VerifyLaunchUriAsyncWasCalledOnce()
        {
            string                commandText = "ui";
            ICoreParseResult      parseResult = CoreParseResultHelper.Create(commandText);
            MockedShellState      shellState  = new MockedShellState();
            MockedFileSystem      fileSystem  = new MockedFileSystem();
            UserFolderPreferences preferences = new UserFolderPreferences(fileSystem, new UserProfileDirectoryProvider(), null);

            preferences.SetValue(WellKnownPreference.SwaggerUIEndpoint, "https://localhost:12345/mySwaggerPath");
            HttpState httpState = new HttpState(fileSystem, preferences, new HttpClient());

            httpState.BaseAddress = new Uri("https://localhost:44366", UriKind.Absolute);

            Mock <IUriLauncher> mockLauncher = new Mock <IUriLauncher>();
            UICommand           uiCommand    = new UICommand(mockLauncher.Object, preferences);

            mockLauncher.Setup(s => s.LaunchUriAsync(It.IsAny <Uri>()))
            .Returns(Task.CompletedTask);

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

            mockLauncher.Verify(l => l.LaunchUriAsync(It.Is <Uri>(u => u.AbsoluteUri == "https://localhost:12345/mySwaggerPath")), Times.Once());
        }
Example #13
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);
        }
Example #14
0
 private static void Arrange(string commandText, out HttpState httpState, out MockedShellState shellState, out ICoreParseResult parseResult, out UserFolderPreferences preferences)
 {
     Arrange(commandText, out httpState, out shellState, out parseResult, out _, out preferences);
 }