Skip to content

Commit 57bfe91

Browse files
fix empty SERVER_ADDRESS config loading
After bumping packages, envconfig behaviour has changed. If a struct contains a pointer, and no value is being loaded, it will now generate the zero value for the pointed type rather than nil. This prevented the proxy from being started if no SERVER_ADDRESS was specified, as ConfigureProxy checks for no server address being specified by comparing it to url.URL{}, which because of the pointer is no longer equal. The fix is a simple config change for envconfig so that it will use nil rather than the zero value for the pointer when no value is specified.
1 parent eb0a754 commit 57bfe91

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

internal/app/configuration/proxyconfig.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ import (
44
"context"
55
"net/url"
66

7-
"github.com/form3tech-oss/pact-proxy/internal/app/pactproxy"
87
"github.com/pkg/errors"
98
"github.com/sethvargo/go-envconfig"
9+
10+
"github.com/form3tech-oss/pact-proxy/internal/app/pactproxy"
1011
)
1112

1213
func NewFromEnv() (pactproxy.Config, error) {
1314
ctx := context.Background()
1415

1516
var config pactproxy.Config
16-
err := envconfig.Process(ctx, &config)
17+
err := envconfig.ProcessWith(ctx, &envconfig.Config{
18+
Target: &config,
19+
DefaultNoInit: true,
20+
})
1721
if err != nil {
1822
return config, errors.Wrap(err, "process env config")
1923
}

internal/app/configuration/proxyconfig_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import (
2121
"testing"
2222
"time"
2323

24-
"github.com/form3tech-oss/pact-proxy/internal/app/pactproxy"
2524
"github.com/pact-foundation/pact-go/utils"
2625
"github.com/stretchr/testify/require"
26+
27+
"github.com/form3tech-oss/pact-proxy/internal/app/pactproxy"
2728
)
2829

2930
const (
@@ -183,6 +184,16 @@ func TestConfigureProxy_MTLS(t *testing.T) {
183184
}
184185
}
185186

187+
// When no server address env var is specified, config.ServerAddress should be the zero value.
188+
// This is used during ConfigureProxy logic, but is no longer the default behaviour of envconfig.Process due to url.URL
189+
// containing a pointer.
190+
func TestProxyConfig_NewFromEnv_EmptyServerAddress(t *testing.T) {
191+
os.Clearenv()
192+
config, err := NewFromEnv()
193+
require.NoError(t, err)
194+
require.Equal(t, url.URL{}, config.ServerAddress)
195+
}
196+
186197
func TestProxyConfig_ForwardUnrecognisedRequests(t *testing.T) {
187198
os.Clearenv()
188199
config, err := NewFromEnv()

0 commit comments

Comments
 (0)