public IEnumerable <string> Suggest(IShellState shellState, HttpState programState, ICoreParseResult parseResult) { if (parseResult.Sections.Count == 0) { return(new[] { Name }); } if (parseResult.Sections.Count > 0 && parseResult.SelectedSection == 0 && Name.StartsWith(parseResult.Sections[0].Substring(0, parseResult.CaretPositionWithinSelectedSection), StringComparison.OrdinalIgnoreCase)) { return(new[] { Name }); } if (string.Equals(Name, parseResult.Sections[0], StringComparison.OrdinalIgnoreCase) && parseResult.SelectedSection == 1 && (parseResult.Sections.Count < 2 || SubCommand.StartsWith(parseResult.Sections[1].Substring(0, parseResult.CaretPositionWithinSelectedSection), StringComparison.OrdinalIgnoreCase))) { return(new[] { SubCommand }); } if (parseResult.Sections.Count > 2 && string.Equals(Name, parseResult.Sections[0], StringComparison.OrdinalIgnoreCase) && string.Equals(SubCommand, parseResult.Sections[1], StringComparison.OrdinalIgnoreCase) && parseResult.SelectedSection == 2) { string prefix = parseResult.Sections[2].Substring(0, parseResult.CaretPositionWithinSelectedSection); return(HeaderCompletion.GetCompletions(null, prefix)); } if (parseResult.Sections.Count > 3 && string.Equals(Name, parseResult.Sections[0], StringComparison.OrdinalIgnoreCase) && string.Equals(SubCommand, parseResult.Sections[1], StringComparison.OrdinalIgnoreCase) && parseResult.SelectedSection == 3) { string prefix = parseResult.Sections[3].Substring(0, parseResult.CaretPositionWithinSelectedSection); return(HeaderCompletion.GetValueCompletions(null, string.Empty, parseResult.Sections[2], prefix, programState)); } return(null); }
public void GetValueCompletions_NoMethod_ReturnsAll() { DirectoryStructure directoryStructure = new DirectoryStructure(null); RequestInfo requestInfo = new RequestInfo(); requestInfo.SetRequestBody("GET", "application/json", ""); requestInfo.SetRequestBody("GET", "text/plain", ""); requestInfo.SetRequestBody("PUT", "application/xml", ""); directoryStructure.RequestInfo = requestInfo; HttpState httpState = SetupHttpState(); httpState.BaseAddress = new Uri("https://localhost/"); ApiDefinition apiDefinition = new ApiDefinition(); apiDefinition.DirectoryStructure = directoryStructure; httpState.ApiDefinition = apiDefinition; IEnumerable <string> result = HeaderCompletion.GetValueCompletions(method: null, path: "", header: "Content-Type", "", httpState); Assert.Equal(3, result.Count()); Assert.Contains("application/json", result, StringComparer.OrdinalIgnoreCase); Assert.Contains("text/plain", result, StringComparer.OrdinalIgnoreCase); Assert.Contains("application/xml", result, StringComparer.OrdinalIgnoreCase); }
public void GetValueCompletions_NoMatch_ReturnsNull() { HttpState httpState = SetupHttpState(); IEnumerable <string> result = HeaderCompletion.GetValueCompletions(method: "GET", path: "", header: "Content-Type", "", httpState); Assert.Null(result); }
public void GetValueCompletions_NoHeaderMatch_ReturnsNull(string header) { HttpState httpState = SetupHttpState(); IEnumerable <string> result = HeaderCompletion.GetValueCompletions(method: null, path: null, header, prefix: null, httpState); Assert.Null(result); }
public void GetValueCompletions_OneMatch_ReturnsMatch() { DirectoryStructure directoryStructure = new DirectoryStructure(null); RequestInfo requestInfo = new RequestInfo(); requestInfo.SetRequestBody("GET", "application/json", ""); requestInfo.SetRequestBody("PUT", "application/xml", ""); directoryStructure.RequestInfo = requestInfo; HttpState httpState = SetupHttpState(); httpState.BaseAddress = new Uri("https://localhost/"); httpState.Structure = directoryStructure; IEnumerable <string> result = HeaderCompletion.GetValueCompletions(method: "GET", path: "", header: "Content-Type", "", httpState); Assert.Single(result); Assert.Contains("application/json", result, StringComparer.OrdinalIgnoreCase); }
protected override IEnumerable <string> GetOptionValueCompletions(IShellState shellState, HttpState programState, string optionId, DefaultCommandInput <ICoreParseResult> commandInput, ICoreParseResult parseResult, string normalizedCompletionText) { if (string.Equals(optionId, BodyFileOption, StringComparison.Ordinal) || string.Equals(optionId, ResponseFileOption, StringComparison.OrdinalIgnoreCase) || string.Equals(optionId, ResponseBodyFileOption, StringComparison.OrdinalIgnoreCase) || string.Equals(optionId, ResponseHeadersFileOption, StringComparison.OrdinalIgnoreCase)) { return(FileSystemCompletion.GetCompletions(normalizedCompletionText)); } if (string.Equals(optionId, HeaderOption, StringComparison.Ordinal)) { HashSet <string> alreadySpecifiedHeaders = new HashSet <string>(StringComparer.Ordinal); IReadOnlyList <InputElement> options = commandInput.Options[HeaderOption]; for (int i = 0; i < options.Count; ++i) { if (options[i] == commandInput.SelectedElement) { continue; } string elementText = options[i].Text; string existingHeaderName = elementText.Split(HeaderSeparatorChars)[0]; alreadySpecifiedHeaders.Add(existingHeaderName); } //Check to see if the selected element is in a header name or value int equalsIndex = normalizedCompletionText.IndexOfAny(HeaderSeparatorChars); string path = commandInput.Arguments.Count > 0 ? commandInput.Arguments[0].Text : string.Empty; if (equalsIndex < 0) { IEnumerable <string> headerNameOptions = HeaderCompletion.GetCompletions(alreadySpecifiedHeaders, normalizedCompletionText); List <string> allSuggestions = new List <string>(); foreach (string suggestion in headerNameOptions.Select(x => x)) { allSuggestions.Add(suggestion + ":"); IEnumerable <string> suggestions = HeaderCompletion.GetValueCompletions(Verb, path, suggestion, string.Empty, programState); if (suggestions != null) { foreach (string valueSuggestion in suggestions) { allSuggestions.Add(suggestion + ":" + valueSuggestion); } } } return(allSuggestions); } else { //Didn't exit from the header name check, so must be a value string headerName = normalizedCompletionText.Substring(0, equalsIndex); IEnumerable <string> suggestions = HeaderCompletion.GetValueCompletions(Verb, path, headerName, normalizedCompletionText.Substring(equalsIndex + 1), programState); if (suggestions == null) { return(null); } return(suggestions.Select(x => normalizedCompletionText.Substring(0, equalsIndex + 1) + x)); } } return(null); }
public void GetValueCompletions_NoHeaderMatch_ReturnsNull(string header) { IEnumerable <string> result = HeaderCompletion.GetValueCompletions(method: null, path: null, header, prefix: null, programState: null); Assert.Null(result); }