mirror of
https://github.com/umputun/reproxy.git
synced 2025-06-30 22:13:42 +02:00
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
![]() |
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) {
|
||
|
t.Logf("%v", r)
|
||
|
w.Header().Add("h1", "v1")
|
||
|
w.Write([]byte("response"))
|
||
|
}))
|
||
|
|
||
|
svc := discovery.NewService([]discovery.Provider{
|
||
|
&provider.Static{Rules: []string{"^/api/(.*)::" + ds.URL + "/123/$1"}}})
|
||
|
|
||
|
go func() {
|
||
|
svc.Do(context.Background())
|
||
|
}()
|
||
|
|
||
|
h.Matcher = svc
|
||
|
go func() {
|
||
|
h.Do(ctx)
|
||
|
}()
|
||
|
time.Sleep(10 * time.Millisecond)
|
||
|
|
||
|
client := http.Client{}
|
||
|
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", 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)
|
||
|
}
|