/// <summary>
        /// Gets a collection of jokes by the search term provided. Supports multiple search terms, split by whitespaces.
        /// An OR operation is used by the API if more than one term is given.
        /// </summary>
        /// <param name="searchTerm">The term to search for jokes with.</param>
        /// <param name="limit">The maximum number of search results to return. Default is 30.</param>
        /// <returns>Collection of jokes that contain the search term provided.</returns>
        public async Task <JokeSearchResponse> GetBySearchTerm(string searchTerm, int limit = 30)
        {
            var responseMessage = await _httpClient.GetAsync(
                DadJokeServiceHelpers.GetSearchRequestUriWithQueryString(_endpointGetBySearchTerm, searchTerm, limit));

            responseMessage.EnsureSuccessStatusCode();

            string content = await responseMessage.Content.ReadAsStringAsync();

            var jokeSearchReponse = JsonConvert.DeserializeObject <JokeSearchResponse>(content);

            if (searchTerm != null)
            {
                // Account for cases where there are two or more terms in the search term
                string[] searchTermSplit = searchTerm.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                jokeSearchReponse.Results = DadJokeServiceHelpers.EmphasizeWithUppercase(jokeSearchReponse.Results, searchTermSplit);
            }

            jokeSearchReponse.ResultsBySize = DadJokeServiceHelpers.GroupByJokeSize(jokeSearchReponse.Results);

            return(jokeSearchReponse);
        }
Exemple #2
0
        public void EmphasizeWithUppercase_ZeroMatches_DoesNotEmphasize()
        {
            // Arrange
            string expected = "this is a bird joke";

            var inputJokeResponses = new JokeResponse[] {
                new JokeResponse()
                {
                    Joke = "this is a bird joke"
                }
            };

            var inputTermsToEmphasize = new string[] { "cat", "dog" };

            // Act
            string actual = DadJokeServiceHelpers
                            .EmphasizeWithUppercase(inputJokeResponses, inputTermsToEmphasize).
                            Single()
                            .Joke;

            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemple #3
0
        public void EmphasizeWithUppercase_MultipleSearchTermOneMatch_EmphasizesMatchingTermOnly()
        {
            // Arrange
            string expected = "this is a DOG joke";

            var inputJokeResponses = new JokeResponse[] {
                new JokeResponse()
                {
                    Joke = "this is a dog joke"
                }
            };

            var inputTermsToEmphasize = new string[] { "cat", "dog" };

            // Act
            string actual = DadJokeServiceHelpers
                            .EmphasizeWithUppercase(inputJokeResponses, inputTermsToEmphasize).
                            Single()
                            .Joke;

            // Assert
            Assert.AreEqual(expected, actual);
        }