<commit_before>use lib 'lib';
use Test::Nginx::Socket;

$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;

plan tests => $Test::Nginx::Socket::RepeatEach * 2 * blocks();

run_tests();

__DATA__

=== TEST 1: test w/ auth-token present in client request
--- main_config
--- http_config
lua_package_path "./build/usr/share/borderpatrol/?.lua;./build/usr/share/lua/5.1/?.lua;;";
lua_package_cpath "./build/usr/lib/lua/5.1/?.so;;";

upstream b {
  server 127.0.0.1:$TEST_NGINX_SERVER_PORT; # self
}

--- config
location /testpath {
    echo_status 200;
    echo_duplicate 1 $echo_client_request_headers;
    echo 'everything is ok';
    echo_flush;
}
location /auth {
    echo_status 200;
    echo_flush;
}
location /b/testpath {
    set $auth_token $http_auth_token;
    access_by_lua_file '../../build/usr/share/borderpatrol/access.lua';
    proxy_set_header auth-token $auth_token;

    # http://hostname/upstream_name/uri -> http://upstream_name/uri
    rewrite ^/([^/]+)/?(.*)$ /$2 break;
    proxy_pass         http://$1;
    proxy_redirect     off;
    proxy_set_header   Host $host;
}
--- request
GET /b/testpath
--- more_headers
auth-token: <PASSWORD>
--- error_code: 200
--- response_body_like
auth-token: <PASSWORD>.+everything is ok$

=== TEST 2: test w/o auth-token not present in client request but with valid session
--- main_config
--- http_config
lua_package_path "./build/usr/share/borderpatrol/?.lua;./build/usr/share/lua/5.1/?.lua;;";
lua_package_cpath "./build/usr/lib/lua/5.1/?.so;;";
init_by_lua 'service_mappings = {b="smb",s="flexd"}';
upstream b {
  server 127.0.0.1:$TEST_NGINX_SERVER_PORT; # self
}
--- config
location /testpath {
    echo_status 200;
    echo_duplicate 1 $echo_client_request_headers;
    echo 'everything is ok';
    echo_flush;
}
location /auth {
    internal;
    echo_status 200;
    more_set_headers 'Auth-Token: <PASSWORD>';
    echo_flush;
}
location /b/testpath {
    set $auth_token $http_auth_token;
    access_by_lua_file '../../build/usr/share/borderpatrol/access.lua';
    proxy_set_header auth-token $auth_token;

    # http://hostname/upstream_name/uri -> http://upstream_name/uri
    rewrite ^/([^/]+)/?(.*)$ /$2 break;
    proxy_pass         http://$1;
    proxy_redirect     off;
    proxy_set_header   Host $host;
}
--- request
GET /b/testpath
--- more_headers
Cookie: border_session=this-is-a-session-id # not checked here!
--- error_code: 200
--- response_body_like
auth-token: <PASSWORD>.+everything is ok$<commit_msg>update tests to use correct auth-token header<commit_after>use lib 'lib';
use Test::Nginx::Socket;

$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;

plan tests => $Test::Nginx::Socket::RepeatEach * 2 * blocks();

run_tests();

__DATA__

=== TEST 1: test w/ Auth-Token present in client request
--- main_config
--- http_config
lua_package_path "./build/usr/share/borderpatrol/?.lua;./build/usr/share/lua/5.1/?.lua;;";
lua_package_cpath "./build/usr/lib/lua/5.1/?.so;;";

upstream b {
  server 127.0.0.1:$TEST_NGINX_SERVER_PORT; # self
}

--- config
location /testpath {
    echo_status 200;
    echo_duplicate 1 $echo_client_request_headers;
    echo 'everything is ok';
    echo_flush;
}
location /auth {
    echo_status 200;
    echo_flush;
}
location /b/testpath {
    set $auth_token $http_auth_token;
    access_by_lua_file '../../build/usr/share/borderpatrol/access.lua';
    proxy_set_header Auth-Token $auth_token;

    # http://hostname/upstream_name/uri -> http://upstream_name/uri
    rewrite ^/([^/]+)/?(.*)$ /$2 break;
    proxy_pass         http://$1;
    proxy_redirect     off;
    proxy_set_header   Host $host;
}
--- request
GET /b/testpath
--- more_headers
Auth-Token: <PASSWORD>
--- error_code: 200
--- response_body_like
Auth-Token: <PASSWORD>.+everything is ok$

=== TEST 2: test w/o Auth-Token not present in client request but with valid session
--- main_config
--- http_config
lua_package_path "./build/usr/share/borderpatrol/?.lua;./build/usr/share/lua/5.1/?.lua;;";
lua_package_cpath "./build/usr/lib/lua/5.1/?.so;;";
init_by_lua 'service_mappings = {b="smb",s="flexd"}';
upstream b {
  server 127.0.0.1:$TEST_NGINX_SERVER_PORT; # self
}
--- config
location /testpath {
    echo_status 200;
    echo_duplicate 1 $echo_client_request_headers;
    echo 'everything is ok';
    echo_flush;
}
location /auth {
    internal;
    echo_status 200;
    more_set_headers 'Auth-Token: tok<PASSWORD>';
    echo_flush;
}
location /b/testpath {
    set $auth_token $http_auth_token;
    access_by_lua_file '../../build/usr/share/borderpatrol/access.lua';
    proxy_set_header Auth-Token $auth_token;

    # http://hostname/upstream_name/uri -> http://upstream_name/uri
    rewrite ^/([^/]+)/?(.*)$ /$2 break;
    proxy_pass         http://$1;
    proxy_redirect     off;
    proxy_set_header   Host $host;
}
--- request
GET /b/testpath
--- more_headers
Cookie: border_session=this-is-a-session-id # not checked here!
--- error_code: 200
--- response_body_like
Auth-Token: <PASSWORD>.+everything is ok$
{'fraction_non_alphanumeric': 0.1583383503913305, 'fraction_numerical': 0.02087096126831226, 'mean_word_length': 3.4184397163120566, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6286692', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1787, 'n_words': 355}