-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthenticator_test.go
56 lines (39 loc) · 1.48 KB
/
authenticator_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
package main
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"net/http"
"net/url"
"net/http/httptest"
)
func TestAuthenticateRetryWithBody(t *testing.T) {
token := genToken()
pk := genPrivateKey(t)
authServer := httptest.NewTLSServer(&mockAuthEndpoint{pk: pk, token: token})
defer authServer.Close()
targetServer := httptest.NewTLSServer(&mockTarget{expectedAuthZ: "token=" + token})
defer targetServer.Close()
targetEndpoint, _ := url.Parse(targetServer.URL)
creds, _ := fromPrivateKey("random", toPEM(pk), authServer.URL)
proxyServer := httptest.NewServer(NewAuthenticationHandler(targetEndpoint, creds, true, true))
defer proxyServer.Close()
req, _ := http.NewRequest("POST", proxyServer.URL, bytes.NewBufferString(`{"cloud":"butt"}`))
req.Header.Set("Content-Type", "application/json")
resp, _ := http.DefaultClient.Do(req)
assert.Equal(t, 200, resp.StatusCode, "Request should succeed", resp.Body)
}
func TestAuthenticate(t *testing.T) {
pk := genPrivateKey(t)
token := genToken()
authServer := httptest.NewTLSServer(&mockAuthEndpoint{pk: pk, token: token})
defer authServer.Close()
targetServer := httptest.NewTLSServer(&mockTarget{expectedAuthZ: "token=" + token})
defer targetServer.Close()
targetEndpoint, _ := url.Parse(targetServer.URL)
creds, _ := fromPrivateKey("random", toPEM(pk), authServer.URL)
a := newAuthenticator(targetEndpoint, creds, true, true)
token, err := a.authenticate()
assert.NoError(t, err)
assert.NotEmpty(t, token)
}