예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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);
        }
예제 #5
0
        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);
        }
예제 #6
0
        public void ConstructorDefaultTest()
        {
            var request = new WebSearchRequest();

            Assert.IsTrue(request.PrettyPrint);
            Assert.AreEqual(request.Alt, AltType.Json);
        }
예제 #7
0
        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}");
        }
예제 #8
0
        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);
        }
예제 #9
0
        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);
        }
예제 #10
0
        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");
        }
예제 #11
0
        public void GetQueryStringParametersTest()
        {
            var request = new WebSearchRequest
            {
                Key            = "abc",
                SearchEngineId = "abc",
                Query          = "abc"
            };

            Assert.DoesNotThrow(() => request.GetQueryStringParameters());
        }
예제 #12
0
        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.");
        }
예제 #13
0
        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}&quotaUser={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);
        }
예제 #14
0
        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");
        }
예제 #15
0
        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");
        }
예제 #16
0
        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);
        }
예제 #18
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");
        }
예제 #19
0
        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");
        }
예제 #20
0
        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");
        }
예제 #21
0
        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}");
        }
예제 #22
0
        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.");
        }
예제 #23
0
        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);
        }
예제 #24
0
        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);
        }
예제 #25
0
        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");
        }
예제 #26
0
        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");
        }
예제 #27
0
        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");
        }
예제 #28
0
        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);
        }
예제 #29
0
        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);
        }
예제 #30
0
        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}");
        }