Skip to content

Commit

Permalink
Fix proxy env test, set env on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Jan 12, 2021
1 parent 3b90e7f commit 4f7a48c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 31 deletions.
2 changes: 1 addition & 1 deletion handler/openapi_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestOpenAPIValidator_ValidateRequest(t *testing.T) {
RequestBodyLimit: "64MiB",
}

proxyOpts, err := handler.NewProxyOptions(beConf, &handler.CORSOptions{})
proxyOpts, err := handler.NewProxyOptions(beConf, &handler.CORSOptions{}, config.DefaultSettings.NoProxyFromEnv)
helper.Must(err)

backend, err := handler.NewProxy(proxyOpts, log.WithContext(context.Background()), &server.Options{APIErrTpl: errors.DefaultJSON}, eval.NewENVContext(nil))
Expand Down
7 changes: 3 additions & 4 deletions handler/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import (
"sync"
"time"

"github.com/hashicorp/hcl/v2"
"github.com/sirupsen/logrus"
"golang.org/x/net/http/httpguts"

"github.com/avenga/couper/config"
"github.com/avenga/couper/config/body"
"github.com/avenga/couper/config/env"
Expand All @@ -31,6 +27,9 @@ import (
"github.com/avenga/couper/internal/seetie"
"github.com/avenga/couper/logging"
"github.com/avenga/couper/utils"
"github.com/hashicorp/hcl/v2"
"github.com/sirupsen/logrus"
"golang.org/x/net/http/httpguts"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion handler/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ func TestProxy_SetGetBody_LimitBody_Roundtrip(t *testing.T) {
proxyOpts, err := handler.NewProxyOptions(&config.Backend{
Remain: helper.NewProxyContext("set_request_headers = { x = req.post }"), // ensure buffering is enabled
RequestBodyLimit: testcase.limit,
}, nil)
}, nil, config.DefaultSettings.NoProxyFromEnv)
if err != nil {
subT.Error(err)
return
Expand Down
58 changes: 33 additions & 25 deletions server/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
var (
testBackend *test.Backend
testWorkingDir string
testProxyAddr = "http://127.0.0.1:9999"
)

func TestMain(m *testing.M) {
Expand All @@ -48,6 +49,11 @@ func setup() {
panic(err)
}

err = os.Setenv("HTTP_PROXY", testProxyAddr)
if err != nil {
panic(err)
}

wd, err := os.Getwd()
if err != nil {
panic(err)
Expand All @@ -57,14 +63,16 @@ func setup() {

func teardown() {
println("INTEGRATION: close test backend...")
if err := os.Unsetenv("COUPER_TEST_BACKEND_ADDR"); err != nil {
panic(err)
for _, key := range []string{"COUPER_TEST_BACKEND_ADDR", "HTTP_PROXY"} {
if err := os.Unsetenv(key); err != nil {
panic(err)
}
}
testBackend.Close()
}

func newCouper(file string, helper *test.Helper) (func(), *logrustest.Hook) {
gatewayConf, err := configload.LoadFile(filepath.Join(testWorkingDir, file))
couperFile, err := configload.LoadFile(filepath.Join(testWorkingDir, file))
helper.Must(err)

log, hook := logrustest.NewNullLogger()
Expand All @@ -81,7 +89,7 @@ func newCouper(file string, helper *test.Helper) (func(), *logrustest.Hook) {
//log.Out = os.Stdout

go func() {
if err := command.NewRun(ctx).Execute([]string{file}, gatewayConf, log.WithContext(ctx)); err != nil {
if err := command.NewRun(ctx).Execute([]string{file}, couperFile, log.WithContext(ctx)); err != nil {
shutdownFn()
panic(err)
}
Expand Down Expand Up @@ -452,37 +460,37 @@ func TestHTTPServer_XFHHeader(t *testing.T) {
}

func TestHTTPServer_ProxyFromEnv(t *testing.T) {
client := newClient()

seenProxyRequest := false
helper := test.New(t)

origin := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
seenProxyRequest = true
seen := make(chan struct{})
origin := httptest.NewUnstartedServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.WriteHeader(http.StatusNoContent)
go func() {
seen <- struct{}{}
}()
}))
ln, err := net.Listen("tcp4", testProxyAddr[7:])
helper.Must(err)
origin.Listener = ln
origin.Start()
defer origin.Close()

os.Setenv("http_proxy", origin.URL)
defer os.Setenv("http_proxy", "")

confPath := path.Join("testdata/integration", "api/01_couper.hcl")
shutdown, _ := newCouper(confPath, test.New(t))
defer shutdown()

t.Run("Test", func(subT *testing.T) {
helper := test.New(subT)

req, err := http.NewRequest(http.MethodGet, "http://anyserver:8080/v1/proxy", nil)
helper.Must(err)

_, err = client.Do(req)
helper.Must(err)
req, err := http.NewRequest(http.MethodGet, "http://anyserver:8080/v1/proxy", nil)
helper.Must(err)

if !seenProxyRequest {
t.Error("Proxy call failed")
}
})
_, err = newClient().Do(req)
helper.Must(err)

cleanup(shutdown, t)
timer := time.NewTimer(time.Second)
select {
case <-timer.C:
t.Error("Missing proxy call")
case <-seen:
}
}

func TestHTTPServer_Gzip(t *testing.T) {
Expand Down

0 comments on commit 4f7a48c

Please sign in to comment.