Ejemplo n.º 1
0
        public void SearchForJokesTest()
        {
            // mock the handler
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock
            .Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(
                    "{" +
                    "\"results\": " +
                    "[" +
                    "{" +
                    "\"id\": \"M7wPC5wPKBd\"," +
                    "\"joke\": \"Did you hear the one about the guy with the broken hearing aid? Neither did he.\"" +
                    "}," +
                    "{" +
                    "\"id\": \"MRZ0LJtHQCd\"," +
                    "\"joke\": \"What do you call a fly without wings? A walk.\"" +
                    "}," +
                    "{" +
                    "\"id\": \"usrcaMuszd\"," +
                    "\"joke\": \"What's the worst thing about ancient history class? The teachers tend to Babylon.\"" +
                    "}" +
                    "]" +
                    "}")
            })
            .Verifiable();

            // use real http client with mocked handler
            var factory = handlerMock.CreateClientFactory();

            Mock.Get(factory).Setup(x => x.CreateClient("icanhazdadjokeSearch"))
            .Returns(() =>
            {
                var client         = handlerMock.CreateClient();
                client.BaseAddress = new Uri("https://icanhazdadjoke.com/search");
                client.DefaultRequestHeaders.Add("Accept", "application/json");
                client.DefaultRequestHeaders.Add("User-Agent", "icanhazdadjoke API Search Test");
                return(client);
            });

            var jokes = new GetJokes().Search(factory.CreateClient("icanhazdadjokeSearch"), "dad").Result;

            Assert.True(jokes.Results.Count() == 3);

            //var jokes = new GetJokes().Search(factory.CreateClient("icanhazdadjokeSearch"), "the").Result;

            //Assert.True(jokes.Results.Count() == 2);
        }
Ejemplo n.º 2
0
        public void RandomJokeTest()
        {
            // mock the handler
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Strict);

            handlerMock
            .Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ReturnsAsync(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(
                    "{" +
                    "\"id\": \"R7UfaahVfFd\"," +
                    "\"joke\": \"My dog used to chase people on a bike a lot. It got so bad I had to take his bike away.\"," +
                    "\"status\": 200" +
                    "}")
            })
            .Verifiable();

            // use real http client with mocked handler
            var factory = handlerMock.CreateClientFactory();

            Mock.Get(factory).Setup(x => x.CreateClient("icanhazdadjokeRandom"))
            .Returns(() =>
            {
                var client         = handlerMock.CreateClient();
                client.BaseAddress = new Uri("https://icanhazdadjoke.com");
                client.DefaultRequestHeaders.Add("Accept", "application/json");
                client.DefaultRequestHeaders.Add("User-Agent", "icanhazdadjoke API Random Test");
                return(client);
            });

            var joke = new GetJokes().Random(factory.CreateClient("icanhazdadjokeRandom")).Result;

            Assert.True(joke.Id == "R7UfaahVfFd");
        }
Ejemplo n.º 3
0
 public DadJokeController()
 {
     _getJokes = new GetJokes();
 }