-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclient_test.go
More file actions
69 lines (59 loc) · 1.44 KB
/
Copy pathclient_test.go
File metadata and controls
69 lines (59 loc) · 1.44 KB
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
package securehttp
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func createServer(isSecure bool) *httptest.Server {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "test")
})
if isSecure {
return httptest.NewServer(&Server{handler})
}
return httptest.NewServer(handler)
}
func testClient(t *testing.T, typ, privateKey string, ts *httptest.Server) {
d, e := NewDecryptor(typ, privateKey)
if e != nil {
t.Fatal(e)
}
c := &Client{d, &http.Client{}}
resp, e := c.Get(ts.URL)
if e != nil {
t.Fatal(e)
}
b, e := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if e != nil {
t.Fatal(e)
}
if string(b) != "test" {
t.Fatal(string(b))
}
}
func testClientsOnServer(t *testing.T, ts *httptest.Server) {
testClient(t, TypeNocrypt, "nokey", ts)
private, _ := rsa.GenerateKey(rand.Reader, 2048)
key := string(x509.MarshalPKCS1PrivateKey(private))
testClient(t, TypeRSA, key, ts)
private, _ = rsa.GenerateKey(rand.Reader, 1024)
key = string(x509.MarshalPKCS1PrivateKey(private))
testClient(t, TypeRSA, key, ts)
private, _ = rsa.GenerateKey(rand.Reader, 512)
key = string(x509.MarshalPKCS1PrivateKey(private))
testClient(t, TypeRSA, key, ts)
}
func TestClient(t *testing.T) {
ts := createServer(true)
defer ts.Close()
testClientsOnServer(t, ts)
ts2 := createServer(false)
defer ts2.Close()
testClientsOnServer(t, ts2)
}