Ejemplo n.º 1
0
        public void GetAnyJokesTest(int count)
        {
            var            jokeProvider = new ChuckNorrisHttpJokeProvider();
            JokeRequest    jr           = new JokeRequest(count);
            IList <string> results      = jokeProvider.GetJokes(jr);

            Assert.AreEqual(count, results.Count);
        }
Ejemplo n.º 2
0
        public void GetRenamedBothNamesJokesTest(int count)
        {
            var            jokeProvider = new ChuckNorrisHttpJokeProvider();
            JokeRequest    jr           = new JokeRequest("Bob", "Smith", "food", count);
            IList <string> results      = jokeProvider.GetJokes(jr);

            Assert.AreEqual(count, results.Count);
        }
Ejemplo n.º 3
0
        public void GetRenamedFirstNameOnlyJokesTest(int count)
        {
            var            jokeProvider = new ChuckNorrisHttpJokeProvider();
            JokeRequest    jr           = new JokeRequest("Bob", null, "history", count);
            IList <string> results      = jokeProvider.GetJokes(jr);

            Assert.AreEqual(count, results.Count);
        }
        public void GetBothNamesJokesTest(int count)
        {
            var options      = GetOptions();
            var jokeProvider = new JokeWebApiClient(options);
            var jr           = new JokeRequest("Thomas", "Crane", "food", count);
            var results      = jokeProvider.GetJokes(jr);

            Assert.AreEqual(count, results.Count);
        }
        public void GetJokesWithLastNameOnlyTest(int count)
        {
            var options      = GetOptions();
            var jokeProvider = new JokeWebApiClient(options);
            var jr           = new JokeRequest(null, "Crane", "food", count);
            var results      = jokeProvider.GetJokes(jr);

            Assert.AreEqual(count, results.Count);
        }
        public void GetJokesWithFirstNameOnlyTest(int count)
        {
            var options      = GetOptions();
            var jokeProvider = new JokeWebApiClient(options);
            var jr           = new JokeRequest("Thomas", null, "movie", count);
            var results      = jokeProvider.GetJokes(jr);

            Assert.AreEqual(count, results.Count);
        }
        public void GetAnyRandomJokesTest(int count)
        {
            var options      = GetOptions();
            var jokeProvider = new JokeWebApiClient(options);
            var jr           = new JokeRequest(count);
            var results      = jokeProvider.GetJokes(jr);

            Assert.AreEqual(count, results.Count);
        }
        public void GetJustCategoryDefaultNameJokesTest(int count)
        {
            var options      = GetOptions();
            var jokeProvider = new JokeWebApiClient(options);
            var jr           = new JokeRequest("animal", count);
            var results      = jokeProvider.GetJokes(jr);

            Assert.AreEqual(count, results.Count);
        }
Ejemplo n.º 9
0
        public override Task <JokeReply> CheckJokeRequest(JokeRequest request, ServerCallContext context)
        {
            request.Uri = _urlJoke;

            var response = _feed.GetRandomJokes(request);

            return(Task.FromResult(new JokeReply
            {
                Message = response    //[0]
            }));
        }
Ejemplo n.º 10
0
        public virtual IList <string> GetJokes(JokeRequest jokeRequest)
        {
            var    client = new ChuckNorrisHttpClient(new Uri(URL));
            string qs     = BuildQueryString(jokeRequest, client);

            List <string> jokes = new List <string>(jokeRequest.JokeCount); //Set that capacity like a boss

            for (int i = 0; i < jokeRequest.JokeCount; i++)
            {
                var response = client.GetStringAsync(new Uri($"/jokes/random{qs}", UriKind.Relative)).Result;

                var joke = JsonConvert.DeserializeObject <ChuckNorrisHttpResponseJoke>(response).value;
                jokes.Add(joke);
            }

            return(jokes);
        }
Ejemplo n.º 11
0
        public void GetRandomJokes()// (string[] expectedResult)
        {
            var request = new JokeRequest();

            request.FirstName = "Maggie";
            request.LastName  = "Chen";
            request.Category  = "animal"; // "aminal";
            request.Uri       = "https://api.chucknorris.io";
            var feed = new JsonFeed();    // ("https://api.chucknorris.io");//, 1);
            var res  = feed.GetRandomJokes(request);

            Assert.IsNotNull(res);

            //mockFeed = new Mock<IJsonFeed>(MockBehavior.Strict);
            //mockFeed.Setup(p => p.GetRandomJokes(request)).Returns(expectedResult);

            //systemUnderTest = new JsonFeed();
            //var result = systemUnderTest.GetRandomJokes(request);
            //Assert.IsNotNull(result);
        }
Ejemplo n.º 12
0
        public /*string[]*/ string GetRandomJokes(JokeRequest request)
        {
            HttpClient _client = new HttpClient();

            _client.BaseAddress = new Uri(request.Uri);

            string url = "jokes/random";

            if (request.Category != null)
            {
                if (url.Contains('?'))
                {
                    url += "&";
                }
                else
                {
                    url += "?";
                }
                url += "category=";
                url += request.Category;
            }
            string result = string.Empty;

            for (int i = 1; i < request.Number + 1; i++)
            {
                string joke = Task.FromResult(_client.GetStringAsync(url).Result).Result;

                if (request.FirstName != null && request.LastName != null)
                {
                    int    index      = joke.IndexOf("Chuck Norris");
                    string firstPart  = joke.Substring(0, index);
                    string secondPart = joke.Substring(0 + index + "Chuck Norris".Length, joke.Length - (index + "Chuck Norris".Length));
                    joke = firstPart + " " + request.FirstName + " " + request.LastName + secondPart;
                }

                //  return new string[] { JsonConvert.DeserializeObject<dynamic>(joke).value };
                result += string.Concat(i.ToString(), ".", JsonConvert.DeserializeObject <dynamic>(joke).value);
            }

            return(result.ToString());
        }
