public void SearchByExpression_ShouldReturnResponse_WhenDataIsAvailable()
        {
            var term = "java";

            var _handler       = new Mock <IHttpHandler>();
            var _configuration = IntialMockConfiguration();
            var _gateway       = new BingSearchEngineGateway(_configuration.Object, _handler.Object);

            var response = new HttpResponseMessage
            {
                StatusCode = System.Net.HttpStatusCode.OK,
                Content    = new StringContent("{\n \"_Type\": \"CustomSearch\",\n \"WebPages\":{\n \"TotalEstimatedMatches\":1000,\n \"WebSearchUrl\":\"SomeUrl\" \n } \n}")
            };

            var expected = new BingResponseDto
            {
                _Type    = "CustomSearch",
                WebPages = new WebPagesDto
                {
                    TotalEstimatedMatches = 1000,
                    WebSearchUrl          = "SomeUrl"
                }
            };

            _handler.Setup(s => s.GetAsync(It.IsAny <string>())).Returns(Task.FromResult(response));

            var actual = _gateway.SearchByExpression(term);

            actual.Should().BeEquivalentTo(expected);
        }
Example #2
0
        public static SearchResult ToSearchResultModel(this BingResponseDto response, string term, string engine)
        {
            var bingResult = new SearchResult();

            bingResult.Result       = response.WebPages.TotalEstimatedMatches;
            bingResult.EngineName   = engine;
            bingResult.TermToSearch = term;
            return(bingResult);
        }
Example #3
0
        public void Search_ShouldThrowException_WhenDataWebPagesIsNull()
        {
            var term     = "java";
            var response = new BingResponseDto()
            {
                _Type = "CustomSearch"
            };

            _gateway.Setup(g => g.SearchByExpression(It.IsAny <string>())).Returns(response);

            Action act = () => _searchEngine.Search(term);

            act.Should().Throw <Exception>().And.Message.Should().Be("Requests to the Search Operation API has exceeded rate limit of your current Bing.CustomSearch");
        }
Example #4
0
        public void SearchEngineResults_ShouldReturnResults_WhenGivenValidTerms()
        {
            string[] terms    = { ".net", "java" };
            var      expected = new List <SearchResult>()
            {
                new SearchResult {
                    EngineName = "Google", Result = 1500, TermToSearch = ".net"
                },
                new SearchResult {
                    EngineName = "Bing", Result = 1000, TermToSearch = ".net"
                },
                new SearchResult {
                    EngineName = "Google", Result = 1500, TermToSearch = "java"
                },
                new SearchResult {
                    EngineName = "Bing", Result = 1000, TermToSearch = "java"
                }
            };

            var bingResponse = new BingResponseDto
            {
                WebPages = new WebPagesDto {
                    TotalEstimatedMatches = 1000, WebSearchUrl = "SomeUrl"
                },
                _Type = "Custom"
            };

            var googleResponse = new GoogleResponseDto
            {
                Kind = "CustomSerch",
                SearchInformation = new SearchInformationDto {
                    SearchTime = 0.123, TotalResults = "1500"
                }
            };

            _bingGateway.Setup(s => s.SearchByExpression(".net")).Returns(bingResponse);
            _bingGateway.Setup(s => s.SearchByExpression("java")).Returns(bingResponse);

            _googleGateway.Setup(s => s.SearchByExpression(".net")).Returns(googleResponse);
            _googleGateway.Setup(s => s.SearchByExpression("java")).Returns(googleResponse);

            var actual = _manager.SearchEngineResults(terms);

            actual.Should().BeEquivalentTo(expected);
        }
Example #5
0
        public void ToSearchResultModel_ShouldMapTotalEstimatedMatchesToResult_WhenProcessingBingResponse()
        {
            var term            = ".net";
            var engine          = "Bing";
            var bingResponseDto = new BingResponseDto
            {
                WebPages = new WebPagesDto {
                    TotalEstimatedMatches = 1500
                }
            };

            var expected = new SearchResult
            {
                EngineName   = engine,
                Result       = 1500,
                TermToSearch = term
            };

            var actual = bingResponseDto.ToSearchResultModel(term, engine);

            actual.Should().BeEquivalentTo(expected);
        }
Example #6
0
        public void Search_ShouldReturnSearchReturnModel_WhenGivenATerm()
        {
            var term     = "java";
            var expected = new SearchResult
            {
                EngineName   = "Bing",
                TermToSearch = term,
                Result       = 1000
            };

            var response = new BingResponseDto()
            {
                WebPages = new WebPagesDto {
                    TotalEstimatedMatches = 1000, WebSearchUrl = "http://FakeUrl"
                },
                _Type = "CustomSearch"
            };

            _gateway.Setup(g => g.SearchByExpression(It.IsAny <string>())).Returns(response);

            var actual = _searchEngine.Search(term);

            actual.Should().BeEquivalentTo(expected);
        }