2016-05-25 21:04:34 +00:00
|
|
|
/*
|
2016-09-08 11:02:39 +00:00
|
|
|
Copyright 2015 The Kubernetes Authors.
|
2016-05-25 21:04:34 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-07 22:53:08 +00:00
|
|
|
package template
|
2016-05-25 21:04:34 +00:00
|
|
|
|
|
|
|
import (
|
2017-08-19 21:13:02 +00:00
|
|
|
"io/ioutil"
|
2017-09-17 18:42:31 +00:00
|
|
|
"net"
|
2016-11-16 18:24:26 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2017-02-04 00:43:15 +00:00
|
|
|
"reflect"
|
2017-04-02 14:07:07 +00:00
|
|
|
"strings"
|
2016-05-25 21:04:34 +00:00
|
|
|
"testing"
|
|
|
|
|
2018-01-28 00:32:08 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2018-02-02 19:53:28 +00:00
|
|
|
|
2018-09-22 17:25:01 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2017-11-22 13:40:54 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/file"
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress"
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/authreq"
|
2018-04-08 20:37:13 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/luarestywaf"
|
2017-11-07 22:02:12 +00:00
|
|
|
"k8s.io/ingress-nginx/internal/ingress/annotations/rewrite"
|
|
|
|
"k8s.io/ingress-nginx/internal/ingress/controller/config"
|
2016-05-25 21:04:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-03-18 13:13:41 +00:00
|
|
|
// TODO: add tests for SSLPassthrough
|
2016-05-25 21:04:34 +00:00
|
|
|
tmplFuncTestcases = map[string]struct {
|
2018-10-09 22:36:10 +00:00
|
|
|
Path string
|
|
|
|
Target string
|
|
|
|
Location string
|
|
|
|
ProxyPass string
|
|
|
|
AddBaseURL bool
|
|
|
|
BaseURLScheme string
|
|
|
|
Sticky bool
|
|
|
|
XForwardedPrefix bool
|
|
|
|
SecureBackend bool
|
|
|
|
enforceRegex bool
|
2016-05-25 21:04:34 +00:00
|
|
|
}{
|
2018-03-18 13:13:41 +00:00
|
|
|
"when secure backend enabled": {
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
"/",
|
2018-10-09 22:36:10 +00:00
|
|
|
"proxy_pass https://upstream_balancer;",
|
2018-03-18 13:13:41 +00:00
|
|
|
false,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
2018-09-14 19:07:57 +00:00
|
|
|
true,
|
2018-10-08 15:26:06 +00:00
|
|
|
false,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"when secure backend and dynamic config enabled": {
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
"proxy_pass https://upstream_balancer;",
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
true,
|
2018-10-09 22:36:10 +00:00
|
|
|
false,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"when secure backend, stickeness and dynamic config enabled": {
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
"proxy_pass https://upstream_balancer;",
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
true,
|
2018-10-08 15:26:06 +00:00
|
|
|
false,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"invalid redirect / to / with dynamic config enabled": {
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
"proxy_pass http://upstream_balancer;",
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
2018-09-14 19:07:57 +00:00
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
false,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"invalid redirect / to /": {
|
|
|
|
"/",
|
|
|
|
"/",
|
|
|
|
"/",
|
2018-10-09 22:36:10 +00:00
|
|
|
"proxy_pass http://upstream_balancer;",
|
2018-03-18 13:13:41 +00:00
|
|
|
false,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-09-14 19:07:57 +00:00
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect / to /jenkins": {
|
|
|
|
"/",
|
|
|
|
"/jenkins",
|
2018-10-01 17:54:11 +00:00
|
|
|
"~* ^/",
|
2016-05-27 14:58:13 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/(.*)" /jenkins/$1 break;
|
|
|
|
rewrite "(?i)/$" /jenkins/ break;
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
false,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect /something to /": {
|
|
|
|
"/something",
|
|
|
|
"/",
|
2018-10-04 14:58:38 +00:00
|
|
|
`~* "^/something\/?(?<baseuri>.*)"`,
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/something/(.*)" /$1 break;
|
|
|
|
rewrite "(?i)/something$" / break;
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
false,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect /end-with-slash/ to /not-root": {
|
|
|
|
"/end-with-slash/",
|
|
|
|
"/not-root",
|
2018-10-04 14:58:38 +00:00
|
|
|
`~* "^/end-with-slash/(?<baseuri>.*)"`,
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/end-with-slash/(.*)" /not-root/$1 break;
|
|
|
|
rewrite "(?i)/end-with-slash/$" /not-root/ break;
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
false,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect /something-complex to /not-root": {
|
|
|
|
"/something-complex",
|
|
|
|
"/not-root",
|
2018-10-04 14:58:38 +00:00
|
|
|
`~* "^/something-complex\/?(?<baseuri>.*)"`,
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/something-complex/(.*)" /not-root/$1 break;
|
|
|
|
rewrite "(?i)/something-complex$" /not-root/ break;
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
false,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect / to /jenkins and rewrite": {
|
|
|
|
"/",
|
|
|
|
"/jenkins",
|
2018-10-01 17:54:11 +00:00
|
|
|
"~* ^/",
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/(.*)" /jenkins/$1 break;
|
|
|
|
rewrite "(?i)/$" /jenkins/ break;
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
|
|
|
|
set_escape_uri $escaped_base_uri $baseuri;
|
|
|
|
subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="$scheme://$http_host/$escaped_base_uri">' ro;
|
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
true,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect /something to / and rewrite": {
|
|
|
|
"/something",
|
|
|
|
"/",
|
2018-10-04 14:58:38 +00:00
|
|
|
`~* "^/something\/?(?<baseuri>.*)"`,
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/something/(.*)" /$1 break;
|
|
|
|
rewrite "(?i)/something$" / break;
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
|
|
|
|
set_escape_uri $escaped_base_uri $baseuri;
|
|
|
|
subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="$scheme://$http_host/something/$escaped_base_uri">' ro;
|
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
true,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect /end-with-slash/ to /not-root and rewrite": {
|
|
|
|
"/end-with-slash/",
|
|
|
|
"/not-root",
|
2018-10-04 14:58:38 +00:00
|
|
|
`~* "^/end-with-slash/(?<baseuri>.*)"`,
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/end-with-slash/(.*)" /not-root/$1 break;
|
|
|
|
rewrite "(?i)/end-with-slash/$" /not-root/ break;
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
|
|
|
|
set_escape_uri $escaped_base_uri $baseuri;
|
|
|
|
subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="$scheme://$http_host/end-with-slash/$escaped_base_uri">' ro;
|
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
true,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect /something-complex to /not-root and rewrite": {
|
|
|
|
"/something-complex",
|
|
|
|
"/not-root",
|
2018-10-04 14:58:38 +00:00
|
|
|
`~* "^/something-complex\/?(?<baseuri>.*)"`,
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/something-complex/(.*)" /not-root/$1 break;
|
|
|
|
rewrite "(?i)/something-complex$" /not-root/ break;
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
|
|
|
|
set_escape_uri $escaped_base_uri $baseuri;
|
|
|
|
subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="$scheme://$http_host/something-complex/$escaped_base_uri">' ro;
|
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
true,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect /something to / and rewrite with specific scheme": {
|
|
|
|
"/something",
|
|
|
|
"/",
|
2018-10-04 14:58:38 +00:00
|
|
|
`~* "^/something\/?(?<baseuri>.*)"`,
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/something/(.*)" /$1 break;
|
|
|
|
rewrite "(?i)/something$" / break;
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
|
|
|
|
set_escape_uri $escaped_base_uri $baseuri;
|
|
|
|
subs_filter '(<(?:H|h)(?:E|e)(?:A|a)(?:D|d)(?:[^">]|"[^"]*")*>)' '$1<base href="http://$http_host/something/$escaped_base_uri">' ro;
|
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
true,
|
|
|
|
"http",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect / to /something with sticky enabled": {
|
|
|
|
"/",
|
|
|
|
"/something",
|
2018-10-01 17:54:11 +00:00
|
|
|
`~* ^/`,
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/(.*)" /something/$1 break;
|
|
|
|
rewrite "(?i)/$" /something/ break;
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
false,
|
|
|
|
"http",
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"redirect / to /something with sticky and dynamic config enabled": {
|
|
|
|
"/",
|
|
|
|
"/something",
|
2018-10-01 17:54:11 +00:00
|
|
|
`~* ^/`,
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/(.*)" /something/$1 break;
|
|
|
|
rewrite "(?i)/$" /something/ break;
|
2018-04-05 23:21:35 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
false,
|
|
|
|
"http",
|
|
|
|
true,
|
|
|
|
false,
|
2018-09-14 19:07:57 +00:00
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-03-18 13:13:41 +00:00
|
|
|
"add the X-Forwarded-Prefix header": {
|
|
|
|
"/there",
|
|
|
|
"/something",
|
2018-10-04 14:58:38 +00:00
|
|
|
`~* "^/there\/?(?<baseuri>.*)"`,
|
2018-03-18 13:13:41 +00:00
|
|
|
`
|
2018-10-04 14:58:38 +00:00
|
|
|
rewrite "(?i)/there/(.*)" /something/$1 break;
|
|
|
|
rewrite "(?i)/there$" /something/ break;
|
2018-04-05 23:21:35 +00:00
|
|
|
proxy_set_header X-Forwarded-Prefix "/there/";
|
2018-10-09 22:36:10 +00:00
|
|
|
proxy_pass http://upstream_balancer;
|
2018-04-05 23:21:35 +00:00
|
|
|
`,
|
2018-03-18 13:13:41 +00:00
|
|
|
false,
|
|
|
|
"http",
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2018-10-01 17:54:11 +00:00
|
|
|
"use ~* location modifier when ingress does not use rewrite/regex target but at least one other ingress does": {
|
2018-09-14 19:07:57 +00:00
|
|
|
"/something",
|
|
|
|
"/something",
|
2018-10-04 14:58:38 +00:00
|
|
|
`~* "^/something"`,
|
2018-10-09 22:36:10 +00:00
|
|
|
"proxy_pass http://upstream_balancer;",
|
2018-09-14 19:07:57 +00:00
|
|
|
false,
|
|
|
|
"",
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
false,
|
2018-10-08 15:26:06 +00:00
|
|
|
true,
|
|
|
|
},
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2018-04-08 20:37:13 +00:00
|
|
|
func TestBuildLuaSharedDictionaries(t *testing.T) {
|
|
|
|
servers := []*ingress.Server{
|
|
|
|
{
|
|
|
|
Hostname: "foo.bar",
|
|
|
|
Locations: []*ingress.Location{{Path: "/", LuaRestyWAF: luarestywaf.Config{}}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Hostname: "another.host",
|
|
|
|
Locations: []*ingress.Location{{Path: "/", LuaRestyWAF: luarestywaf.Config{}}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:36:10 +00:00
|
|
|
config := buildLuaSharedDictionaries(servers, false)
|
2018-04-08 20:37:13 +00:00
|
|
|
if !strings.Contains(config, "lua_shared_dict configuration_data") {
|
|
|
|
t.Errorf("expected to include 'configuration_data' but got %s", config)
|
|
|
|
}
|
|
|
|
if strings.Contains(config, "waf_storage") {
|
|
|
|
t.Errorf("expected to not include 'waf_storage' but got %s", config)
|
|
|
|
}
|
|
|
|
|
2018-04-09 12:19:13 +00:00
|
|
|
servers[1].Locations[0].LuaRestyWAF = luarestywaf.Config{Mode: "ACTIVE"}
|
2018-10-09 22:36:10 +00:00
|
|
|
config = buildLuaSharedDictionaries(servers, false)
|
2018-04-08 20:37:13 +00:00
|
|
|
if !strings.Contains(config, "lua_shared_dict waf_storage") {
|
|
|
|
t.Errorf("expected to configure 'waf_storage', but got %s", config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-09 03:11:00 +00:00
|
|
|
func TestFormatIP(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Input, Output string
|
|
|
|
}{
|
|
|
|
"ipv4-localhost": {"127.0.0.1", "127.0.0.1"},
|
|
|
|
"ipv4-internet": {"8.8.8.8", "8.8.8.8"},
|
|
|
|
"ipv6-localhost": {"::1", "[::1]"},
|
|
|
|
"ipv6-internet": {"2001:4860:4860::8888", "[2001:4860:4860::8888]"},
|
|
|
|
"invalid-ip": {"nonsense", "nonsense"},
|
|
|
|
"empty-ip": {"", ""},
|
|
|
|
}
|
|
|
|
for k, tc := range cases {
|
|
|
|
res := formatIP(tc.Input)
|
|
|
|
if res != tc.Output {
|
|
|
|
t.Errorf("%s: called formatIp('%s'); expected '%v' but returned '%v'", k, tc.Input, tc.Output, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 21:04:34 +00:00
|
|
|
func TestBuildLocation(t *testing.T) {
|
|
|
|
for k, tc := range tmplFuncTestcases {
|
2016-08-07 22:53:08 +00:00
|
|
|
loc := &ingress.Location{
|
2017-08-19 21:13:02 +00:00
|
|
|
Path: tc.Path,
|
2017-11-07 16:36:51 +00:00
|
|
|
Rewrite: rewrite.Config{Target: tc.Target, AddBaseURL: tc.AddBaseURL},
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-01 17:54:11 +00:00
|
|
|
newLoc := buildLocation(loc, tc.enforceRegex)
|
2016-05-25 21:04:34 +00:00
|
|
|
if tc.Location != newLoc {
|
2016-05-27 14:58:13 +00:00
|
|
|
t.Errorf("%s: expected '%v' but returned %v", k, tc.Location, newLoc)
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildProxyPass(t *testing.T) {
|
2017-08-23 16:11:33 +00:00
|
|
|
defaultBackend := "upstream-name"
|
|
|
|
defaultHost := "example.com"
|
|
|
|
|
2016-05-25 21:04:34 +00:00
|
|
|
for k, tc := range tmplFuncTestcases {
|
2016-08-07 22:53:08 +00:00
|
|
|
loc := &ingress.Location{
|
2017-12-06 20:11:18 +00:00
|
|
|
Path: tc.Path,
|
|
|
|
Rewrite: rewrite.Config{Target: tc.Target, AddBaseURL: tc.AddBaseURL, BaseURLScheme: tc.BaseURLScheme},
|
|
|
|
Backend: defaultBackend,
|
|
|
|
XForwardedPrefix: tc.XForwardedPrefix,
|
2017-08-23 16:11:33 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 15:26:06 +00:00
|
|
|
if tc.SecureBackend {
|
|
|
|
loc.BackendProtocol = "HTTPS"
|
|
|
|
}
|
|
|
|
|
2018-03-18 13:13:41 +00:00
|
|
|
backend := &ingress.Backend{
|
2018-10-08 15:26:06 +00:00
|
|
|
Name: defaultBackend,
|
2018-03-18 13:13:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-23 16:11:33 +00:00
|
|
|
if tc.Sticky {
|
2018-03-18 13:13:41 +00:00
|
|
|
backend.SessionAffinity = ingress.SessionAffinityConfig{
|
|
|
|
AffinityType: "cookie",
|
|
|
|
CookieSessionAffinity: ingress.CookieSessionAffinity{
|
|
|
|
Locations: map[string][]string{
|
|
|
|
defaultHost: {tc.Path},
|
2017-08-23 16:11:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-18 13:13:41 +00:00
|
|
|
backends := []*ingress.Backend{backend}
|
|
|
|
|
2018-10-09 22:36:10 +00:00
|
|
|
pp := buildProxyPass(defaultHost, backends, loc)
|
2016-05-27 14:58:13 +00:00
|
|
|
if !strings.EqualFold(tc.ProxyPass, pp) {
|
|
|
|
t.Errorf("%s: expected \n'%v'\nbut returned \n'%v'", k, tc.ProxyPass, pp)
|
2016-05-25 21:04:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-16 18:24:26 +00:00
|
|
|
|
2018-01-28 00:32:08 +00:00
|
|
|
func TestBuildAuthLocation(t *testing.T) {
|
|
|
|
authURL := "foo.com/auth"
|
|
|
|
|
|
|
|
loc := &ingress.Location{
|
|
|
|
ExternalAuth: authreq.Config{
|
|
|
|
URL: authURL,
|
|
|
|
},
|
|
|
|
Path: "/cat",
|
|
|
|
}
|
|
|
|
|
|
|
|
str := buildAuthLocation(loc)
|
|
|
|
|
|
|
|
encodedAuthURL := strings.Replace(base64.URLEncoding.EncodeToString([]byte(loc.Path)), "=", "", -1)
|
|
|
|
expected := fmt.Sprintf("/_external-auth-%v", encodedAuthURL)
|
|
|
|
|
|
|
|
if str != expected {
|
|
|
|
t.Errorf("Expected \n'%v'\nbut returned \n'%v'", expected, str)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 00:43:15 +00:00
|
|
|
func TestBuildAuthResponseHeaders(t *testing.T) {
|
|
|
|
loc := &ingress.Location{
|
2017-11-07 16:36:51 +00:00
|
|
|
ExternalAuth: authreq.Config{ResponseHeaders: []string{"h1", "H-With-Caps-And-Dashes"}},
|
2017-02-04 00:43:15 +00:00
|
|
|
}
|
|
|
|
headers := buildAuthResponseHeaders(loc)
|
|
|
|
expected := []string{
|
|
|
|
"auth_request_set $authHeader0 $upstream_http_h1;",
|
|
|
|
"proxy_set_header 'h1' $authHeader0;",
|
|
|
|
"auth_request_set $authHeader1 $upstream_http_h_with_caps_and_dashes;",
|
|
|
|
"proxy_set_header 'H-With-Caps-And-Dashes' $authHeader1;",
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(expected, headers) {
|
|
|
|
t.Errorf("Expected \n'%v'\nbut returned \n'%v'", expected, headers)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 18:24:26 +00:00
|
|
|
func TestTemplateWithData(t *testing.T) {
|
|
|
|
pwd, _ := os.Getwd()
|
2017-11-05 01:18:28 +00:00
|
|
|
f, err := os.Open(path.Join(pwd, "../../../../test/data/config.json"))
|
2016-11-16 18:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("unexpected error reading json file: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
data, err := ioutil.ReadFile(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
t.Error("unexpected error reading json file: ", err)
|
|
|
|
}
|
|
|
|
var dat config.TemplateConfig
|
2018-09-22 17:25:01 +00:00
|
|
|
if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, &dat); err != nil {
|
2016-11-16 18:24:26 +00:00
|
|
|
t.Errorf("unexpected error unmarshalling json: %v", err)
|
|
|
|
}
|
2017-08-24 13:33:26 +00:00
|
|
|
if dat.ListenPorts == nil {
|
|
|
|
dat.ListenPorts = &config.ListenPorts{}
|
|
|
|
}
|
2017-11-22 13:40:54 +00:00
|
|
|
|
|
|
|
fs, err := file.NewFakeFS()
|
2016-11-16 18:24:26 +00:00
|
|
|
if err != nil {
|
2017-11-22 13:40:54 +00:00
|
|
|
t.Fatalf("unexpected error: %v", err)
|
2016-11-16 18:24:26 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 13:40:54 +00:00
|
|
|
ngxTpl, err := NewTemplate("/etc/nginx/template/nginx.tmpl", fs)
|
2016-11-16 18:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("invalid NGINX template: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-05-16 18:08:43 +00:00
|
|
|
rt, err := ngxTpl.Write(dat)
|
2016-11-16 18:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("invalid NGINX template: %v", err)
|
|
|
|
}
|
2018-05-16 18:08:43 +00:00
|
|
|
|
|
|
|
if !strings.Contains(string(rt), "listen [2001:db8:a0b:12f0::1]") {
|
|
|
|
t.Errorf("invalid NGINX template, expected IPV6 listen address not present")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(string(rt), "listen [3731:54:65fe:2::a7]") {
|
|
|
|
t.Errorf("invalid NGINX template, expected IPV6 listen address not present")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(string(rt), "listen 2.2.2.2") {
|
|
|
|
t.Errorf("invalid NGINX template, expected IPV4 listen address not present")
|
|
|
|
}
|
2016-11-16 18:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTemplateWithData(b *testing.B) {
|
|
|
|
pwd, _ := os.Getwd()
|
2017-11-05 01:18:28 +00:00
|
|
|
f, err := os.Open(path.Join(pwd, "../../../../test/data/config.json"))
|
2016-11-16 18:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Errorf("unexpected error reading json file: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
data, err := ioutil.ReadFile(f.Name())
|
|
|
|
if err != nil {
|
|
|
|
b.Error("unexpected error reading json file: ", err)
|
|
|
|
}
|
|
|
|
var dat config.TemplateConfig
|
2018-09-22 17:25:01 +00:00
|
|
|
if err := jsoniter.ConfigCompatibleWithStandardLibrary.Unmarshal(data, &dat); err != nil {
|
2016-11-16 18:24:26 +00:00
|
|
|
b.Errorf("unexpected error unmarshalling json: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-11-22 13:40:54 +00:00
|
|
|
fs, err := file.NewFakeFS()
|
2016-11-16 18:24:26 +00:00
|
|
|
if err != nil {
|
2017-11-22 13:40:54 +00:00
|
|
|
b.Fatalf("unexpected error: %v", err)
|
2016-11-16 18:24:26 +00:00
|
|
|
}
|
|
|
|
|
2017-11-22 13:40:54 +00:00
|
|
|
ngxTpl, err := NewTemplate("/etc/nginx/template/nginx.tmpl", fs)
|
2016-11-16 18:24:26 +00:00
|
|
|
if err != nil {
|
|
|
|
b.Errorf("invalid NGINX template: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2017-02-20 02:34:05 +00:00
|
|
|
ngxTpl.Write(dat)
|
2016-11-16 18:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-02 03:30:22 +00:00
|
|
|
|
|
|
|
func TestBuildDenyVariable(t *testing.T) {
|
|
|
|
a := buildDenyVariable("host1.example.com_/.well-known/acme-challenge")
|
|
|
|
b := buildDenyVariable("host1.example.com_/.well-known/acme-challenge")
|
|
|
|
if !reflect.DeepEqual(a, b) {
|
|
|
|
t.Errorf("Expected '%v' but returned '%v'", a, b)
|
|
|
|
}
|
|
|
|
}
|
2017-08-23 04:57:35 +00:00
|
|
|
|
2018-11-09 14:45:11 +00:00
|
|
|
func TestBuildByteSize(t *testing.T) {
|
2018-11-30 01:45:32 +00:00
|
|
|
cases := []struct {
|
2018-12-02 18:10:36 +00:00
|
|
|
value interface{}
|
|
|
|
isOffset bool
|
2018-11-30 01:45:32 +00:00
|
|
|
expected bool
|
|
|
|
}{
|
2018-12-02 18:10:36 +00:00
|
|
|
{"1000", false, true},
|
|
|
|
{"1000k", false, true},
|
|
|
|
{"1m", false, true},
|
|
|
|
{"10g", false, false},
|
|
|
|
{" 1m ", false, true},
|
|
|
|
{"1000kk", false, false},
|
|
|
|
{"1000km", false, false},
|
|
|
|
{"1mm", false, false},
|
|
|
|
{nil, false, false},
|
|
|
|
{"", false, false},
|
|
|
|
{" ", false, false},
|
|
|
|
{"1G", true, true},
|
|
|
|
{"1000kk", true, false},
|
|
|
|
{"", true, false},
|
2018-11-30 01:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
2018-12-02 18:10:36 +00:00
|
|
|
val := isValidByteSize(tc.value, tc.isOffset)
|
2018-11-30 01:45:32 +00:00
|
|
|
if tc.expected != val {
|
|
|
|
t.Errorf("Expected '%v' but returned '%v'", tc.expected, val)
|
|
|
|
}
|
2017-08-23 04:57:35 +00:00
|
|
|
}
|
|
|
|
}
|
2017-09-09 05:10:38 +00:00
|
|
|
|
|
|
|
func TestIsLocationAllowed(t *testing.T) {
|
|
|
|
loc := ingress.Location{
|
|
|
|
Denied: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
isAllowed := isLocationAllowed(&loc)
|
|
|
|
if !isAllowed {
|
|
|
|
t.Errorf("Expected '%v' but returned '%v'", true, isAllowed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildForwardedFor(t *testing.T) {
|
|
|
|
inputStr := "X-Forwarded-For"
|
|
|
|
outputStr := buildForwardedFor(inputStr)
|
|
|
|
|
|
|
|
validStr := "$http_x_forwarded_for"
|
|
|
|
|
|
|
|
if outputStr != validStr {
|
|
|
|
t.Errorf("Expected '%v' but returned '%v'", validStr, outputStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 02:27:10 +00:00
|
|
|
func TestBuildResolversForLua(t *testing.T) {
|
|
|
|
ipOne := net.ParseIP("192.0.0.1")
|
|
|
|
ipTwo := net.ParseIP("2001:db8:1234:0000:0000:0000:0000:0000")
|
|
|
|
ipList := []net.IP{ipOne, ipTwo}
|
|
|
|
|
|
|
|
expected := "\"192.0.0.1\", \"2001:db8:1234::\""
|
|
|
|
actual := buildResolversForLua(ipList, false)
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("Expected '%v' but returned '%v'", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = "\"192.0.0.1\""
|
|
|
|
actual = buildResolversForLua(ipList, true)
|
|
|
|
|
|
|
|
if expected != actual {
|
|
|
|
t.Errorf("Expected '%v' but returned '%v'", expected, actual)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 05:10:38 +00:00
|
|
|
func TestBuildResolvers(t *testing.T) {
|
|
|
|
ipOne := net.ParseIP("192.0.0.1")
|
|
|
|
ipTwo := net.ParseIP("2001:db8:1234:0000:0000:0000:0000:0000")
|
|
|
|
ipList := []net.IP{ipOne, ipTwo}
|
|
|
|
|
|
|
|
validResolver := "resolver 192.0.0.1 [2001:db8:1234::] valid=30s;"
|
2018-02-02 19:53:28 +00:00
|
|
|
resolver := buildResolvers(ipList, false)
|
|
|
|
|
|
|
|
if resolver != validResolver {
|
|
|
|
t.Errorf("Expected '%v' but returned '%v'", validResolver, resolver)
|
|
|
|
}
|
|
|
|
|
|
|
|
validResolver = "resolver 192.0.0.1 valid=30s ipv6=off;"
|
|
|
|
resolver = buildResolvers(ipList, true)
|
2017-09-09 05:10:38 +00:00
|
|
|
|
|
|
|
if resolver != validResolver {
|
|
|
|
t.Errorf("Expected '%v' but returned '%v'", validResolver, resolver)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildNextUpstream(t *testing.T) {
|
2017-10-10 10:18:45 +00:00
|
|
|
cases := map[string]struct {
|
|
|
|
NextUpstream string
|
|
|
|
NonIdempotent bool
|
|
|
|
Output string
|
|
|
|
}{
|
|
|
|
"default": {
|
|
|
|
"timeout http_500 http_502",
|
|
|
|
false,
|
|
|
|
"timeout http_500 http_502",
|
|
|
|
},
|
|
|
|
"global": {
|
|
|
|
"timeout http_500 http_502",
|
|
|
|
true,
|
|
|
|
"timeout http_500 http_502 non_idempotent",
|
|
|
|
},
|
|
|
|
"local": {
|
|
|
|
"timeout http_500 http_502 non_idempotent",
|
|
|
|
false,
|
|
|
|
"timeout http_500 http_502 non_idempotent",
|
|
|
|
},
|
|
|
|
}
|
2017-09-09 05:10:38 +00:00
|
|
|
|
2017-10-10 10:18:45 +00:00
|
|
|
for k, tc := range cases {
|
|
|
|
nextUpstream := buildNextUpstream(tc.NextUpstream, tc.NonIdempotent)
|
|
|
|
if nextUpstream != tc.Output {
|
|
|
|
t.Errorf(
|
|
|
|
"%s: called buildNextUpstream('%s', %v); expected '%v' but returned '%v'",
|
|
|
|
k,
|
|
|
|
tc.NextUpstream,
|
|
|
|
tc.NonIdempotent,
|
|
|
|
tc.Output,
|
|
|
|
nextUpstream,
|
|
|
|
)
|
|
|
|
}
|
2017-09-09 05:10:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildRateLimit(t *testing.T) {
|
2017-10-01 00:48:14 +00:00
|
|
|
loc := &ingress.Location{}
|
2017-09-09 05:10:38 +00:00
|
|
|
|
|
|
|
loc.RateLimit.Connections.Name = "con"
|
|
|
|
loc.RateLimit.Connections.Limit = 1
|
|
|
|
|
|
|
|
loc.RateLimit.RPS.Name = "rps"
|
|
|
|
loc.RateLimit.RPS.Limit = 1
|
|
|
|
loc.RateLimit.RPS.Burst = 1
|
|
|
|
|
|
|
|
loc.RateLimit.RPM.Name = "rpm"
|
|
|
|
loc.RateLimit.RPM.Limit = 2
|
|
|
|
loc.RateLimit.RPM.Burst = 2
|
|
|
|
|
|
|
|
loc.RateLimit.LimitRateAfter = 1
|
|
|
|
loc.RateLimit.LimitRate = 1
|
|
|
|
|
|
|
|
validLimits := []string{
|
|
|
|
"limit_conn con 1;",
|
|
|
|
"limit_req zone=rps burst=1 nodelay;",
|
|
|
|
"limit_req zone=rpm burst=2 nodelay;",
|
|
|
|
"limit_rate_after 1k;",
|
|
|
|
"limit_rate 1k;",
|
|
|
|
}
|
|
|
|
|
|
|
|
limits := buildRateLimit(loc)
|
|
|
|
|
|
|
|
for i, limit := range limits {
|
|
|
|
if limit != validLimits[i] {
|
|
|
|
t.Errorf("Expected '%v' but returned '%v'", validLimits, limits)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-05 04:55:42 +00:00
|
|
|
|
|
|
|
func TestBuildAuthSignURL(t *testing.T) {
|
|
|
|
cases := map[string]struct {
|
|
|
|
Input, Output string
|
|
|
|
}{
|
2018-07-19 06:22:05 +00:00
|
|
|
"default url": {"http://google.com", "http://google.com?rd=$pass_access_scheme://$http_host$escaped_request_uri"},
|
|
|
|
"with random field": {"http://google.com?cat=0", "http://google.com?cat=0&rd=$pass_access_scheme://$http_host$escaped_request_uri"},
|
2017-10-05 04:55:42 +00:00
|
|
|
"with rd field": {"http://google.com?cat&rd=$request", "http://google.com?cat&rd=$request"},
|
|
|
|
}
|
|
|
|
for k, tc := range cases {
|
|
|
|
res := buildAuthSignURL(tc.Input)
|
|
|
|
if res != tc.Output {
|
|
|
|
t.Errorf("%s: called buildAuthSignURL('%s'); expected '%v' but returned '%v'", k, tc.Input, tc.Output, res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-18 20:44:59 +00:00
|
|
|
|
|
|
|
func TestIsLocationInLocationList(t *testing.T) {
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
location *ingress.Location
|
|
|
|
rawLocationList string
|
|
|
|
expected bool
|
|
|
|
}{
|
|
|
|
{&ingress.Location{Path: "/match"}, "/match", true},
|
|
|
|
{&ingress.Location{Path: "/match"}, ",/match", true},
|
|
|
|
{&ingress.Location{Path: "/match"}, "/dontmatch", false},
|
|
|
|
{&ingress.Location{Path: "/match"}, ",/dontmatch", false},
|
|
|
|
{&ingress.Location{Path: "/match"}, "/dontmatch,/match", true},
|
|
|
|
{&ingress.Location{Path: "/match"}, "/dontmatch,/dontmatcheither", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, testCase := range testCases {
|
|
|
|
result := isLocationInLocationList(testCase.location, testCase.rawLocationList)
|
|
|
|
if result != testCase.expected {
|
|
|
|
t.Errorf(" expected %v but return %v, path: '%s', rawLocation: '%s'", testCase.expected, result, testCase.location.Path, testCase.rawLocationList)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-12 18:01:46 +00:00
|
|
|
|
|
|
|
func TestBuildUpstreamName(t *testing.T) {
|
|
|
|
defaultBackend := "upstream-name"
|
|
|
|
defaultHost := "example.com"
|
|
|
|
|
|
|
|
for k, tc := range tmplFuncTestcases {
|
|
|
|
loc := &ingress.Location{
|
|
|
|
Path: tc.Path,
|
|
|
|
Rewrite: rewrite.Config{Target: tc.Target, AddBaseURL: tc.AddBaseURL, BaseURLScheme: tc.BaseURLScheme},
|
|
|
|
Backend: defaultBackend,
|
|
|
|
XForwardedPrefix: tc.XForwardedPrefix,
|
|
|
|
}
|
|
|
|
|
2018-10-08 15:26:06 +00:00
|
|
|
if tc.SecureBackend {
|
|
|
|
loc.BackendProtocol = "HTTPS"
|
|
|
|
}
|
|
|
|
|
2018-04-12 18:01:46 +00:00
|
|
|
backend := &ingress.Backend{
|
2018-10-08 15:26:06 +00:00
|
|
|
Name: defaultBackend,
|
2018-04-12 18:01:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expected := defaultBackend
|
|
|
|
|
|
|
|
if tc.Sticky {
|
|
|
|
backend.SessionAffinity = ingress.SessionAffinityConfig{
|
|
|
|
AffinityType: "cookie",
|
|
|
|
CookieSessionAffinity: ingress.CookieSessionAffinity{
|
|
|
|
Locations: map[string][]string{
|
|
|
|
defaultHost: {tc.Path},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:36:10 +00:00
|
|
|
pp := buildUpstreamName(loc)
|
2018-04-12 18:01:46 +00:00
|
|
|
if !strings.EqualFold(expected, pp) {
|
|
|
|
t.Errorf("%s: expected \n'%v'\nbut returned \n'%v'", k, expected, pp)
|
|
|
|
}
|
|
|
|
}
|
2018-04-12 18:14:43 +00:00
|
|
|
}
|
2018-10-03 22:05:12 +00:00
|
|
|
|
2018-10-09 19:58:50 +00:00
|
|
|
func TestEscapeLiteralDollar(t *testing.T) {
|
|
|
|
escapedPath := escapeLiteralDollar("/$")
|
2018-10-03 22:05:12 +00:00
|
|
|
expected := "/${literal_dollar}"
|
|
|
|
if escapedPath != expected {
|
2018-10-09 19:58:50 +00:00
|
|
|
t.Errorf("Expected %v but returned %v", expected, escapedPath)
|
2018-10-03 22:05:12 +00:00
|
|
|
}
|
2018-10-09 19:58:50 +00:00
|
|
|
|
|
|
|
escapedPath = escapeLiteralDollar("/hello-$/world-$/")
|
|
|
|
expected = "/hello-${literal_dollar}/world-${literal_dollar}/"
|
|
|
|
if escapedPath != expected {
|
|
|
|
t.Errorf("Expected %v but returned %v", expected, escapedPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
leaveUnchagned := "/leave-me/unchagned"
|
|
|
|
escapedPath = escapeLiteralDollar(leaveUnchagned)
|
|
|
|
if escapedPath != leaveUnchagned {
|
|
|
|
t.Errorf("Expected %v but returned %v", leaveUnchagned, escapedPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
escapedPath = escapeLiteralDollar(false)
|
2018-10-03 22:05:12 +00:00
|
|
|
expected = ""
|
|
|
|
if escapedPath != expected {
|
2018-10-09 19:58:50 +00:00
|
|
|
t.Errorf("Expected %v but returned %v", expected, escapedPath)
|
2018-10-03 22:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-27 13:35:29 +00:00
|
|
|
|
2018-12-04 19:59:54 +00:00
|
|
|
func TestOpentracingPropagateContext(t *testing.T) {
|
2018-11-27 13:35:29 +00:00
|
|
|
tests := map[interface{}]string{
|
|
|
|
&ingress.Location{BackendProtocol: "HTTP"}: "opentracing_propagate_context",
|
|
|
|
&ingress.Location{BackendProtocol: "HTTPS"}: "opentracing_propagate_context",
|
|
|
|
&ingress.Location{BackendProtocol: "GRPC"}: "opentracing_grpc_propagate_context",
|
|
|
|
&ingress.Location{BackendProtocol: "GRPCS"}: "opentracing_grpc_propagate_context",
|
|
|
|
&ingress.Location{BackendProtocol: "AJP"}: "opentracing_propagate_context",
|
|
|
|
"not a location": "opentracing_propagate_context",
|
|
|
|
}
|
|
|
|
|
|
|
|
for loc, expectedDirective := range tests {
|
|
|
|
actualDirective := opentracingPropagateContext(loc)
|
|
|
|
if actualDirective != expectedDirective {
|
|
|
|
t.Errorf("Expected %v but returned %v", expectedDirective, actualDirective)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|