2021-04-01 02:37:28 -05:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/umputun/docker-proxy/app/discovery"
|
|
|
|
"github.com/umputun/docker-proxy/app/discovery/provider"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHttp_Do(t *testing.T) {
|
|
|
|
port := rand.Intn(10000) + 40000
|
|
|
|
h := Http{TimeOut: 200 * time.Millisecond, Address: fmt.Sprintf("127.0.0.1:%d", port)}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ds := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-04-02 00:50:16 -05:00
|
|
|
t.Logf("req: %v", r)
|
2021-04-01 02:37:28 -05:00
|
|
|
w.Header().Add("h1", "v1")
|
2021-04-02 00:50:16 -05:00
|
|
|
fmt.Fprintf(w, "response %s", r.URL.String())
|
2021-04-01 02:37:28 -05:00
|
|
|
}))
|
|
|
|
|
|
|
|
svc := discovery.NewService([]discovery.Provider{
|
2021-04-02 00:50:16 -05:00
|
|
|
&provider.Static{Rules: []string{
|
|
|
|
"localhost,^/api/(.*)," + ds.URL + "/123/$1",
|
|
|
|
"127.0.0.1,^/api/(.*)," + ds.URL + "/567/$1",
|
|
|
|
},
|
|
|
|
}})
|
2021-04-01 02:37:28 -05:00
|
|
|
|
|
|
|
go func() {
|
2021-04-03 00:22:54 -05:00
|
|
|
svc.Run(context.Background())
|
2021-04-01 02:37:28 -05:00
|
|
|
}()
|
|
|
|
|
|
|
|
h.Matcher = svc
|
|
|
|
go func() {
|
2021-04-03 00:22:54 -05:00
|
|
|
h.Run(ctx)
|
2021-04-01 02:37:28 -05:00
|
|
|
}()
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
|
|
|
|
client := http.Client{}
|
2021-04-02 00:50:16 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
resp, err := client.Get("http://127.0.0.1:" + strconv.Itoa(port) + "/api/something")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
t.Logf("%+v", resp.Header)
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "response /567/something", string(body))
|
|
|
|
assert.Equal(t, "dpx", resp.Header.Get("App-Name"))
|
|
|
|
assert.Equal(t, "v1", resp.Header.Get("h1"))
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
resp, err := client.Get("http://localhost:" + strconv.Itoa(port) + "/api/something")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusOK, resp.StatusCode)
|
|
|
|
t.Logf("%+v", resp.Header)
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, "response /123/something", string(body))
|
|
|
|
assert.Equal(t, "dpx", resp.Header.Get("App-Name"))
|
|
|
|
assert.Equal(t, "v1", resp.Header.Get("h1"))
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
resp, err := client.Get("http://127.0.0.1:" + strconv.Itoa(port) + "/bad/something")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, http.StatusBadGateway, resp.StatusCode)
|
|
|
|
|
|
|
|
}
|
2021-04-01 02:37:28 -05:00
|
|
|
}
|