-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add mock config BE support for transformer
- Loading branch information
Showing
8 changed files
with
263 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
testhelper/docker/resource/transformer/transformer_backend_config.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package transformer | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
const ( | ||
getByVersionIdEndPoint = "/transformation/getByVersionId" | ||
versionIDKey = "versionId" | ||
) | ||
|
||
type mockHttpServer struct { | ||
Transformations map[string]string | ||
} | ||
|
||
func (m *mockHttpServer) handleGetByVersionId(w http.ResponseWriter, r *http.Request) { | ||
transformationVersionId := r.URL.Query().Get(versionIDKey) | ||
transformationCode, ok := m.Transformations[transformationVersionId] | ||
if !ok { | ||
w.WriteHeader(http.StatusNotFound) | ||
return | ||
} | ||
transformationCode = sanitiseTransformationCode(transformationCode) | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write([]byte(fmt.Sprintf(`{ | ||
"id": "%s", | ||
"createdAt": "2023-03-27T11:40:00.894Z", | ||
"updatedAt": "2023-03-27T11:40:00.894Z", | ||
"versionId": "%s", | ||
"name": "Add Transformed field", | ||
"description": "", | ||
"code": "%s", | ||
"secretsVersion": null, | ||
"codeVersion": "1", | ||
"language": "javascript", | ||
"imports": [], | ||
"secrets": {} | ||
}`, uuid.NewString(), transformationVersionId, transformationCode))) | ||
if err != nil { | ||
return | ||
} | ||
w.WriteHeader(http.StatusInternalServerError) | ||
} | ||
|
||
func sanitiseTransformationCode(transformationCode string) string { | ||
sanitisedTransformationCode := strings.ReplaceAll(transformationCode, "\t", " ") | ||
sanitisedTransformationCode = strings.ReplaceAll(sanitisedTransformationCode, "\n", "\\n") | ||
return sanitisedTransformationCode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package httptest | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"net/http" | ||
nethttptest "net/http/httptest" | ||
) | ||
|
||
// NewServer starts a new httptest server that listens on all interfaces, contrary to the standard net/httptest.Server that listens only on localhost. | ||
// This is useful when you want to access the test http server from within a docker container. | ||
func NewServer(handler http.Handler) *Server { | ||
ts := newUnStartedServer(handler) | ||
ts.Start() | ||
return ts | ||
} | ||
|
||
// Simple net/httptest.Server wrapper | ||
type Server struct { | ||
*nethttptest.Server | ||
} | ||
|
||
func (s *Server) Start() { | ||
s.Server.Start() | ||
_, port, err := net.SplitHostPort(s.Listener.Addr().String()) | ||
if err != nil { | ||
panic(fmt.Sprintf("httptest: failed to parse listener address: %v", err)) | ||
} | ||
s.URL = fmt.Sprintf("http://%s:%s", "localhost", port) | ||
} | ||
|
||
func newUnStartedServer(handler http.Handler) *Server { | ||
return &Server{&nethttptest.Server{ | ||
Listener: newListener(), | ||
Config: &http.Server{Handler: handler}, | ||
}} | ||
} | ||
|
||
func newListener() net.Listener { | ||
l, err := net.Listen("tcp", "0.0.0.0:0") | ||
if err != nil { | ||
if l, err = net.Listen("tcp6", "[::]:0"); err != nil { | ||
panic(fmt.Sprintf("httptest: failed to listen on a port: %v", err)) | ||
} | ||
} | ||
return l | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package httptest_test | ||
|
||
import ( | ||
"io" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/rudderlabs/rudder-go-kit/httputil" | ||
kithttptest "github.com/rudderlabs/rudder-go-kit/testhelper/httptest" | ||
) | ||
|
||
func TestServer(t *testing.T) { | ||
httpServer := kithttptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte("Hello, world!")) | ||
})) | ||
defer httpServer.Close() | ||
|
||
httpServerParsedURL, err := url.Parse(httpServer.URL) | ||
require.NoError(t, err) | ||
|
||
_, httpServerPort, err := net.SplitHostPort(httpServerParsedURL.Host) | ||
require.NoError(t, err) | ||
|
||
var ( | ||
body []byte | ||
statusCode int | ||
) | ||
require.Eventually(t, func() bool { | ||
resp, err := http.Get("http://0.0.0.0:" + httpServerPort) | ||
defer func() { httputil.CloseResponse(resp) }() | ||
if err == nil { | ||
statusCode = resp.StatusCode | ||
body, err = io.ReadAll(resp.Body) | ||
} | ||
return err == nil | ||
}, 5*time.Second, 10*time.Millisecond, "failed to connect to proxy") | ||
|
||
require.Equal(t, http.StatusOK, statusCode) | ||
require.Equal(t, "Hello, world!", string(body)) | ||
} |