@@ -3,9 +3,11 @@ package main
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "io/ioutil"
6
7
"net/http"
7
8
"net/http/httptest"
8
9
"net/url"
10
+ "strings"
9
11
"testing"
10
12
"time"
11
13
@@ -25,16 +27,22 @@ type mockStatResponseWriter struct {
25
27
}
26
28
27
29
type mockHosts struct {
30
+ t * testing.T
31
+ b string
28
32
hs []string
29
33
hst []string
30
34
}
31
35
32
36
// TestQueryWithRetryFail function statResponseWriter's statusCode will not be 200, but the query has been proxied
33
37
// The request will be retried 1 time with a different host in the same replica
34
38
func TestQueryWithRetryFail (t * testing.T ) {
35
- req := newRequest ("http://localhost:8080" )
39
+ body := "foo query"
40
+
41
+ req := newRequest ("http://localhost:8080" , body )
36
42
37
43
mhs := & mockHosts {
44
+ t : t ,
45
+ b : body ,
38
46
hs : []string {"localhost:8080" , "localhost:8081" },
39
47
}
40
48
@@ -71,9 +79,13 @@ func TestQueryWithRetryFail(t *testing.T) {
71
79
// TestRunQuerySuccessOnce function statResponseWriter's statusCode will be StatusOK after executeWithRetry, the query has been proxied
72
80
// The execution will succeeded without retry
73
81
func TestQuerySuccessOnce (t * testing.T ) {
74
- req := newRequest ("http://localhost:8090" )
82
+ body := "foo query"
83
+
84
+ req := newRequest ("http://localhost:8090" , body )
75
85
76
86
mhs := & mockHosts {
87
+ t : t ,
88
+ b : body ,
77
89
hs : []string {"localhost:8080" , "localhost:8090" },
78
90
}
79
91
@@ -108,9 +120,13 @@ func TestQuerySuccessOnce(t *testing.T) {
108
120
// TestQueryWithRetrySuccess function statResponseWriter's statusCode will be StatusOK after executeWithRetry, the query has been proxied
109
121
// The execution will succeeded after retry
110
122
func TestQueryWithRetrySuccess (t * testing.T ) {
111
- req := newRequest ("http://localhost:8080" )
123
+ body := "foo query"
124
+
125
+ req := newRequest ("http://localhost:8080" , body )
112
126
113
127
mhs := & mockHosts {
128
+ t : t ,
129
+ b : body ,
114
130
hs : []string {"localhost:8080" , "localhost:8090" },
115
131
}
116
132
@@ -148,12 +164,21 @@ func (mhs *mockHosts) mockReverseProxy(rw http.ResponseWriter, req *http.Request
148
164
} else {
149
165
rw .WriteHeader (http .StatusOK )
150
166
}
167
+
168
+ b , err := ioutil .ReadAll (req .Body )
169
+ if err != nil {
170
+ mhs .t .Errorf ("The req body cannot be read: %v" , err )
171
+ }
172
+ req .Body .Close ()
173
+
174
+ assert .Equal (mhs .t , mhs .b , string (b ))
175
+
151
176
mhs .hst = append (mhs .hst , req .URL .Host )
152
177
}
153
178
154
- func newRequest (host string ) * http.Request {
179
+ func newRequest (host , body string ) * http.Request {
155
180
// create a new req
156
- req := httptest .NewRequest (http .MethodGet , host , nil )
181
+ req := httptest .NewRequest (http .MethodPost , host , strings . NewReader ( body ) )
157
182
158
183
ctx := context .Background ()
159
184
0 commit comments