Skip to content

Commit

Permalink
chore: export un-started httpserver and enable conn with host for tra…
Browse files Browse the repository at this point in the history
…nsformer container
  • Loading branch information
mihir20 committed Mar 13, 2024
1 parent 6255c36 commit 88137be
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
8 changes: 8 additions & 0 deletions testhelper/docker/resource/transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ func WithUserTransformations(transformations map[string]string, cleaner resource
}
}

// WithConnectionToHostEnabled lets transformer container connect with the host machine
// i.e. transformer container will be able to access localhost of the host machine
func WithConnectionToHostEnabled() func(*config) {
return func(conf *config) {
conf.extraHosts = append(conf.extraHosts, "host.docker.internal:host-gateway")
}
}

// WithConfigBackendURL lets transformer use custom backend config server for transformations
// WithConfigBackendURL should not be used with WithUserTransformations option
func WithConfigBackendURL(url string) func(*config) {
Expand Down
4 changes: 2 additions & 2 deletions testhelper/httptest/httptest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// 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 := NewUnStartedServer(handler)
ts.start()
return ts
}
Expand All @@ -29,7 +29,7 @@ func (s *Server) start() {
s.URL = fmt.Sprintf("http://%s:%s", "localhost", port)
}

func newUnStartedServer(handler http.Handler) *Server {
func NewUnStartedServer(handler http.Handler) *Server {
return &Server{&nethttptest.Server{
Listener: newListener(),
Config: &http.Server{Handler: handler},
Expand Down
38 changes: 38 additions & 0 deletions testhelper/httptest/httptest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,41 @@ func TestServer(t *testing.T) {
require.Equal(t, http.StatusOK, statusCode)
require.Equal(t, "Hello, world!", string(body))
}

func TestUnStartedServer(t *testing.T) {
// create a server which is not started
httpUnStartedServer := kithttptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte("Hello, world!"))
}))

var (
body []byte
statusCode int
)
require.Never(t, func() bool {
resp, err := http.Get(httpUnStartedServer.URL)
defer func() { httputil.CloseResponse(resp) }()
if err == nil {
statusCode = resp.StatusCode
body, err = io.ReadAll(resp.Body)
}
return err == nil
}, 5*time.Second, time.Second, "connected to an un-started server")

// start the server now
httpUnStartedServer.Start()
defer httpUnStartedServer.Close()

require.Eventually(t, func() bool {
resp, err := http.Get(httpUnStartedServer.URL)
defer func() { httputil.CloseResponse(resp) }()
if err == nil {
statusCode = resp.StatusCode
body, err = io.ReadAll(resp.Body)
}
return err == nil
}, 5*time.Second, time.Second, "failed to connect to server")

require.Equal(t, http.StatusOK, statusCode)
require.Equal(t, "Hello, world!", string(body))
}

0 comments on commit 88137be

Please sign in to comment.