-
Notifications
You must be signed in to change notification settings - Fork 3
/
jndi_test.go
48 lines (45 loc) · 1.1 KB
/
jndi_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package jndi
import (
"context"
"fmt"
"net"
"net/http"
"net/http/httptest"
"testing"
)
func TestSubst(t *testing.T) {
envVar := map[string]string{
"USER": "alice",
"HOME": "/home/alice",
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "got-path=%v", r.URL.Path)
}))
defer ts.Close()
e := env{
getEnv: func(k string) string { return envVar[k] },
transport: &http.Transport{
DialContext: func(ctx context.Context, netw, addr string) (net.Conn, error) {
c, err := net.Dial("tcp", ts.Listener.Addr().String())
if err != nil {
t.Errorf("dial error: %v", err)
}
return c, err
},
},
}
tests := []struct {
in, want string
}{
{"foo${env:user}bar", "fooalicebar"},
{"lower ${lower:FOO}", "lower foo"},
{"upper ${upper:foo}", "upper FOO"},
{"nested ${env:${lower:u}ser}", "nested alice"},
{"hit network: ${jndi:ldap://foo.com/bar}", "hit network: got-path=/bar"},
}
for _, tt := range tests {
if got := e.subst(tt.in); got != tt.want {
t.Errorf("subst(%q) = %q; want %q", tt.in, got, tt.want)
}
}
}