2023-02-02 17:15:17 -08:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2023-01-17 09:54:01 -07:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2024-02-21 16:32:26 -05:00
|
|
|
"sync/atomic"
|
2023-01-17 09:54:01 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestProxyStateHelpers(t *testing.T) {
|
|
|
|
|
old := ProxyState
|
2024-02-21 16:32:26 -05:00
|
|
|
ProxyState = proxyState{proxyCount: new(atomic.Int64)}
|
2023-01-17 09:54:01 -07:00
|
|
|
t.Cleanup(func() {
|
|
|
|
|
ProxyState = old
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("proxyConnFnCounter", func(t *testing.T) {
|
|
|
|
|
assert.EqualValues(t, 0, ProxyState.CurrentProxiedConnections())
|
2023-05-30 10:23:23 -07:00
|
|
|
proxyConnFnCounter(func() {
|
2023-01-17 09:54:01 -07:00
|
|
|
assert.EqualValues(t, 1, ProxyState.CurrentProxiedConnections())
|
2023-05-30 10:23:23 -07:00
|
|
|
})()
|
2023-01-17 09:54:01 -07:00
|
|
|
assert.EqualValues(t, 0, ProxyState.CurrentProxiedConnections())
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("ProxyHandlerCounter", func(t *testing.T) {
|
|
|
|
|
assert.EqualValues(t, 0, ProxyState.CurrentProxiedConnections())
|
|
|
|
|
var handlerRan bool
|
|
|
|
|
h := ProxyHandlerCounter(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
|
|
|
|
handlerRan = true
|
|
|
|
|
assert.EqualValues(t, 1, ProxyState.CurrentProxiedConnections())
|
|
|
|
|
}))
|
|
|
|
|
assert.EqualValues(t, 0, ProxyState.CurrentProxiedConnections())
|
|
|
|
|
req, err := http.NewRequest("GET", "test/path", nil)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
h.ServeHTTP(httptest.NewRecorder(), req)
|
|
|
|
|
require.True(t, handlerRan)
|
|
|
|
|
})
|
|
|
|
|
}
|