forked from juju/utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp_test.go
156 lines (130 loc) · 4.43 KB
/
http_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2013 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package utils_test
import (
"encoding/base64"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/http/httptest"
"os"
"strings"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "launchpad.net/gocheck"
"github.com/juju/utils"
)
func init() {
// The default Proxy implementation for HTTP transports,
// ProxyFromEnvironment, uses a sync.Once in Go 1.3 onwards.
// No tests should be dialing out, so no proxy should be
// used.
os.Setenv("http_proxy", "")
os.Setenv("HTTP_PROXY", "")
}
type httpSuite struct {
testing.IsolationSuite
Server *httptest.Server
}
var _ = gc.Suite(&httpSuite{})
type trivialResponseHandler struct{}
func (t *trivialResponseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Greetings!\n")
}
func (s *httpSuite) SetUpTest(c *gc.C) {
s.IsolationSuite.SetUpTest(c)
s.Server = httptest.NewTLSServer(&trivialResponseHandler{})
}
func (s *httpSuite) TearDownTest(c *gc.C) {
if s.Server != nil {
s.Server.Close()
}
s.IsolationSuite.TearDownTest(c)
}
func (s *httpSuite) TestDefaultClientFails(c *gc.C) {
_, err := http.Get(s.Server.URL)
c.Assert(err, gc.ErrorMatches, "(.|\n)*x509: certificate signed by unknown authority")
}
func (s *httpSuite) TestValidatingClientGetter(c *gc.C) {
client1 := utils.GetValidatingHTTPClient()
client2 := utils.GetHTTPClient(utils.VerifySSLHostnames)
c.Check(client1, gc.Equals, client2)
}
func (s *httpSuite) TestNonValidatingClientGetter(c *gc.C) {
client1 := utils.GetNonValidatingHTTPClient()
client2 := utils.GetHTTPClient(utils.NoVerifySSLHostnames)
c.Check(client1, gc.Equals, client2)
}
func (s *httpSuite) TestValidatingClientFails(c *gc.C) {
client := utils.GetValidatingHTTPClient()
_, err := client.Get(s.Server.URL)
c.Assert(err, gc.ErrorMatches, "(.|\n)*x509: certificate signed by unknown authority")
}
func (s *httpSuite) TestInsecureClientSucceeds(c *gc.C) {
response, err := utils.GetNonValidatingHTTPClient().Get(s.Server.URL)
c.Assert(err, gc.IsNil)
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
c.Assert(err, gc.IsNil)
c.Check(string(body), gc.Equals, "Greetings!\n")
}
func (s *httpSuite) TestInsecureClientCached(c *gc.C) {
client1 := utils.GetNonValidatingHTTPClient()
client2 := utils.GetNonValidatingHTTPClient()
c.Check(client1, gc.Equals, client2)
}
func (s *httpSuite) TestBasicAuthHeader(c *gc.C) {
header := utils.BasicAuthHeader("eric", "sekrit")
c.Assert(len(header), gc.Equals, 1)
auth := header.Get("Authorization")
fields := strings.Fields(auth)
c.Assert(len(fields), gc.Equals, 2)
basic, encoded := fields[0], fields[1]
c.Assert(basic, gc.Equals, "Basic")
decoded, err := base64.StdEncoding.DecodeString(encoded)
c.Assert(err, gc.IsNil)
c.Assert(string(decoded), gc.Equals, "eric:sekrit")
}
type dialSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&dialSuite{})
func (s *dialSuite) TestDialRejectsNonLocal(c *gc.C) {
s.PatchValue(&utils.OutgoingAccessAllowed, false)
_, err := utils.Dial("tcp", "10.0.0.1:80")
c.Assert(err, gc.ErrorMatches, `access to address "10.0.0.1:80" not allowed`)
_, err = utils.Dial("tcp", "somehost:80")
c.Assert(err, gc.ErrorMatches, `access to address "somehost:80" not allowed`)
}
func (s *dialSuite) assertDial(c *gc.C, addr string) {
dialed := false
s.PatchValue(utils.NetDial, func(network, addr string) (net.Conn, error) {
c.Assert(network, gc.Equals, "tcp")
c.Assert(addr, gc.Equals, addr)
dialed = true
return nil, nil
})
_, err := utils.Dial("tcp", addr)
c.Assert(err, gc.IsNil)
c.Assert(dialed, jc.IsTrue)
}
func (s *dialSuite) TestDialAllowsNonLocal(c *gc.C) {
s.PatchValue(&utils.OutgoingAccessAllowed, true)
s.assertDial(c, "10.0.0.1:80")
}
func (s *dialSuite) TestDialAllowsLocal(c *gc.C) {
s.PatchValue(&utils.OutgoingAccessAllowed, false)
s.assertDial(c, "127.0.0.1:1234")
s.assertDial(c, "localhost:1234")
}
func (s *dialSuite) TestInsecureClientNoAccess(c *gc.C) {
s.PatchValue(&utils.OutgoingAccessAllowed, false)
_, err := utils.GetNonValidatingHTTPClient().Get("http://10.0.0.1:1234")
c.Assert(err, gc.ErrorMatches, `.*access to address "10.0.0.1:1234" not allowed`)
}
func (s *dialSuite) TestSecureClientNoAccess(c *gc.C) {
s.PatchValue(&utils.OutgoingAccessAllowed, false)
_, err := utils.GetValidatingHTTPClient().Get("http://10.0.0.1:1234")
c.Assert(err, gc.ErrorMatches, `.*access to address "10.0.0.1:1234" not allowed`)
}