DDG

 view release on metacpan or  search on metacpan

t/55-rewrite.t  view on Meta::CPAN

	rewrite ^/js/spice/spice_name/(.*) /$1 break;
	proxy_pass $spice_name_upstream;
	expires 1s;
}
', 'Check POST body rewrite');

my $minrewrite = DDG::Rewrite->new(
	path => '/js/spice/spice_name/',
	to => 'http://some.api/$1',
);

isa_ok($minrewrite,'DDG::Rewrite');

is($minrewrite->missing_envs ? 1 : 0,0,'Checking now not missing ENV');
is($minrewrite->nginx_conf,'location ^~ /js/spice/spice_name/ {
	set $spice_name_upstream http://some.api:80;
	rewrite ^/js/spice/spice_name/(.*) /$1 break;
	proxy_pass $spice_name_upstream;
	expires 1s;
}
','Checking generated nginx.conf');

my $minrewrite_https = DDG::Rewrite->new(
	path => '/js/spice/spice_name/',
	to => 'https://some.api/$1',
);

isa_ok($minrewrite_https,'DDG::Rewrite');

is($minrewrite_https->missing_envs ? 1 : 0,0,'Checking now not missing ENV');
is($minrewrite_https->nginx_conf,'location ^~ /js/spice/spice_name/ {
	set $spice_name_upstream https://some.api:443;
	rewrite ^/js/spice/spice_name/(.*) /$1 break;
	proxy_pass $spice_name_upstream;
	proxy_ssl_server_name on;
	expires 1s;
}
','Checking generated nginx.conf');

my $minrewrite_with_port = DDG::Rewrite->new(
	path => '/js/spice/spice_test2/',
	to => 'http://some.api:3000/$1',
);

isa_ok($minrewrite_with_port,'DDG::Rewrite');

is($minrewrite_with_port->missing_envs ? 1 : 0,0,'Checking now not missing ENV');
is($minrewrite_with_port->nginx_conf,'location ^~ /js/spice/spice_test2/ {
	set $spice_test2_upstream http://some.api:3000;
	rewrite ^/js/spice/spice_test2/(.*) /$1 break;
	proxy_pass $spice_test2_upstream;
	expires 1s;
}
','Checking generated nginx.conf');

my $localhostrewrite = DDG::Rewrite->new(
       path => '/js/spice/spice_test/',
       to => 'https://127.0.0.1',
);
isa_ok($localhostrewrite,'DDG::Rewrite');
like($localhostrewrite->nginx_conf,qr/X-Forwarded-For/,'Checking localhost rewrite');

my $ddgrewrite = DDG::Rewrite->new(
       path => '/js/spice/spice_test/',
       to => 'https://duckduckgo.com',
);
isa_ok($ddgrewrite,'DDG::Rewrite');
like($ddgrewrite->nginx_conf,qr/X-Forwarded-For/,'Checking DuckDuckGo rewrite');

my $headers_rewrite = DDG::Rewrite->new(
	path => '/js/spice/spice_name/',
	to => 'https://some.api/$1',
	headers => 'Accept "application/vnd.citationstyles.csl+json"'
);

my $headers_nginx_conf = 'location ^~ /js/spice/spice_name/ {
	proxy_set_header Accept "application/vnd.citationstyles.csl+json";
	set $spice_name_upstream https://some.api:443;
	rewrite ^/js/spice/spice_name/(.*) /$1 break;
	proxy_pass $spice_name_upstream;
	proxy_ssl_server_name on;
	expires 1s;
}
';

is($headers_rewrite->nginx_conf, $headers_nginx_conf,'Checking generated nginx.conf with custom headers');

$headers_rewrite = DDG::Rewrite->new(
	path => '/js/spice/spice_name/',
	to => 'https://some.api/$1',
	headers => [ q{Accept "application/vnd.citationstyles.csl+json"} ]
);

is($headers_rewrite->nginx_conf, $headers_nginx_conf, 'Checking generated nginx.conf with custom headers (array)');

$headers_rewrite = DDG::Rewrite->new(
	path => '/js/spice/spice_name/',
	to => 'https://some.api/$1',
	headers => { Accept => 'application/vnd.citationstyles.csl+json' }
);

is($headers_rewrite->nginx_conf, $headers_nginx_conf, 'Checking generated nginx.conf with custom headers (hash)');

$headers_rewrite = DDG::Rewrite->new(
	path => '/js/spice/spice_name/',
	to => 'https://some.api/$1',
	headers => {
	       Accept => 'application/vnd.citationstyles.csl+json',
	       Range  => '1024-2047',
	}
);

is($headers_rewrite->nginx_conf, 'location ^~ /js/spice/spice_name/ {
	proxy_set_header Accept "application/vnd.citationstyles.csl+json";
	proxy_set_header Range "1024-2047";
	set $spice_name_upstream https://some.api:443;
	rewrite ^/js/spice/spice_name/(.*) /$1 break;
	proxy_pass $spice_name_upstream;
	proxy_ssl_server_name on;
	expires 1s;
}
', 'Checking generated nginx.conf with custom headers (hash multi)');

my $upstream_rewrite = DDG::Rewrite->new(
        path => '/js/spice/spice_name/',
        from => '([^/]+)/?(?:([^/]+)/?(?:([^/]+)|)|)',
        to => 'http://some.api/$1/?a=$2&b=$3&cb={{callback}}&ak={{ENV{DDGTEST_DDG_REWRITE_TEST_API_KEY}}}',
	callback => 'test',



( run in 1.429 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )