Files
upstream-corteza/server/pkg/apigw/route_test.go
upstream-mirror 7e385fc126
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
2026-09-18 01:22:23 +05:00

108 lines
2.6 KiB
Go

package apigw
import (
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/cortezaproject/corteza/server/pkg/apigw/pipeline"
"github.com/cortezaproject/corteza/server/pkg/apigw/pipeline/chain"
"github.com/cortezaproject/corteza/server/pkg/apigw/types"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
func Test_pl(t *testing.T) {
type (
tf struct {
name string
method string
endpoint string
expError string
expStatus int
handler *types.MockHandler
errHandler *types.MockErrorHandler
}
)
var (
tcc = []tf{
{
name: "successful handler",
handler: &types.MockHandler{
Handler_: func(rw http.ResponseWriter, r *http.Request) error {
rw.WriteHeader(http.StatusTemporaryRedirect)
return nil
},
},
method: "POST",
expStatus: http.StatusTemporaryRedirect,
expError: "",
},
{
name: "unsuccessful handle custom error response",
handler: &types.MockHandler{
Handler_: func(rw http.ResponseWriter, r *http.Request) error {
rw.WriteHeader(http.StatusTemporaryRedirect)
return errors.New("test error")
},
},
errHandler: &types.MockErrorHandler{
Handler_: func(rw http.ResponseWriter, r *http.Request, err error) {
rw.Write([]byte("custom error response: " + err.Error()))
},
},
method: "POST",
expStatus: http.StatusTemporaryRedirect,
expError: "custom error response: test error",
},
{
name: "unsuccessful handle default error response",
handler: &types.MockHandler{
Handler_: func(rw http.ResponseWriter, r *http.Request) error {
rw.WriteHeader(http.StatusTemporaryRedirect)
return errors.New("test error")
},
},
method: "POST",
expStatus: http.StatusTemporaryRedirect,
expError: "{\"error\":{\"message\":\"test error\"}}\n",
},
}
)
for _, tc := range tcc {
t.Run(tc.name, func(t *testing.T) {
var (
req = require.New(t)
rr = httptest.NewRecorder()
pipe = pipeline.NewPipeline(zap.NewNop(), chain.NewDefault())
)
r := httptest.NewRequest("POST", "/foo", http.NoBody)
pipe.Add(&pipeline.Worker{
Handler: tc.handler.Handler(),
})
if tc.errHandler != nil {
pipe.ErrorHandler(tc.errHandler.Handler())
}
route := &route{
method: tc.method,
endpoint: tc.endpoint,
log: zap.NewNop(),
handler: pipe.Handler(),
errHandler: pipe.Error(),
}
route.ServeHTTP(rr, r)
req.Equal(tc.expStatus, rr.Result().StatusCode)
req.Equal(tc.expError, rr.Body.String())
})
}
}