MSP-1470 implement ws proxy test
This commit is contained in:
1 parent
02334b3617
commit
f78db86e39
1 file changed
+99
-36
+99
-36
@@ -8,13 +8,16 @@ import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
etp "github.com/integration-system/isp-etp-go/v2"
|
||||
etpcli "github.com/integration-system/isp-etp-go/v2/client"
|
||||
"github.com/integration-system/isp-kit/http/httpcli"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/stretchr/testify/require"
|
||||
"isp-gate-service/assembly"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/routes"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/google/uuid"
|
||||
"github.com/integration-system/isp-kit/grpc"
|
||||
"github.com/integration-system/isp-kit/grpc/client"
|
||||
@@ -82,16 +85,16 @@ func (s *HappyPathTestSuite) TestGrpcProxy() {
|
||||
require.NoError(err)
|
||||
|
||||
srv := httptest.NewServer(handler)
|
||||
cli := resty.NewWithClient(srv.Client()).SetBaseURL(srv.URL)
|
||||
cli := httpcli.New()
|
||||
req := request{Id: uuid.New().String()}
|
||||
resp := response{}
|
||||
_, err = cli.R().
|
||||
SetHeader("x-application-token", "token").
|
||||
SetHeader("x-request-id", requestId).
|
||||
SetHeader("x-auth-admin", "mock-token").
|
||||
SetBody(req).
|
||||
SetResult(&resp).
|
||||
Post("/api/endpoint")
|
||||
_, err = cli.Post(srv.URL+"/api/endpoint").
|
||||
Header("x-application-token", "token").
|
||||
Header("x-request-id", requestId).
|
||||
Header("x-auth-admin", "mock-token").
|
||||
JsonRequestBody(req).
|
||||
JsonResponseBody(&resp).
|
||||
Do(context.Background())
|
||||
require.NoError(err)
|
||||
require.EqualValues(req.Id, resp.Id)
|
||||
}
|
||||
@@ -103,24 +106,7 @@ func (s *HappyPathTestSuite) TestHttpProxy() {
|
||||
requestId := requestid.Next()
|
||||
targetService := httpt.NewMock(test)
|
||||
targetService.POST("/endpoint", func(ctx context.Context, httpReq *http.Request, req request) response {
|
||||
systemId, err := strconv.Atoi(httpReq.Header.Get("x-system-identity"))
|
||||
require.NoError(err)
|
||||
domainId, err := strconv.Atoi(httpReq.Header.Get("x-domain-identity"))
|
||||
require.NoError(err)
|
||||
serviceId, err := strconv.Atoi(httpReq.Header.Get("x-service-identity"))
|
||||
require.NoError(err)
|
||||
applicationId, err := strconv.Atoi(httpReq.Header.Get("x-application-identity"))
|
||||
require.NoError(err)
|
||||
require.EqualValues(requestId, requestid.FromContext(ctx))
|
||||
require.EqualValues(1, systemId)
|
||||
require.EqualValues(2, domainId)
|
||||
require.EqualValues(3, serviceId)
|
||||
require.EqualValues(4, applicationId)
|
||||
|
||||
adminId, err := strconv.Atoi(httpReq.Header.Get("x-admin-id"))
|
||||
require.NoError(err)
|
||||
require.EqualValues(1, adminId)
|
||||
|
||||
assertHeaders(require, requestId, ctx, httpReq.Header)
|
||||
return response{Id: req.Id} //nolint:gosimple
|
||||
})
|
||||
targetUrl, err := url.Parse(targetService.BaseURL())
|
||||
@@ -138,20 +124,77 @@ func (s *HappyPathTestSuite) TestHttpProxy() {
|
||||
require.NoError(err)
|
||||
|
||||
srv := httptest.NewServer(handler)
|
||||
cli := resty.NewWithClient(srv.Client()).SetBaseURL(srv.URL)
|
||||
cli := httpcli.New()
|
||||
req := request{Id: uuid.New().String()}
|
||||
resp := response{}
|
||||
_, err = cli.R().
|
||||
SetHeader("x-application-token", "token").
|
||||
SetHeader("x-request-id", requestId).
|
||||
SetHeader("x-auth-admin", "mock-token").
|
||||
SetBody(req).
|
||||
SetResult(&resp).
|
||||
Post("/api/endpoint")
|
||||
_, err = cli.Post(srv.URL+"/api/endpoint").
|
||||
Header("x-application-token", "token").
|
||||
Header("x-request-id", requestId).
|
||||
Header("x-auth-admin", "mock-token").
|
||||
JsonRequestBody(req).
|
||||
JsonResponseBody(&resp).
|
||||
Do(context.Background())
|
||||
require.NoError(err)
|
||||
require.EqualValues(req.Id, resp.Id)
|
||||
}
|
||||
|
||||
func (s *HappyPathTestSuite) TestWsProxy() {
|
||||
test, require := test.New(s.T())
|
||||
config, redisCli, systemCli, adminCli := s.commonDependencies(test)
|
||||
|
||||
requestId := requestid.Next()
|
||||
wsServer := etp.NewServer(context.Background(), etp.ServerConfig{
|
||||
ConnectionReadLimit: 2048,
|
||||
InsecureSkipVerify: true,
|
||||
})
|
||||
wsServer.OnWithAck("hello", func(conn etp.Conn, data []byte) []byte {
|
||||
headers := conn.RemoteHeader()
|
||||
ctx := requestid.ToContext(context.Background(), headers.Get("x-request-id"))
|
||||
assertHeaders(require, requestId, ctx, headers)
|
||||
require.EqualValues("data", string(data))
|
||||
return []byte("world")
|
||||
})
|
||||
wsMux := http.NewServeMux()
|
||||
wsMux.Handle("/service", http.HandlerFunc(func(writer http.ResponseWriter, r *http.Request) {
|
||||
wsServer.ServeHttp(writer, r)
|
||||
}))
|
||||
targetService := httptest.NewServer(wsMux)
|
||||
targetUrl, err := url.Parse(targetService.URL)
|
||||
require.NoError(err)
|
||||
rr := lb.NewRoundRobin([]string{targetUrl.Host})
|
||||
targetClients := map[string]*lb.RoundRobin{"target": rr}
|
||||
locator := assembly.NewLocator(test.Logger(), nil, targetClients, routes.NewRoutes(), systemCli, adminCli)
|
||||
locations := []conf.Location{{
|
||||
SkipAuth: false,
|
||||
PathPrefix: "/ws",
|
||||
Protocol: "ws",
|
||||
TargetModule: "target",
|
||||
}}
|
||||
handler, err := locator.Handler(config, locations, redisCli)
|
||||
require.NoError(err)
|
||||
srv := httptest.NewServer(handler)
|
||||
|
||||
cli := etpcli.NewClient(etpcli.Config{
|
||||
HttpHeaders: map[string][]string{
|
||||
"x-request-id": {requestId},
|
||||
},
|
||||
})
|
||||
requestUrl, err := url.Parse(srv.URL)
|
||||
require.NoError(err)
|
||||
requestUrl.Path = "ws/service"
|
||||
requestUrl.RawQuery = url.Values{
|
||||
"x-application-token": []string{"token"},
|
||||
"x-auth-admin": []string{"mock-token"},
|
||||
}.Encode()
|
||||
err = cli.Dial(context.Background(), requestUrl.String())
|
||||
require.NoError(err)
|
||||
resp, err := cli.EmitWithAck(context.Background(), "hello", []byte("data"))
|
||||
require.NoError(err)
|
||||
require.EqualValues("world", string(resp))
|
||||
err = cli.Close()
|
||||
require.NoError(err)
|
||||
}
|
||||
|
||||
func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, redis.UniversalClient, *client.Client, *client.Client) {
|
||||
require := test.Assert()
|
||||
redisCli := NewRedis(test)
|
||||
@@ -210,3 +253,23 @@ func (s *HappyPathTestSuite) commonDependencies(test *test.Test) (conf.Remote, r
|
||||
func TestHappyPathTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(HappyPathTestSuite))
|
||||
}
|
||||
|
||||
func assertHeaders(require *require.Assertions, requestId string, ctx context.Context, headers http.Header) {
|
||||
systemId, err := strconv.Atoi(headers.Get("x-system-identity"))
|
||||
require.NoError(err)
|
||||
domainId, err := strconv.Atoi(headers.Get("x-domain-identity"))
|
||||
require.NoError(err)
|
||||
serviceId, err := strconv.Atoi(headers.Get("x-service-identity"))
|
||||
require.NoError(err)
|
||||
applicationId, err := strconv.Atoi(headers.Get("x-application-identity"))
|
||||
require.NoError(err)
|
||||
require.EqualValues(requestId, requestid.FromContext(ctx))
|
||||
require.EqualValues(1, systemId)
|
||||
require.EqualValues(2, domainId)
|
||||
require.EqualValues(3, serviceId)
|
||||
require.EqualValues(4, applicationId)
|
||||
|
||||
adminId, err := strconv.Atoi(headers.Get("x-admin-id"))
|
||||
require.NoError(err)
|
||||
require.EqualValues(1, adminId)
|
||||
}
|
||||
Reference in new issue
Block a user