-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_helpers.go
68 lines (58 loc) · 1.43 KB
/
test_helpers.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 main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/pem"
mrand "math/rand"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"encoding/json"
"io/ioutil"
)
func genPrivateKey(t *testing.T) *rsa.PrivateKey {
pk, err := rsa.GenerateKey(rand.Reader, 2048)
assert.NoError(t, err)
return pk
}
func toPEM(pk *rsa.PrivateKey) []byte {
var buffer bytes.Buffer
pem.Encode(&buffer, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(pk)})
return buffer.Bytes()
}
func genToken() string {
b := make([]rune, 64)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
for i := range b {
b[i] = letters[mrand.Intn(len(letters))]
}
return string(b)
}
type mockAuthEndpoint struct {
pk *rsa.PrivateKey
token string
}
func (m *mockAuthEndpoint) ServeHTTP(w http.ResponseWriter, req *http.Request) {
bytes, _ := ioutil.ReadAll(req.Body)
data := make(map[string]interface{})
json.Unmarshal(bytes, &data)
// TODO: verify the token
w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"token":"` + m.token + `"}`))
}
type mockTarget struct {
expectedAuthZ string
}
func (m *mockTarget) ServeHTTP(w http.ResponseWriter, req *http.Request) {
authZ := req.Header.Get("Authorization")
if authZ == m.expectedAuthZ {
w.WriteHeader(200)
w.Write([]byte("OK"))
} else {
w.WriteHeader(401)
w.Write([]byte("Unauthorized"))
}
}