(t *testing.T)
| 178 | } |
| 179 | |
| 180 | func TestProxyRewrite(t *testing.T) { |
| 181 | var testCases = []struct { |
| 182 | whenPath string |
| 183 | expectProxiedURI string |
| 184 | expectStatus int |
| 185 | }{ |
| 186 | { |
| 187 | whenPath: "/api/users", |
| 188 | expectProxiedURI: "/users", |
| 189 | expectStatus: http.StatusOK, |
| 190 | }, |
| 191 | { |
| 192 | whenPath: "/js/main.js", |
| 193 | expectProxiedURI: "/public/javascripts/main.js", |
| 194 | expectStatus: http.StatusOK, |
| 195 | }, |
| 196 | { |
| 197 | whenPath: "/old", |
| 198 | expectProxiedURI: "/new", |
| 199 | expectStatus: http.StatusOK, |
| 200 | }, |
| 201 | { |
| 202 | whenPath: "/users/jack/orders/1", |
| 203 | expectProxiedURI: "/user/jack/order/1", |
| 204 | expectStatus: http.StatusOK, |
| 205 | }, |
| 206 | { |
| 207 | whenPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F", |
| 208 | expectProxiedURI: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F", |
| 209 | expectStatus: http.StatusOK, |
| 210 | }, |
| 211 | { // ` ` (space) is encoded by httpClient to `%20` when doing request to Echo. `%20` should not be double escaped when proxying request |
| 212 | whenPath: "/api/new users", |
| 213 | expectProxiedURI: "/new%20users", |
| 214 | expectStatus: http.StatusOK, |
| 215 | }, |
| 216 | { // query params should be proxied and not be modified |
| 217 | whenPath: "/api/users?limit=10", |
| 218 | expectProxiedURI: "/users?limit=10", |
| 219 | expectStatus: http.StatusOK, |
| 220 | }, |
| 221 | } |
| 222 | |
| 223 | for _, tc := range testCases { |
| 224 | t.Run(tc.whenPath, func(t *testing.T) { |
| 225 | receivedRequestURI := make(chan string, 1) |
| 226 | upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 227 | // RequestURI is the unmodified request-target of the Request-Line (RFC 7230, Section 3.1.1) as sent by the client to a server |
| 228 | // we need unmodified target to see if we are encoding/decoding the url in addition to rewrite/replace logic |
| 229 | // if original request had `%2F` we should not magically decode it to `/` as it would change what was requested |
| 230 | receivedRequestURI <- r.RequestURI |
| 231 | })) |
| 232 | defer upstream.Close() |
| 233 | serverURL, _ := url.Parse(upstream.URL) |
| 234 | rrb := NewRoundRobinBalancer([]*ProxyTarget{{Name: "upstream", URL: serverURL}}) |
| 235 | |
| 236 | // Rewrite |
| 237 | e := echo.New() |
nothing calls this directly
no test coverage detected
searching dependent graphs…