This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathwpad_test.go
103 lines (84 loc) · 1.93 KB
/
wpad_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package e2e
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const expectedPACFile = `function FindProxyForURL(url, host) {
var proxy = "PROXY 127.0.0.1:18070; DIRECT";
var direct = "DIRECT";
// no proxy for local hosts without domain:
if(isPlainHostName(host)) return direct;
// We only proxy http, not even https.
if (
url.substring(0, 4) == "ftp:" ||
url.substring(0, 6) == "rsync:" ||
url.substring(0, 6) == "https:"
)
return direct;
// Commented for debug purposes.
// Use direct connection whenever we have direct network connectivity.
//if (isResolvable(host)) {
// return direct
//}
if (shExpMatch(host, "*.*.internal.example.com")) {
return proxy;
}
return direct;
}`
func TestWinchPAC(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
exit, err := spinup(t, ctx, config{winch: true})
if err != nil {
t.Error(err)
cancel()
return
}
defer func() {
cancel()
<-exit
}()
err = Retry(time.Second, ctx.Done(), func() error {
if err = assertRunning(exit); err != nil {
t.Error(err)
return nil
}
res, err := queryWPAD(ctx)
if err != nil {
return err
}
assert.Equal(t, expectedPACFile, res)
return nil
})
require.NoError(t, err)
}
func assertRunning(unexpectedExit chan error) error {
select {
case err := <-unexpectedExit:
return errors.Wrap(err, "Some process exited unexpectedly")
default:
return nil
}
}
func queryWPAD(ctx context.Context) (string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("http://127.0.0.1:%s/wpad.dat", httpWinchPort), nil)
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(b), nil
}