-
Notifications
You must be signed in to change notification settings - Fork 17
/
mitm_handler_test.go
50 lines (42 loc) · 1.15 KB
/
mitm_handler_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
package mps
import (
"crypto/tls"
"crypto/x509"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/telanflow/mps/cert"
)
func TestNewMitmHandler(t *testing.T) {
mitmHandler := NewMitmHandler()
mitmSrv := httptest.NewServer(mitmHandler)
defer mitmSrv.Close()
clientCertPool := x509.NewCertPool()
ok := clientCertPool.AppendCertsFromPEM([]byte(cert.CertPEM))
if !ok {
panic("failed to parse root certificate")
}
req, _ := http.NewRequest(http.MethodGet, "https://httpbin.org/get", nil)
http.DefaultClient.Transport = &http.Transport{
Proxy: func(r *http.Request) (*url.URL, error) {
return url.Parse(mitmSrv.URL)
},
TLSClientConfig: &tls.Config{
Certificates: []tls.Certificate{cert.DefaultCertificate},
ClientAuth: tls.RequireAndVerifyClientCert,
RootCAs: clientCertPool,
},
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
asserts := assert.New(t)
asserts.Equal(resp.StatusCode, 200, "response status code not equal 200")
asserts.Equal(int64(len(body)), resp.ContentLength)
}