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 pathhttp_test.go
181 lines (149 loc) · 4.66 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package e2e
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"testing"
"time"
"github.com/stretchr/testify/require"
)
// TestHTTPEndpointCallWithBackendAuth invokes end backend sayHello HTTP handler through winch and kedge using
// authentication in the backend.
func TestHTTPEndpointCallWithBackendAuth(t *testing.T) {
const name = "kedge"
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
exit, err := spinup(t, ctx, config{winch: true, kedge: true, testEndpoint: true, testEndpointAuthentication: true})
if err != nil {
t.Error(err)
cancel()
return
}
defer func() {
cancel()
<-exit
}()
t.Run("AuthenticatedBackendAuthRequest_ShouldSucceed", func(t *testing.T) {
err = Retry(time.Second, ctx.Done(), func() error {
if err = assertRunning(exit); err != nil {
t.Error(err)
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
status, body, err := httpHelloViaWinchAndKedge(ctx, endpointDNS, name)
if err != nil {
return err
}
if status != http.StatusOK {
if status == http.StatusBadGateway {
return errors.New("not ready")
}
t.Errorf("Unexpected status code: %v. Resp: %v", status, body)
return nil
}
if body != expectedResponse(name) {
t.Errorf("Unexpected response: %v; Exp: %s", body, expectedResponse(name))
return nil
}
return nil
})
require.NoError(t, err)
})
t.Run("UnauthenticatedBackendAuthRequest_ShouldReturn401", func(t *testing.T) {
// Try URL for which winch will not append backend auth and we expect it to fail.
status, body, err := httpHelloViaWinchAndKedge(ctx, noAuthEndpointDNS, name)
require.NoError(t, err)
if status != http.StatusUnauthorized {
t.Errorf("Unexpected status code: %v. Expected 401. Resp: %v", status, body)
return
}
})
}
// TestHTTPEndpointCallWithBearerTokenProxyAuth invokes end backend SayHello RPC through winch and kedge using
// authentication in the proxy.
func TestHTTPEndpointCallWithBearerTokenProxyAuth(t *testing.T) {
const name = "kedge"
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
// Do not require auth in the endpoint only in the proxy.
exit, err := spinup(t, ctx, config{winch: true, kedge: true, kedgeBearerTokenAuth: true, testEndpoint: true, testEndpointAuthentication: false})
if err != nil {
t.Error(err)
cancel()
return
}
defer func() {
cancel()
<-exit
}()
t.Run("AuthenticatedProxyAuthRequest_ShouldSucceed", func(t *testing.T) {
err = Retry(time.Second, ctx.Done(), func() error {
if err = assertRunning(exit); err != nil {
t.Error(err)
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
status, body, err := httpHelloViaWinchAndKedge(ctx, bearerTokenProxyAuthEndpointDNS, name)
if err != nil {
return err
}
if status != http.StatusOK {
if status == http.StatusBadGateway {
return errors.New("not ready")
}
t.Errorf("Unexpected status code: %v. Resp: %v", status, body)
return nil
}
if body != expectedResponse(name) {
t.Errorf("Unexpected response: %v; Exp: %s", body, expectedResponse(name))
return nil
}
return nil
})
require.NoError(t, err)
})
t.Run("UnauthenticatedProxyAuthRequest_ShouldReturn401", func(t *testing.T) {
// Try URL for which winch will not append backend auth and we expect it to fail.
status, body, err := httpHelloViaWinchAndKedge(ctx, wrongBearerTokenProxyAuthEndpointDNS, name)
require.NoError(t, err)
if status != http.StatusUnauthorized {
t.Errorf("Unexpected status code: %v. Expected 401. Resp: %v", status, body)
return
}
})
}
func httpHelloViaWinchAndKedge(ctx context.Context, dnsName string, name string) (int, string, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s:%s/?name=%s", dnsName, httpTestEndpointPort, name), nil)
if err != nil {
return 0, "", err
}
defTransport := &http.Transport{
Proxy: func(*http.Request) (*url.URL, error) {
// Call via winch.
return url.Parse(fmt.Sprintf("http://127.0.0.1:%s", httpWinchPort))
},
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
resp, err := (&http.Client{Transport: defTransport}).Do(req.WithContext(ctx))
if err != nil {
return 0, "", err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, "", err
}
return resp.StatusCode, string(b), nil
}