forked from virtual-kubelet/virtual-kubelet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaadMock.go
68 lines (56 loc) · 1.33 KB
/
aadMock.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
package azure
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"strconv"
"time"
"github.com/Azure/go-autorest/autorest/adal"
)
// AADMock implements a AAD mock server .
type AADMock struct {
server *httptest.Server
OnAcquireToken func(http.ResponseWriter, *http.Request)
}
// NewAADMock creates a new AAD server mocker.
func NewAADMock() *AADMock {
aadServer := new(AADMock)
aadServer.start()
return aadServer
}
// Start the AAD server mocker.
func (mock *AADMock) start() {
if mock.server != nil {
return
}
mock.server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if mock.OnAcquireToken != nil {
mock.OnAcquireToken(w, r)
return
}
w.WriteHeader(http.StatusOK)
token := adal.Token{
AccessToken: "Test Token",
NotBefore: strconv.FormatInt(time.Now().UnixNano(), 10),
ExpiresIn: strconv.FormatInt(int64(time.Minute), 10),
}
b := new(bytes.Buffer)
json.NewEncoder(b).Encode(token)
w.Write(b.Bytes())
}))
}
// GetServerURL returns the mock server URL.
func (mock *AADMock) GetServerURL() string {
if mock.server != nil {
return mock.server.URL
}
panic("Mock server is not initialized.")
}
// Close terminates the AAD server mocker.
func (mock *AADMock) Close() {
if mock.server != nil {
mock.server.Close()
mock.server = nil
}
}