diff --git a/tests/unit/expected_test_results/routing/api-v0-attribute-groups.json b/tests/unit/expected_test_results/routing/api-v0-attribute-groups.json new file mode 100644 index 0000000000000..51ee8bbdb81fc --- /dev/null +++ b/tests/unit/expected_test_results/routing/api-v0-attribute-groups.json @@ -0,0 +1,17 @@ +{ + "api" : "v0", + "api_action" : "attribute_groups", + "api_method" : null, + "api_version" : "0", + "cc" : "world", + "lc" : "en", + "original_query_string" : "api/v0/attribute_groups", + "page" : 1, + "query_string" : "api/v0/attribute_groups", + "no_index" : "0", + "is_crawl_bot" : "1", + "rate_limiter_blocking" : 0, + "rate_limiter_limit" : null, + "rate_limiter_user_requests" : null, + "components" : ["api", "v0", "attribute_groups"] +} \ No newline at end of file diff --git a/tests/unit/expected_test_results/routing/api-v3-product-code.json b/tests/unit/expected_test_results/routing/api-v3-product-code.json new file mode 100644 index 0000000000000..b533f701f2677 --- /dev/null +++ b/tests/unit/expected_test_results/routing/api-v3-product-code.json @@ -0,0 +1,23 @@ +{ + "api": "v3", + "api_action": "product", + "api_method": null, + "api_version": "3", + "cc": "world", + "lc": "en", + "original_query_string": "api/v3/product/03564703999971", + "query_string": "api/v3/product/03564703999971", + "code": "03564703999971", + "page": "1", + "no_index": "0", + "is_crawl_bot": "0", + "rate_limiter_blocking": 0, + "rate_limiter_limit": 100, + "rate_limiter_user_requests": null, + "components": [ + "api", + "v3", + "product", + "03564703999971" + ] +} \ No newline at end of file diff --git a/tests/unit/expected_test_results/routing/api-v3-product-gs1-data-uri.json b/tests/unit/expected_test_results/routing/api-v3-product-gs1-data-uri.json new file mode 100644 index 0000000000000..29a0178963280 --- /dev/null +++ b/tests/unit/expected_test_results/routing/api-v3-product-gs1-data-uri.json @@ -0,0 +1,23 @@ +{ + "api": "v3", + "api_action": "product", + "api_method": null, + "api_version": "3", + "cc": "world", + "lc": "en", + "original_query_string": "api/v3/product/https%3A%2F%2Fid.gs1.org%2F01%2F03564703999971%2F10%2FABC%2F21%2F123456%3F17%3D211200", + "query_string": "api/v3/product/https%3A%2F%2Fid.gs1.org%2F01%2F03564703999971%2F10%2FABC%2F21%2F123456%3F17%3D211200", + "code": "https://id.gs1.org/01/03564703999971/10/ABC/21/123456?17=211200", + "page": "1", + "no_index": "0", + "is_crawl_bot": "0", + "rate_limiter_blocking": 0, + "rate_limiter_limit": 100, + "rate_limiter_user_requests": null, + "components": [ + "api", + "v3", + "product", + "https://id.gs1.org/01/03564703999971/10/ABC/21/123456?17=211200" + ] +} \ No newline at end of file diff --git a/tests/unit/expected_test_results/routing/facet-url-group-by-in-english.json b/tests/unit/expected_test_results/routing/facet-url-group-by-in-english.json new file mode 100644 index 0000000000000..4707913dc46b7 --- /dev/null +++ b/tests/unit/expected_test_results/routing/facet-url-group-by-in-english.json @@ -0,0 +1,26 @@ +{ + "components": [], + "groupby_tagtype": "ingredients", + "tags": [ + { + "tag": "es:breads", + "tagid": "es:breads", + "tag_prefix": "", + "tagtype": "categories" + } + ], + "is_crawl_bot": "1", + "tag_prefix": "", + "tagtype": "categories", + "canon_rel_url": "/categoria/es:breads/ingredientes", + "cc": "world", + "query_string": "category/breads/ingredients", + "original_query_string": "category/breads/ingredients", + "tagid": "es:breads", + "tag": "es:breads", + "api": "v0", + "lc": "es", + "no_index": 1, + "param": {}, + "page": 1 +} \ No newline at end of file diff --git a/tests/unit/expected_test_results/routing/facet-url-group-by.json b/tests/unit/expected_test_results/routing/facet-url-group-by.json new file mode 100644 index 0000000000000..f3df463015447 --- /dev/null +++ b/tests/unit/expected_test_results/routing/facet-url-group-by.json @@ -0,0 +1,26 @@ +{ + "tag_prefix": "", + "components": [], + "no_index": 1, + "canon_rel_url": "/category/en:breads/ingredients", + "api": "v0", + "query_string": "category/breads/ingredients", + "original_query_string": "category/breads/ingredients", + "tagid": "en:breads", + "tagtype": "categories", + "tag": "en:breads", + "lc": "en", + "cc": "world", + "page": 1, + "tags": [ + { + "tag": "en:breads", + "tagtype": "categories", + "tagid": "en:breads", + "tag_prefix": "" + } + ], + "is_crawl_bot": "1", + "param": {}, + "groupby_tagtype": "ingredients" +} \ No newline at end of file diff --git a/tests/unit/expected_test_results/routing/facet-url-with-page-number.json b/tests/unit/expected_test_results/routing/facet-url-with-page-number.json new file mode 100644 index 0000000000000..78f43af6db0b1 --- /dev/null +++ b/tests/unit/expected_test_results/routing/facet-url-with-page-number.json @@ -0,0 +1,25 @@ +{ + "api": "v0", + "canon_rel_url": "/category/en:breads", + "cc": "world", + "lc": "en", + "original_query_string": "category/breads/4", + "page": "4", + "query_string": "category/breads/4", + "tag": "en:breads", + "tag_prefix": "", + "tagid": "en:breads", + "tagtype": "categories", + "tags": [ + { + "tag": "en:breads", + "tag_prefix": "", + "tagid": "en:breads", + "tagtype": "categories" + } + ], + "param": {}, + "no_index": "1", + "is_crawl_bot": "1", + "components": [] +} \ No newline at end of file diff --git a/tests/unit/expected_test_results/routing/facet-url-with-synonym-and-page-number.json b/tests/unit/expected_test_results/routing/facet-url-with-synonym-and-page-number.json new file mode 100644 index 0000000000000..39526b8f867c2 --- /dev/null +++ b/tests/unit/expected_test_results/routing/facet-url-with-synonym-and-page-number.json @@ -0,0 +1,28 @@ +{ + "api": "v0", + "canon_rel_url": "/category/en:bread", + "cc": "world", + "lc": "en", + "original_query_string": "category/bread/4", + "page": "4", + "query_string": "category/bread/4", + "tag": "en:bread", + "tag_prefix": "", + "tagid": "en:bread", + "tagtype": "categories", + "tags": [ + { + "tag": "en:bread", + "tag_prefix": "", + "tagid": "en:bread", + "tagtype": "categories" + } + ], + "param": {}, + "no_index": "0", + "is_crawl_bot": "0", + "rate_limiter_blocking": 0, + "rate_limiter_limit": null, + "rate_limiter_user_requests": null, + "components": [] +} \ No newline at end of file diff --git a/tests/unit/expected_test_results/routing/facet-url.json b/tests/unit/expected_test_results/routing/facet-url.json new file mode 100644 index 0000000000000..36b98fa21b126 --- /dev/null +++ b/tests/unit/expected_test_results/routing/facet-url.json @@ -0,0 +1,28 @@ +{ + "api": "v0", + "canon_rel_url": "/category/en:breads", + "cc": "world", + "lc": "en", + "original_query_string": "category/breads", + "page": 1, + "query_string": "category/breads", + "tag": "en:breads", + "tag_prefix": "", + "tagid": "en:breads", + "tagtype": "categories", + "tags": [ + { + "tag": "en:breads", + "tag_prefix": "", + "tagid": "en:breads", + "tagtype": "categories" + } + ], + "param": {}, + "no_index": "0", + "is_crawl_bot": "1", + "rate_limiter_blocking": 0, + "rate_limiter_limit": null, + "rate_limiter_user_requests": null, + "components": [] +} \ No newline at end of file diff --git a/tests/unit/expected_test_results/routing/geoip-get-country-from-ipv4-us.json b/tests/unit/expected_test_results/routing/geoip-get-country-from-ipv4-us.json new file mode 100644 index 0000000000000..914fd7be11403 --- /dev/null +++ b/tests/unit/expected_test_results/routing/geoip-get-country-from-ipv4-us.json @@ -0,0 +1,22 @@ +{ + "original_query_string": "api/v3/geopip/12.45.23.45", + "page": 1, + "cc": "world", + "api_version": "3", + "components": [ + "api", + "v3", + "geopip", + "12.45.23.45" + ], + "is_crawl_bot": "0", + "no_index": "0", + "rate_limiter_blocking": 0, + "query_string": "api/v3/geopip/12.45.23.45", + "api": "v3", + "rate_limiter_limit": null, + "api_action": "geopip", + "rate_limiter_user_requests": null, + "api_method": null, + "lc": "en" +} \ No newline at end of file diff --git a/tests/unit/expected_test_results/routing/geoip-get-country-from-ipv6-fr.json b/tests/unit/expected_test_results/routing/geoip-get-country-from-ipv6-fr.json new file mode 100644 index 0000000000000..f05217ec2ebc1 --- /dev/null +++ b/tests/unit/expected_test_results/routing/geoip-get-country-from-ipv6-fr.json @@ -0,0 +1,22 @@ +{ + "api_action": "geopip", + "lc": "en", + "cc": "world", + "rate_limiter_user_requests": null, + "api": "v3", + "rate_limiter_blocking": 0, + "is_crawl_bot": "0", + "no_index": "0", + "api_version": "3", + "page": 1, + "api_method": null, + "components": [ + "api", + "v3", + "geopip", + "2001:ac8:25:3b::e01d" + ], + "original_query_string": "api/v3/geopip/2001:ac8:25:3b::e01d", + "query_string": "api/v3/geopip/2001:ac8:25:3b::e01d", + "rate_limiter_limit": null +} \ No newline at end of file diff --git a/tests/unit/expected_test_results/routing/invalid-last-url-component.json b/tests/unit/expected_test_results/routing/invalid-last-url-component.json new file mode 100644 index 0000000000000..264de01350dcd --- /dev/null +++ b/tests/unit/expected_test_results/routing/invalid-last-url-component.json @@ -0,0 +1,32 @@ +{ + "api": "v0", + "canon_rel_url": "/category/en:breads", + "cc": "world", + "error_message": "Invalid address.", + "lc": "en", + "original_query_string": "category/breads/no-nutrition-data", + "page": 1, + "query_string": "category/breads/no-nutrition-data", + "status_code": 404, + "tag": "en:breads", + "tag_prefix": "", + "tagid": "en:breads", + "tagtype": "categories", + "tags": [ + { + "tag": "en:breads", + "tag_prefix": "", + "tagid": "en:breads", + "tagtype": "categories" + } + ], + "param": {}, + "no_index": "0", + "is_crawl_bot": "0", + "components": [ + "no-nutrition-data" + ], + "rate_limiter_blocking": 0, + "rate_limiter_limit": null, + "rate_limiter_user_requests": null +} \ No newline at end of file diff --git a/tests/unit/routing.t b/tests/unit/routing.t index 8f78a86085eda..fcd7798c9ac55 100644 --- a/tests/unit/routing.t +++ b/tests/unit/routing.t @@ -3,23 +3,23 @@ use Modern::Perl '2017'; use utf8; +use ProductOpener::Test qw/compare_to_expected_results init_expected_results/; +use ProductOpener::Routing qw/analyze_request load_routes/; +use ProductOpener::Lang qw/$lc /; + use Test2::V0; +use Mock::Quick; use Data::Dumper; $Data::Dumper::Terse = 1; use Log::Any::Adapter 'TAP'; -use ProductOpener::Routing qw/analyze_request load_routes/; -use ProductOpener::Lang qw/$lc /; - -# TODO: create a test case array and use the update_test_results system to -# store and compare the returned $request object - +my ($test_id, $test_dir, $expected_result_dir, $update_expected_results) = (init_expected_results(__FILE__)); # TODO: add tests for all routes - load_routes(); my @tests = ( { + id => "api-v0-attribute-groups", desc => "API to get attribute groups", lc => "en", input_request => { @@ -29,25 +29,9 @@ my @tests = ( no_index => '0', is_crawl_bot => '1' }, - expected_output_request => { - 'api' => 'v0', - 'api_action' => 'attribute_groups', - 'api_method' => undef, - 'api_version' => '0', - 'cc' => 'world', - 'lc' => 'en', - 'original_query_string' => 'api/v0/attribute_groups', - 'page' => 1, - 'query_string' => 'api/v0/attribute_groups', - 'no_index' => '0', - 'is_crawl_bot' => '1', - 'rate_limiter_blocking' => 0, - 'rate_limiter_limit' => undef, - 'rate_limiter_user_requests' => undef, - 'components' => ['api', 'v0', 'attribute_groups'], - }, }, { + id => 'invalid-last-url-component', desc => "Invalid URL with last component which is not a facet or a page number", lc => "en", input_request => { @@ -57,38 +41,9 @@ my @tests = ( no_index => '0', is_crawl_bot => '0' }, - expected_output_request => { - 'api' => 'v0', - 'canon_rel_url' => '/category/en:breads', - 'cc' => 'world', - 'error_message' => 'Invalid address.', - 'lc' => 'en', - 'original_query_string' => 'category/breads/no-nutrition-data', - 'page' => 1, - 'query_string' => 'category/breads/no-nutrition-data', - 'status_code' => 404, - 'tag' => 'en:breads', - 'tag_prefix' => '', - 'tagid' => 'en:breads', - 'tagtype' => 'categories', - 'tags' => [ - { - 'tag' => 'en:breads', - 'tag_prefix' => '', - 'tagid' => 'en:breads', - 'tagtype' => 'categories' - }, - ], - 'param' => {}, - 'no_index' => '0', - 'is_crawl_bot' => '0', - 'components' => ['no-nutrition-data'], - 'rate_limiter_blocking' => 0, - 'rate_limiter_limit' => undef, - 'rate_limiter_user_requests' => undef, - }, }, { + id => 'facet-url', desc => "Facet URL", lc => "en", input_request => { @@ -98,36 +53,9 @@ my @tests = ( no_index => '0', is_crawl_bot => '1' }, - expected_output_request => { - 'api' => 'v0', - 'canon_rel_url' => '/category/en:breads', - 'cc' => 'world', - 'lc' => 'en', - 'original_query_string' => 'category/breads', - 'page' => 1, - 'query_string' => 'category/breads', - 'tag' => 'en:breads', - 'tag_prefix' => '', - 'tagid' => 'en:breads', - 'tagtype' => 'categories', - 'tags' => [ - { - 'tag' => 'en:breads', - 'tag_prefix' => '', - 'tagid' => 'en:breads', - 'tagtype' => 'categories' - }, - ], - 'param' => {}, - 'no_index' => '0', - 'is_crawl_bot' => '1', - 'rate_limiter_blocking' => 0, - 'rate_limiter_limit' => undef, - 'rate_limiter_user_requests' => undef, - 'components' => [], - }, }, { + id => 'facet-url-with-page-number', desc => "Facet URL with a page number", lc => "en", input_request => { @@ -137,33 +65,9 @@ my @tests = ( no_index => '0', is_crawl_bot => '1' }, - expected_output_request => { - 'api' => 'v0', - 'canon_rel_url' => '/category/en:breads', - 'cc' => 'world', - 'lc' => 'en', - 'original_query_string' => 'category/breads/4', - 'page' => '4', - 'query_string' => 'category/breads/4', - 'tag' => 'en:breads', - 'tag_prefix' => '', - 'tagid' => 'en:breads', - 'tagtype' => 'categories', - 'tags' => [ - { - 'tag' => 'en:breads', - 'tag_prefix' => '', - 'tagid' => 'en:breads', - 'tagtype' => 'categories' - }, - ], - 'param' => {}, - 'no_index' => '1', - 'is_crawl_bot' => '1', - 'components' => [], - }, }, { + id => 'facet-url-with-synonym-and-page-number', desc => "Facet URL with a facet synonym and a page number", lc => "en", input_request => { @@ -173,36 +77,9 @@ my @tests = ( no_index => '0', is_crawl_bot => '0' }, - expected_output_request => { - 'api' => 'v0', - 'canon_rel_url' => '/category/en:bread', - 'cc' => 'world', - 'lc' => 'en', - 'original_query_string' => 'category/bread/4', - 'page' => '4', - 'query_string' => 'category/bread/4', - 'tag' => 'en:bread', - 'tag_prefix' => '', - 'tagid' => 'en:bread', - 'tagtype' => 'categories', - 'tags' => [ - { - 'tag' => 'en:bread', - 'tag_prefix' => '', - 'tagid' => 'en:bread', - 'tagtype' => 'categories' - }, - ], - 'param' => {}, - 'no_index' => '0', - 'is_crawl_bot' => '0', - 'rate_limiter_blocking' => 0, - 'rate_limiter_limit' => undef, - 'rate_limiter_user_requests' => undef, - 'components' => [], - }, }, { + id => 'api-v3-product-code', desc => "API v3 URL with product code", lc => "en", input_request => { @@ -212,26 +89,9 @@ my @tests = ( no_index => '0', is_crawl_bot => '0' }, - expected_output_request => { - 'api' => 'v3', - 'api_action' => 'product', - 'api_method' => undef, - 'api_version' => '3', - 'cc' => 'world', - 'lc' => 'en', - 'original_query_string' => 'api/v3/product/03564703999971', - 'query_string' => 'api/v3/product/03564703999971', - 'code' => '03564703999971', - 'page' => '1', - 'no_index' => '0', - 'is_crawl_bot' => '0', - 'rate_limiter_blocking' => 0, - 'rate_limiter_limit' => 100, - 'rate_limiter_user_requests' => undef, - 'components' => ['api', 'v3', 'product', '03564703999971'], - }, }, { + id => 'api-v3-product-gs1-data-uri', desc => "API v3 URL with product GS1 Data URI", lc => "en", input_request => { @@ -242,28 +102,9 @@ my @tests = ( no_index => '0', is_crawl_bot => '0' }, - expected_output_request => { - 'api' => 'v3', - 'api_action' => 'product', - 'api_method' => undef, - 'api_version' => '3', - 'cc' => 'world', - 'lc' => 'en', - 'original_query_string' => - 'api/v3/product/https%3A%2F%2Fid.gs1.org%2F01%2F03564703999971%2F10%2FABC%2F21%2F123456%3F17%3D211200', - 'query_string' => - 'api/v3/product/https%3A%2F%2Fid.gs1.org%2F01%2F03564703999971%2F10%2FABC%2F21%2F123456%3F17%3D211200', - 'code' => 'https://id.gs1.org/01/03564703999971/10/ABC/21/123456?17=211200', - 'page' => '1', - 'no_index' => '0', - 'is_crawl_bot' => '0', - 'rate_limiter_blocking' => 0, - 'rate_limiter_limit' => 100, - 'rate_limiter_user_requests' => undef, - 'components' => ['api', 'v3', 'product', 'https://id.gs1.org/01/03564703999971/10/ABC/21/123456?17=211200'], - }, }, { + id => 'facet-url-group-by', desc => "Facet URL with a group-by", lc => "en", input_request => { @@ -273,35 +114,9 @@ my @tests = ( no_index => '0', is_crawl_bot => '1' }, - expected_output_request => { - 'tag_prefix' => '', - 'components' => [], - 'no_index' => 1, - 'canon_rel_url' => '/category/en:breads/ingredients', - 'api' => 'v0', - 'query_string' => 'category/breads/ingredients', - 'original_query_string' => 'category/breads/ingredients', - 'tagid' => 'en:breads', - 'tagtype' => 'categories', - 'tag' => 'en:breads', - 'lc' => 'en', - 'cc' => 'world', - 'page' => 1, - 'tags' => [ - { - 'tag' => 'en:breads', - 'tagtype' => 'categories', - 'tagid' => 'en:breads', - 'tag_prefix' => '' - } - ], - 'is_crawl_bot' => '1', - 'param' => {}, - 'groupby_tagtype' => 'ingredients' - } - }, { + id => 'facet-url-group-by-in-english', desc => "Facet URL with a group-by in English", lc => "en", input_request => { @@ -311,33 +126,30 @@ my @tests = ( no_index => '0', is_crawl_bot => '1' }, - expected_output_request => { - 'components' => [], - 'groupby_tagtype' => 'ingredients', - 'tags' => [ - { - 'tag' => 'es:breads', - 'tagid' => 'es:breads', - 'tag_prefix' => '', - 'tagtype' => 'categories' - } - ], - 'is_crawl_bot' => '1', - 'tag_prefix' => '', - 'tagtype' => 'categories', - 'canon_rel_url' => '/categoria/es:breads/ingredientes', - 'cc' => 'world', - 'query_string' => 'category/breads/ingredients', - 'original_query_string' => 'category/breads/ingredients', - 'tagid' => 'es:breads', - 'tag' => 'es:breads', - 'api' => 'v0', - 'lc' => 'es', - 'no_index' => 1, - 'param' => {}, - 'page' => 1 - }, + }, + { + id => 'geoip-get-country-from-ipv4-us', + desc => 'geoip get country from ipv4 us', + input_request => { + cc => "world", + lc => "en", + original_query_string => 'api/v3/geopip/12.45.23.45', + no_index => '0', + is_crawl_bot => '0', + } + }, + { + id => 'geoip-get-country-from-ipv6-fr', + desc => 'geoip get country from ipv6 fr', + lc => "en", + input_request => { + cc => "world", + lc => "en", + original_query_string => 'api/v3/geopip/2001:ac8:25:3b::e01d', + no_index => '0', + is_crawl_bot => '0', + }, }, ); @@ -346,8 +158,11 @@ foreach my $test_ref (@tests) { # Set $lc global because currently analyze_request uses the global $lc $lc = $test_ref->{input_request}{lc}; analyze_request($test_ref->{input_request}); - - is($test_ref->{input_request}, $test_ref->{expected_output_request}, $test_ref->{desc}) or diag Dumper $test_ref; + compare_to_expected_results( + $test_ref->{input_request}, + "$expected_result_dir/$test_ref->{id}.json", + $update_expected_results, $test_ref + ); } done_testing();