public async Task GetRankings_ShouldReturn10Items() { var googleSearchService = new Mock <IGoogleSearchService>(); var apiController = new SearchApiController(googleSearchService.Object); var webHelper = new WebHelper(); var googleQuery = new GoogleQuery(webHelper); googleSearchService.Setup(service => service.GetRankingsAsync("", "", 100)) .ReturnsAsync(await googleQuery.GetRankingsAsync("test", 1)); var results = await apiController.Get("", ""); Assert.AreEqual(10, results.Count()); }
protected void Page_Load(object sender, EventArgs e) { var skinnable = Master as BaseMasterPage; if (skinnable != null) { skinnable.Skin = new CustomerFocusSkin(ViewSelector.CurrentViewIs(MasterPageFile)); } // Use standard parameter instead of the old tQ if (String.IsNullOrEmpty(Request.QueryString["q"]) && !String.IsNullOrEmpty(Request.QueryString["tq"])) { var query = HttpUtility.ParseQueryString(Request.QueryString.ToString()); query.Remove("tq"); query.Add("q", Request.QueryString["tq"]); var revisedUrl = new Uri(Request.Url.Scheme + "://" + Request.Url.Authority + Request.Url.AbsolutePath + "?" + query); new HttpStatus().MovedPermanently(revisedUrl); } // If there's a search query if (!String.IsNullOrEmpty(Request.QueryString["q"])) { // Redisplay search term if (!String.IsNullOrEmpty(Request.QueryString["refine"])) { this.Title = "Search results for '" + HttpUtility.HtmlEncode(Request.QueryString["refine"]) + "' within '" + HttpUtility.HtmlEncode(Request.QueryString["q"]) + "'"; } else { this.Title = "Search results for '" + HttpUtility.HtmlEncode(Request.QueryString["q"]) + "'"; } this.heading.InnerHtml = this.Title; this.catalogueSearch.InnerHtml = string.Format("<a href=\"https://e-library.eastsussex.gov.uk/cgi-bin/spydus.exe/ENQ/OPAC/BIBENQ?ENTRY_NAME=BS&ENTRY={0}&ENTRY_TYPE=K&NRECS=20&SORTS=HBT.SOVR&SEARCH_FORM=%2Fcgi-bin%2Fspydus.exe%2FMSGTRN%2FOPAC%2FBSEARCH&CF=GEN&ISGLB=0\"> Search the library catalogue for '{1}' </a>", HttpUtility.HtmlEncode(Request.QueryString["q"]), HttpUtility.HtmlEncode(Request.QueryString["q"])); // Search Google with standard options var service = new GoogleSiteSearch(ConfigurationManager.AppSettings["GoogleSearchEngineId"]); var cacheHours = new TimeSpan(Int32.Parse(ConfigurationManager.AppSettings["CacheHours"], CultureInfo.CurrentCulture), 0, 0); service.CacheStrategy = new FileCacheStrategy(Server.MapPath(ConfigurationManager.AppSettings["CacheFilePath"]), cacheHours); var query = new GoogleQuery(Request.QueryString["q"]); if (!String.IsNullOrEmpty(Request.QueryString["refine"])) { query.QueryWithinResultsTerms = Request.QueryString["refine"]; } query.PageSize = this.paging.PageSize; query.Page = this.paging.CurrentPage; try { var response = service.Search(query); // Display results this.paging.TotalResults = response.TotalResults; this.noResults.Visible = (this.paging.TotalResults == 0 && response.ResultsAvailable); this.resultsUnavailable.Visible = (this.paging.TotalResults == 0 && !response.ResultsAvailable); if (this.noResults.Visible == false) { this.searchLibrary.InnerHtml = string.Format("<a href=\"https://e-library.eastsussex.gov.uk/cgi-bin/spydus.exe/ENQ/OPAC/BIBENQ?ENTRY_NAME=BS&ENTRY={0}&ENTRY_TYPE=K&NRECS=20&SORTS=HBT.SOVR&SEARCH_FORM=%2Fcgi-bin%2Fspydus.exe%2FMSGTRN%2FOPAC%2FBSEARCH&CF=GEN&ISGLB=0\"> Search the library catalogue for '{1}' </a>", HttpUtility.HtmlEncode(Request.QueryString["q"]), HttpUtility.HtmlEncode(Request.QueryString["q"])); } var searchResults = response.Results(); this.results.DataSource = searchResults; this.results.DataBind(); // Display spelling suggestions if (response.SpellingSuggestions().Count > 0) { this.spelling.Visible = true; foreach (string suggestion in response.SpellingSuggestions()) { if (this.suggestions.Controls.Count > 0) { this.suggestions.Controls.Add(new LiteralControl(" or ")); } using (var spellingLink = new HtmlAnchor()) { spellingLink.InnerText = suggestion; spellingLink.HRef = Request.Url.LocalPath + "?q=" + HttpUtility.UrlEncode(suggestion); this.suggestions.Controls.Add(spellingLink); } } } } catch (XmlException ex) { // This catches where Google has a 500 error and sends back malformed XML, <GSP VER="3.2"> <ERROR>500</ERROR>. // Exception is "Unexpected end of file has occurred. The following elements are not closed: GSP. Line 3, position 19." this.noResults.Visible = true; new HttpStatus().BadGateway(); ex.ToExceptionless().Submit(); } } else { this.noResults.Visible = true; new HttpStatus().BadRequest(); } #if (!DEBUG) new HttpCacheHeaders().CacheUntil(Response.Cache, DateTime.Now.AddDays(1)); #endif }
// GET: Search public async Task <ActionResult> Index() { var redirect = RedirectOldUrls(); if (redirect != null) { return(redirect); } var model = new SearchResultsViewModel(); model.SearchTerm = Request.QueryString["q"]; model.SearchTermWithinResults = Request.QueryString["refine"]; model.Metadata.Title = "Search results"; model.Metadata.DateCreated = new DateTimeOffset(new DateTime(2012, 3, 16)); model.Metadata.IsInSearch = false; // If there's a search query if (!String.IsNullOrEmpty(model.SearchTerm)) { // Redisplay search term if (!String.IsNullOrEmpty(model.SearchTermWithinResults)) { model.Metadata.Title = "Search results for '" + HttpUtility.HtmlEncode(model.SearchTermWithinResults) + "' within '" + HttpUtility.HtmlEncode(model.SearchTerm) + "'"; } else { model.Metadata.Title = "Search results for '" + HttpUtility.HtmlEncode(model.SearchTerm) + "'"; } // Search Google with standard options var service = new GoogleCustomSearch(ConfigurationManager.AppSettings["GoogleSearchApiKey"], ConfigurationManager.AppSettings["GoogleSearchEngineId"], new ConfigurationProxyProvider()); var cacheHours = new TimeSpan(Int32.Parse(ConfigurationManager.AppSettings["CacheHours"], CultureInfo.CurrentCulture), 0, 0); service.CacheStrategy = new FileCacheStrategy(Server.MapPath(ConfigurationManager.AppSettings["CacheFilePath"]), cacheHours); var query = new GoogleQuery(model.SearchTerm); if (!String.IsNullOrEmpty(model.SearchTermWithinResults)) { query.QueryWithinResultsTerms = model.SearchTermWithinResults; } query.PageSize = model.Paging.PageSize; query.Page = model.Paging.CurrentPage; if ((model.Paging.PageSize * model.Paging.CurrentPage) <= model.Paging.MaximumResultsAvailable) { var response = await service.SearchAsync(query); model.Paging.TotalResults = response.TotalResults; model.Results = response.Results(); model.SpellingSuggestions = response.SpellingSuggestions(); model.ResultsAvailable = true; } else { model.ResultsAvailable = false; } } else { model.ResultsAvailable = true; } new HttpCacheHeaders().CacheUntil(Response.Cache, DateTime.Now.AddDays(1)); var templateRequest = new EastSussexGovUKTemplateRequest(Request); try { model.WebChat = await templateRequest.RequestWebChatSettingsAsync(); } catch (Exception ex) { // Catch and report exceptions - don't throw them and cause the page to fail ex.ToExceptionless().Submit(); } try { model.TemplateHtml = await templateRequest.RequestTemplateHtmlAsync(); } catch (Exception ex) { // Catch and report exceptions - don't throw them and cause the page to fail ex.ToExceptionless().Submit(); } return(View(model)); }