Ejemplo n.º 13
0
        public virtual IList <string> GetJokes(JokeRequest jokeRequest)
        {
            using (var client = new HttpClient().Build(new Uri(_options.Value.ChucknorrisUrl)))
            {
                var queryString = new QueryBuilder()
                                  .AppendToQuery("name", jokeRequest.Name)
                                  .AppendToQuery("category", jokeRequest.Category)
                                  .Build();

                var result = new List <string>(jokeRequest.JokeCount); //Set that capacity like a boss

                for (var i = 0; i < jokeRequest.JokeCount; i++)
                {
                    var jokes = client.GetAsArray <JokeDto>(new Uri($"/jokes/random{queryString}",
                                                                    UriKind.Relative));
                    result.AddRange(jokes.Select(_ => _.value));
                }

                return(result);
            }
        }
Ejemplo n.º 14
0
        private string BuildQueryString(JokeRequest jokeRequest, JokeCompany.Utilities.Http.HttpClient client)
        {
            if (string.IsNullOrEmpty(jokeRequest.FirstName) && !string.IsNullOrEmpty(jokeRequest.LastName)) //We can use LastName only
            {
                client.AppendToQuery("name", jokeRequest.LastName);
            }
            else if (string.IsNullOrEmpty(jokeRequest.LastName) && !string.IsNullOrEmpty(jokeRequest.FirstName)) //We can use FirstName only
            {
                client.AppendToQuery("name", jokeRequest.FirstName);
            }
            else if (!string.IsNullOrEmpty(jokeRequest.FirstName) && !string.IsNullOrEmpty(jokeRequest.LastName)) //Use Both!
            {
                client.AppendToQuery("name", $"{jokeRequest.FirstName} {jokeRequest.LastName}");
            }

            if (!string.IsNullOrEmpty(jokeRequest.Category))
            {
                client.AppendToQuery("category", jokeRequest.Category);
            }

            return(client.QueryString);
        }
Ejemplo n.º 15
0
 internal IList <string> GetRandomJokes(JokeRequest jokeRequest)
 {
     return(m_jokeFactory.JokeProvider.GetJokes(jokeRequest));
 }
Ejemplo n.º 16
0
        static async Task Main(string[] args)
        {
            while (true)
            {
                printer.Value("Press ? to get instructions.").ToString();
                GetEnteredKey(Console.ReadKey());
                //if (Console.ReadLine() == "?")
                if (key == '?')
                {
                    while (true)
                    {
                        // The port number(5001) must match the port of the gRPC server.
                        var channel = GrpcChannel.ForAddress("https://localhost:5001");
                        var client  = new JokeCheck.JokeCheckClient(channel);

                        printer.Value("Press r to get random jokes").ToString();
                        GetEnteredKey(Console.ReadKey());

                        if (key == 'r')
                        {
                            var jokeRequest = new JokeRequest();

                            printer.Value("Want to specify a category? y/n").ToString();
                            GetEnteredKey(Console.ReadKey());
                            if (key == 'y')
                            {
                                printer.Value("Please select the category by typing the 1-9 or a-z inside[] following each category.").ToString();
                                printer.Value("'animal[1]','career[2],'celebrity[3]','dev[4]','explicit[5]','fashion[6]','food[7]','history[8]','money[9]','movie[a]','music[b]','political[c]','religion[d]','science[e]','sport[f]','travel[g]'").ToString();

                                jokeRequest.Category = MapCategoryKey(Console.ReadKey());
                            }

                            printer.Value("Want to use a random name? y/n").ToString();
                            GetEnteredKey(Console.ReadKey());
                            if (key == 'y')
                            {
                                var nameRequest = new NameRequest();

                                var nameReply = await client.CheckJokeNameRequestAsync(nameRequest);

                                jokeRequest.FirstName = nameReply.FirstName;
                                jokeRequest.LastName  = nameReply.LastName;

                                Console.WriteLine($"Random name is {jokeRequest.FirstName} {jokeRequest.LastName} :-)");
                            }

                            printer.Value("How many jokes do you want? (1-9)").ToString();
                            //GetEnteredKey(Console.ReadLine());
                            int n = Int32.Parse(Console.ReadLine());

                            jokeRequest.Number = n;

                            var reply   = new JokeReply();
                            var success = true;
                            try
                            {
                                reply = await client.CheckJokeRequestAsync(jokeRequest);
                            }
                            catch (Exception ex)
                            {
                                //To do: logger ex

                                reply.Message = "Joke service is not available at this moment. Please come back later.";
                                success       = false;
                            }
                            Console.WriteLine($"Hello {jokeRequest.FirstName} {jokeRequest.LastName}!");
                            if (success)
                            {
                                Console.WriteLine($"Enjoy the joke for {jokeRequest.Category.ToUpper()} category: {reply.Message}");
                            }
                            else
                            {
                                Console.WriteLine($"Sorry, {reply.Message}");
                            }
                        }

                        Console.WriteLine("Press any key to continue new joke... Press ctrl+c to exit...");
                        Console.ReadKey();
                    }
                }
            }
        }