public void WebSearchWhenFieldsTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google", Fields = "kind" }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.AreEqual(response.Status, Status.Ok); Assert.AreEqual(response.Kind, "customsearch#search"); Assert.IsNull(response.Url); Assert.IsNull(response.Context); Assert.IsNull(response.Spelling); Assert.IsNull(response.Search); Assert.IsNull(response.Items); Assert.IsNull(response.Promotions); Assert.IsNull(response.Query); Assert.IsNull(response.NextPage); Assert.IsNull(response.PreviousPage); }
public void WebSearchWhenDisableCnTwTranslationFalseTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google", Options = { DisableCnTwTranslation = false } }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.AreEqual(response.Status, Status.Ok); var items = response.Items; Assert.IsNotNull(items); Assert.IsNotEmpty(response.Items); var query = response.Query; Assert.IsNotNull(query); Assert.IsFalse(query.DisableCnTwTranslation); }
public void WebSearchWhenInterfaceLanguageTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google", Options = { InterfaceLanguage = Language.German } }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.AreEqual(response.Status, Status.Ok); var items = response.Items; Assert.IsNotNull(items); Assert.IsNotEmpty(response.Items); var query = response.Query; Assert.IsNotNull(query); Assert.AreEqual(Language.German, query.InterfaceLanguage); }
public void WebSearchWhenGeoLocationTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google", Options = { GeoLocation = GeoLocation.Denmark } }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.AreEqual(response.Status, Status.Ok); var items = response.Items; Assert.IsNotNull(items); Assert.IsNotEmpty(response.Items); var query = response.Query; Assert.IsNotNull(query); Assert.AreEqual(GeoLocation.Denmark, query.GeoLocation); }
public void WebSearchWhenSiteSearchTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google", Options = { SiteSearch = new SiteSearch { Site = "google.com", Filter = SiteSearchFilter.Exclude } } }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.AreEqual(response.Status, Status.Ok); var items = response.Items; Assert.IsNotNull(items); Assert.IsNotEmpty(response.Items); var query = response.Query; Assert.IsNotNull(query); Assert.IsNotNull(query.SiteSearch); Assert.AreEqual("google.com", query.SiteSearch.Site); Assert.AreEqual(SiteSearchFilter.Exclude, query.SiteSearch.Filter); }
public void ConstructorDefaultTest() { var request = new WebSearchRequest(); Assert.IsTrue(request.PrettyPrint); Assert.AreEqual(request.Alt, AltType.Json); }
public void WebSearchWhenSafetyLevelAndNotAllowedTest() { var request = new WebSearchRequest { Key = this.ApiKey, Query = "google", SearchEngineId = this.SearchEngineId, Options = { InterfaceLanguage = Language.Danish, SafetyLevel = SafetyLevel.High } }; var exception = Assert.Throws <AggregateException>(() => GoogleSearch.WebSearch.QueryAsync(request).Wait()); Assert.IsNotNull(exception); Assert.AreEqual("One or more errors occurred.", exception.Message); var innerException = exception.InnerException; Assert.IsNotNull(innerException); Assert.AreEqual(typeof(GoogleApiException), innerException.GetType()); Assert.AreEqual(innerException.Message, $"SafetyLevel is not allowed for specified InterfaceLanguage: {request.Options.InterfaceLanguage}"); }
public void WebSearchWhenDateRestrictTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google", Options = { DateRestrict = new DateRestrict { Type = DateRestrictType.Days, Number = 3 } } }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.AreEqual(response.Status, Status.Ok); var items = response.Items; Assert.IsNotNull(items); Assert.IsNotEmpty(response.Items); var query = response.Query; Assert.IsNotNull(query); Assert.AreEqual(3, query.DateRestrict.Number); Assert.AreEqual(DateRestrictType.Days, query.DateRestrict.Type); }
public void WebSearchWhenNumberTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google", Options = { Number = 6 } }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.AreEqual(response.Status, Status.Ok); var items = response.Items; Assert.IsNotNull(items); Assert.IsNotEmpty(response.Items); var query = response.Query; Assert.IsNotNull(query); Assert.AreEqual(6, query.Count); }
public void WebSearchWhenKeyIsNullTest() { var request = new WebSearchRequest { Key = null }; var exception = Assert.Throws <ArgumentException>(() => GoogleSearch.WebSearch.Query(request)); Assert.AreEqual(exception.Message, "Key is required"); }
public void GetQueryStringParametersTest() { var request = new WebSearchRequest { Key = "abc", SearchEngineId = "abc", Query = "abc" }; Assert.DoesNotThrow(() => request.GetQueryStringParameters()); }
public void WebSearchWhenQueryIsNullTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = null }; var exception = Assert.Throws <ArgumentException>(() => GoogleSearch.WebSearch.Query(request)); Assert.AreEqual(exception.Message, "Query is required."); }
public void GetUriTest() { var request = new WebSearchRequest { Key = "abc", SearchEngineId = "abc", Query = "abc" }; var uri = request.GetUri(); Assert.IsNotNull(uri); Assert.AreEqual($"/customsearch/v1?key={request.Key}&q={request.Query}&cx={request.SearchEngineId}&alt={request.Alt.ToString().ToLower()}&prettyPrint={request.PrettyPrint.ToString().ToLower()}&userIp={request.UserIp}"aUser={request.QuotaUser}&c2coff=1&fileType={string.Join(",", request.Options.FileTypes)}&filter=0&hl={request.Options.InterfaceLanguage.ToHl()}&num={request.Options.Number}&rights={string.Join(",", request.Options.Rights)}&safe={request.Options.SafetyLevel.ToString().ToLower()}&start={request.Options.StartIndex.ToString()}", uri.PathAndQuery); }
public void GetQueryStringParametersWhenKeyIsStringEmptyTest() { var request = new WebSearchRequest { Key = string.Empty }; var exception = Assert.Throws <ArgumentException>(() => { var parameters = request.GetQueryStringParameters(); Assert.IsNull(parameters); }); Assert.AreEqual(exception.Message, "Key is required"); }
public void GetQueryStringParametersWhenQueryIsNullTest() { var request = new WebSearchRequest { Key = "abc", Query = null }; var exception = Assert.Throws <ArgumentException>(() => { var parameters = request.GetQueryStringParameters(); Assert.IsNull(parameters); }); Assert.AreEqual(exception.Message, "Query is required"); }
public void WebSearchWhenSafetyLevelOffTest() { var request = new WebSearchRequest { Key = this.ApiKey, Query = "google", SearchEngineId = this.SearchEngineId, Options = { InterfaceLanguage = Language.Danish, SafetyLevel = SafetyLevel.Off } }; Assert.DoesNotThrow(() => GoogleSearch.WebSearch.Query(request)); }
public void GetWebResultsTest() { var request = new WebSearchRequest(); request.Query = "unicorns"; request.Count = 10; request.Offset = 0; request.Market = "en-US"; request.SafeSearch = SafeSearch.Moderate; var client = new WebSearchClient(this.apiKey); var response = client.GetWebResults(request); Assert.IsTrue(response.WebPages.Results.Count > 0); }
public void WebSearchWhenKeyIsNullTest() { var request = new WebSearchRequest { Key = null }; var exception = Assert.Throws <AggregateException>(() => GoogleSearch.WebSearch.QueryAsync(request).Wait()); Assert.IsNotNull(exception); var innerException = exception.InnerException; Assert.IsNotNull(innerException); Assert.AreEqual(typeof(GoogleApiException), innerException.GetType()); Assert.AreEqual(innerException.Message, "Key is required"); }
public void WebSearchWhenNumberIsGreaterThanTenTest() { var request = new WebSearchRequest { Key = this.ApiKey, Query = "google", SearchEngineId = this.SearchEngineId, Options = { Number = 11 } }; var exception = Assert.Throws <InvalidOperationException>(() => GoogleSearch.WebSearch.Query(request)); Assert.AreEqual(exception.Message, "Number must be between 1 and 10"); }
public void GetQueryStringParametersWhenSearchEngineIdIsNullTest() { var request = new WebSearchRequest { Key = this.ApiKey, Query = "google", SearchEngineId = null }; var exception = Assert.Throws <ArgumentException>(() => { var parameters = request.GetQueryStringParameters(); Assert.IsNull(parameters); }); Assert.AreEqual(exception.Message, "SearchEngineId is required"); }
public void WebSearchWhenSafetyLevelAndNotAllowedTest() { var request = new WebSearchRequest { Key = this.ApiKey, Query = "google", SearchEngineId = this.SearchEngineId, Options = { InterfaceLanguage = Language.Danish, SafetyLevel = SafetyLevel.High } }; var exception = Assert.Throws <InvalidOperationException>(() => GoogleSearch.WebSearch.Query(request)); Assert.AreEqual(exception.Message, $"SafetyLevel is not allowed for specified InterfaceLanguage: {request.Options.InterfaceLanguage}"); }
public void WebSearchWhenAsyncAndCancelledTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google" }; var cancellationTokenSource = new CancellationTokenSource(); var task = GoogleSearch.WebSearch.QueryAsync(request, cancellationTokenSource.Token); cancellationTokenSource.Cancel(); var exception = Assert.Throws <OperationCanceledException>(() => task.Wait(cancellationTokenSource.Token)); Assert.IsNotNull(exception); Assert.AreEqual(exception.Message, "The operation was canceled."); }
public void WebSearchWhenStartIndexTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google", Options = { StartIndex = 11 } }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.AreEqual(response.Status, Status.Ok); Assert.AreEqual(response.Kind, "customsearch#search"); var items = response.Items; Assert.IsNotNull(items); Assert.IsNotEmpty(response.Items); var item = response.Items.FirstOrDefault(); Assert.IsNotNull(item); var query = response.Query; Assert.IsNotNull(query); Assert.AreEqual(11, query.StartIndex); var nextPage = response.NextPage; Assert.IsNotNull(nextPage); Assert.AreEqual(21, nextPage.StartIndex); var previousPage = response.PreviousPage; Assert.IsNotNull(previousPage); Assert.AreEqual(1, previousPage.StartIndex); }
public void WebSearchAsyncTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google" }; var response = GoogleSearch.WebSearch.QueryAsync(request).Result; Assert.IsNotNull(response); Assert.IsNotEmpty(response.Items); Assert.AreEqual(response.Status, Status.Ok); var items = response.Items; Assert.IsNotEmpty(items); }
public void WebSearchWhenQueryIsNullTest() { var request = new WebSearchRequest { Key = this.ApiKey, Query = null }; var exception = Assert.Throws <AggregateException>(() => GoogleSearch.WebSearch.Query(request)); Assert.IsNotNull(exception); Assert.AreEqual("One or more errors occurred.", exception.Message); var innerException = exception.InnerException; Assert.IsNotNull(innerException); Assert.AreEqual(typeof(GoogleApiException), innerException.GetType()); Assert.AreEqual(innerException.Message, "Query is required"); }
public void WebSearchTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google" }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.IsNotEmpty(response.Items); Assert.AreEqual(response.Kind, "customsearch#search"); Assert.AreEqual(response.Status, Status.Ok); Assert.IsNotNull(response.Url); Assert.AreEqual(response.Url.Type, "application/json"); Assert.AreEqual(response.Url.Template, "https://www.googleapis.com/customsearch/v1?q={searchTerms}&num={count?}&start={startIndex?}&lr={language?}&safe={safe?}&cx={cx?}&cref={cref?}&sort={sort?}&filter={filter?}&gl={gl?}&cr={cr?}&googlehost={googleHost?}&c2coff={disableCnTwTranslation?}&hq={hq?}&hl={hl?}&siteSearch={siteSearch?}&siteSearchFilter={siteSearchFilter?}&exactTerms={exactTerms?}&excludeTerms={excludeTerms?}&linkSite={linkSite?}&orTerms={orTerms?}&relatedSite={relatedSite?}&dateRestrict={dateRestrict?}&lowRange={lowRange?}&highRange={highRange?}&searchType={searchType}&fileType={fileType?}&rights={rights?}&imgSize={imgSize?}&imgType={imgType?}&imgColorType={imgColorType?}&imgDominantColor={imgDominantColor?}&alt=json"); Assert.IsNotNull(response.SearchInformation); Assert.Greater(response.SearchInformation.SearchTime, 0.00); Assert.IsNotEmpty(response.SearchInformation.SearchTimeFormatted); Assert.Greater(response.SearchInformation.TotalResults, 0); Assert.IsNotEmpty(response.SearchInformation.TotalResultsFormatted); var context = response.Context; Assert.IsNotNull(context); Assert.AreEqual(context.Title, "Google Web"); var items = response.Items; Assert.IsNotNull(items); var item = response.Items.FirstOrDefault(); Assert.IsNotNull(item); Assert.AreEqual(item.Link, "http://www.google.com/"); Assert.AreEqual(item.Title, "Google"); Assert.AreEqual(item.DisplayLink, "www.google.com"); }
public void GetQueryStringParametersWhenOptionsNumberIsGreaterThanTenTest() { var request = new WebSearchRequest { Key = "abc", Query = "google", SearchEngineId = "abc", Options = { Number = 11 } }; var exception = Assert.Throws <InvalidOperationException>(() => { var parameters = request.GetQueryStringParameters(); Assert.IsNull(parameters); }); Assert.AreEqual(exception.Message, "Number must be between 1 and 10"); }
public void WebSearchWhenCountryRestrictionTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google", Options = { CountryRestrict = new CountryRestrict { Expressions = new List <CountryRestrictExpression> { new CountryRestrictExpression { Country = Country.Denmark }, new CountryRestrictExpression { Country = Country.Italy } } } } }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.AreEqual(response.Status, Status.Ok); var items = response.Items; Assert.IsNotNull(items); Assert.IsNotEmpty(response.Items); var query = response.Query; Assert.IsNotNull(query); Assert.AreEqual("(countryDK.countryIT).", query.CountryRestrict); }
public void WebSearchWhenSortExpressionTest() { var request = new WebSearchRequest { Key = this.ApiKey, SearchEngineId = this.SearchEngineId, Query = "google", Options = { SortExpression = new SortExpression { By = SortBy.Date, Order = SortOrder.Descending, DefaultValue = 2 } } }; var response = GoogleSearch.WebSearch.Query(request); Assert.IsNotNull(response); Assert.AreEqual(response.Status, Status.Ok); var items = response.Items; Assert.IsNotNull(items); Assert.IsNotEmpty(response.Items); var query = response.Query; Assert.IsNotNull(query); var sort = query.SortExpression; Assert.IsNotNull(sort); Assert.AreEqual(SortBy.Date, sort.By); Assert.AreEqual(SortOrder.Descending, sort.Order); Assert.AreEqual(2, sort.DefaultValue); }
public void GetQueryStringParametersWhenSafetyLevelInterfaceLanguageIsNotSupportedTest() { var request = new WebSearchRequest { Key = "abc", Query = "google", SearchEngineId = "abc", Options = { SafetyLevel = SafetyLevel.Medium, InterfaceLanguage = Language.Afrikaans } }; var exception = Assert.Throws <InvalidOperationException>(() => { var parameters = request.GetQueryStringParameters(); Assert.IsNull(parameters); }); Assert.AreEqual(exception.Message, $"SafetyLevel is not allowed for specified InterfaceLanguage: {request.Options.InterfaceLanguage}"); }