-
Notifications
You must be signed in to change notification settings - Fork 17
/
http_proxy_test.go
102 lines (83 loc) · 2.25 KB
/
http_proxy_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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package mps
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func newTestServer() *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
query := req.URL.Query()
text := []byte("hello world")
if query.Get("text") != "" {
text = []byte(query.Get("text"))
}
rw.Header().Set("Server", "MPS proxy server")
_, _ = rw.Write(text)
}))
}
func HttpGet(rawurl string, proxy func(r *http.Request) (*url.URL, error)) (*http.Response, error) {
req, _ := http.NewRequest(http.MethodGet, rawurl, nil)
http.DefaultClient.Transport = &http.Transport{
Proxy: proxy,
}
return http.DefaultClient.Do(req)
}
func TestNewHttpProxy(t *testing.T) {
srv := newTestServer()
defer srv.Close()
proxy := NewHttpProxy()
proxySrv := httptest.NewServer(proxy)
defer proxySrv.Close()
resp, err := HttpGet(srv.URL, func(r *http.Request) (*url.URL, error) {
return url.Parse(proxySrv.URL)
})
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
asserts := assert.New(t)
asserts.Equal(resp.StatusCode, 200, "statusCode should be equal 200")
asserts.Equal(int64(len(body)), resp.ContentLength)
}
func TestMiddlewareFunc(t *testing.T) {
// target server
srv := newTestServer()
defer srv.Close()
// proxy server
proxy := NewHttpProxy()
// use Middleware
proxy.UseFunc(func(req *http.Request, ctx *Context) (*http.Response, error) {
resp, err := ctx.Next(req)
if err != nil {
return nil, err
}
var buf bytes.Buffer
buf.WriteString("middleware")
resp.Body = io.NopCloser(&buf)
//
// You have to reset Content-Length, if you change the Body.
//resp.ContentLength = int64(buf.Len())
//resp.Header.Set("Content-Length", strconv.Itoa(buf.Len()))
return resp, nil
})
proxySrv := httptest.NewServer(proxy)
defer proxySrv.Close()
// send request
resp, err := HttpGet(srv.URL, func(r *http.Request) (*url.URL, error) {
return url.Parse(proxySrv.URL)
})
if err != nil {
t.Fatal(err)
}
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
asserts := assert.New(t)
asserts.Equal(resp.StatusCode, 200)
asserts.Equal(int64(len(body)), resp.ContentLength)
asserts.Equal(string(body), "middleware")
}