-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
resilient_client_test.go
74 lines (58 loc) · 1.71 KB
/
resilient_client_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright © 2022 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package httpx
import (
"net"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"go.opentelemetry.io/otel"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNoPrivateIPs(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("Hello, world!"))
}))
t.Cleanup(ts.Close)
target, err := url.ParseRequestURI(ts.URL)
require.NoError(t, err)
_, port, err := net.SplitHostPort(target.Host)
require.NoError(t, err)
allowed := "http://localhost:" + port + "/foobar"
c := NewResilientClient(
ResilientClientWithMaxRetry(1),
ResilientClientDisallowInternalIPs(),
ResilientClientAllowInternalIPRequestsTo(allowed),
)
for destination, passes := range map[string]bool{
"http://127.0.0.1:" + port: false,
"http://localhost:" + port: false,
"http://192.168.178.5:" + port: false,
allowed: true,
"http://localhost:" + port + "/FOOBAR": false,
} {
_, err := c.Get(destination)
if !passes {
require.Error(t, err)
assert.Contains(t, err.Error(), "is not a public IP address")
} else {
require.NoError(t, err)
}
}
}
func TestClientWithTracer(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
_, _ = w.Write([]byte("Hello, world!"))
}))
t.Cleanup(ts.Close)
tracer := otel.Tracer("github.com/ory/x/httpx test")
c := NewResilientClient(
ResilientClientWithTracer(tracer),
)
target, err := url.ParseRequestURI(ts.URL)
require.NoError(t, err)
_, err = c.Get(target.String())
assert.NoError(t, err)
}