Skip to content

Commit

Permalink
Update recording tests to use standalone proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
benbp committed Jul 15, 2023
1 parent 475add0 commit 9301849
Show file tree
Hide file tree
Showing 25 changed files with 586 additions and 570 deletions.
4 changes: 3 additions & 1 deletion sdk/internal/recording/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package recording
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -76,7 +77,8 @@ func SetDefaultMatcher(t *testing.T, options *SetDefaultMatcherOptions) error {
return nil
}
options.fillOptions()
req, err := http.NewRequest("POST", "http://localhost:5000/Admin/SetMatcher", http.NoBody)
url := fmt.Sprintf("%s/Admin/SetMatcher", defaultOptions().baseURL())
req, err := http.NewRequest("POST", url, http.NoBody)
if err != nil {
panic(err)
}
Expand Down
171 changes: 100 additions & 71 deletions sdk/internal/recording/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,167 +9,196 @@ package recording
import (
"bytes"
"net/http"
"os"
"os/exec"
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

func TestSetBodilessMatcher(t *testing.T) {
type matchersTests struct {
suite.Suite
proxyCmd *exec.Cmd
}

func TestMatchers(t *testing.T) {
suite.Run(t, new(matchersTests))
}

func (s *matchersTests) SetupSuite() {
proxyCmd, err := StartTestProxyInstance(nil)
s.proxyCmd = proxyCmd
require.NoError(s.T(), err)
}

func (s *matchersTests) TearDownSuite() {
StopTestProxyInstance(s.proxyCmd, nil)

err := os.RemoveAll("./testdata/recordings/TestMatchers/")
require.NoError(s.T(), err)
}

func (s *matchersTests) TestSetBodilessMatcher() {
require := require.New(s.T())
temp := recordMode
recordMode = RecordingMode
defer func() { recordMode = temp }()

err := Start(t, packagePath, nil)
require.NoError(t, err)
err := Start(s.T(), packagePath, nil)
require.NoError(err)

req, err := http.NewRequest("POST", "https://localhost:5001", nil)
require.NoError(t, err)
req, err := http.NewRequest("POST", defaultOptions().baseURL(), nil)
require.NoError(err)

req.Header.Set(UpstreamURIHeader, "https://bing.com")
req.Header.Set(ModeHeader, GetRecordMode())
req.Header.Set(IDHeader, GetRecordingId(t))
req.Header.Set(IDHeader, GetRecordingId(s.T()))

client, err := GetHTTPClient(t)
require.NoError(t, err)
client, err := GetHTTPClient(s.T())
require.NoError(err)

_, err = client.Do(req)
require.NoError(t, err)
require.NoError(err)

err = Stop(t, nil)
require.NoError(t, err)
err = Stop(s.T(), nil)
require.NoError(err)

// Run a second request to with different body to verify it works
recordMode = PlaybackMode

err = Start(t, packagePath, nil)
require.NoError(t, err)
err = Start(s.T(), packagePath, nil)
require.NoError(err)

err = SetBodilessMatcher(t, nil)
require.NoError(t, err)
err = SetBodilessMatcher(s.T(), nil)
require.NoError(err)

req, err = http.NewRequest("POST", "https://localhost:5001", bytes.NewReader([]byte("abcdef")))
require.NoError(t, err)
req, err = http.NewRequest("POST", defaultOptions().baseURL(), bytes.NewReader([]byte("abcdef")))
require.NoError(err)

req.Header.Set(UpstreamURIHeader, "https://bing.com")
req.Header.Set(ModeHeader, GetRecordMode())
req.Header.Set(IDHeader, GetRecordingId(t))
req.Header.Set(IDHeader, GetRecordingId(s.T()))

_, err = client.Do(req)
require.NoError(t, err)
require.NoError(err)

err = Stop(t, nil)
require.NoError(t, err)
err = Stop(s.T(), nil)
require.NoError(err)

err = ResetProxy(nil)
require.NoError(t, err)
require.NoError(err)
}

func TestSetBodilessMatcherNilTest(t *testing.T) {
func (s *matchersTests) TestSetBodilessMatcherNilTest() {
require := require.New(s.T())
temp := recordMode
recordMode = RecordingMode
defer func() { recordMode = temp }()

err := Start(t, packagePath, nil)
require.NoError(t, err)
err := Start(s.T(), packagePath, nil)
require.NoError(err)

req, err := http.NewRequest("POST", "https://localhost:5001", nil)
require.NoError(t, err)
req, err := http.NewRequest("POST", defaultOptions().baseURL(), nil)
require.NoError(err)

req.Header.Set(UpstreamURIHeader, "https://bing.com")
req.Header.Set(ModeHeader, GetRecordMode())
req.Header.Set(IDHeader, GetRecordingId(t))
req.Header.Set(IDHeader, GetRecordingId(s.T()))

client, err := GetHTTPClient(t)
require.NoError(t, err)
client, err := GetHTTPClient(s.T())
require.NoError(err)

_, err = client.Do(req)
require.NoError(t, err)
require.NoError(err)

err = Stop(t, nil)
require.NoError(t, err)
err = Stop(s.T(), nil)
require.NoError(err)

// Run a second request to with different body to verify it works
recordMode = PlaybackMode

err = Start(t, packagePath, nil)
require.NoError(t, err)
err = Start(s.T(), packagePath, nil)
require.NoError(err)

err = SetBodilessMatcher(nil, nil)
require.NoError(t, err)
require.NoError(err)

req, err = http.NewRequest("POST", "https://localhost:5001", bytes.NewReader([]byte("abcdef")))
require.NoError(t, err)
req, err = http.NewRequest("POST", defaultOptions().baseURL(), bytes.NewReader([]byte("abcdef")))
require.NoError(err)

req.Header.Set(UpstreamURIHeader, "https://bing.com")
req.Header.Set(ModeHeader, GetRecordMode())
req.Header.Set(IDHeader, GetRecordingId(t))
req.Header.Set(IDHeader, GetRecordingId(s.T()))

_, err = client.Do(req)
require.NoError(t, err)
require.NoError(err)

err = Stop(t, nil)
require.NoError(t, err)
err = Stop(s.T(), nil)
require.NoError(err)

err = ResetProxy(nil)
require.NoError(t, err)
require.NoError(err)
}

func TestSetDefaultMatcher(t *testing.T) {
func (s *matchersTests) TestSetDefaultMatcher() {
require := require.New(s.T())
temp := recordMode
recordMode = RecordingMode
defer func() { recordMode = temp }()

err := Start(t, packagePath, nil)
require.NoError(t, err)
err := Start(s.T(), packagePath, nil)
require.NoError(err)

req, err := http.NewRequest("POST", "https://localhost:5001", nil)
require.NoError(t, err)
req, err := http.NewRequest("POST", defaultOptions().baseURL(), nil)
require.NoError(err)

req.Header.Set(UpstreamURIHeader, "https://bing.com")
req.Header.Set(ModeHeader, GetRecordMode())
req.Header.Set(IDHeader, GetRecordingId(t))
req.Header.Set(IDHeader, GetRecordingId(s.T()))

client, err := GetHTTPClient(t)
require.NoError(t, err)
client, err := GetHTTPClient(s.T())
require.NoError(err)

_, err = client.Do(req)
require.NoError(t, err)
require.NoError(err)

err = Stop(t, nil)
require.NoError(t, err)
err = Stop(s.T(), nil)
require.NoError(err)

// Run a second request to with different body to verify it works
recordMode = PlaybackMode

err = Start(t, packagePath, nil)
require.NoError(t, err)
err = Start(s.T(), packagePath, nil)
require.NoError(err)

err = SetDefaultMatcher(nil, &SetDefaultMatcherOptions{ExcludedHeaders: []string{"ExampleHeader"}})
require.NoError(t, err)
require.NoError(err)

req, err = http.NewRequest("POST", "https://localhost:5001", nil)
require.NoError(t, err)
req, err = http.NewRequest("POST", defaultOptions().baseURL(), nil)
require.NoError(err)

req.Header.Set(UpstreamURIHeader, "https://bing.com")
req.Header.Set(ModeHeader, GetRecordMode())
req.Header.Set(IDHeader, GetRecordingId(t))
req.Header.Set(IDHeader, GetRecordingId(s.T()))
req.Header.Set("ExampleHeader", "blah-blah-blah")

err = handleProxyResponse(client.Do(req))
require.NoError(t, err)
require.NoError(err)

err = Stop(t, nil)
require.NoError(t, err)
err = Stop(s.T(), nil)
require.NoError(err)

err = ResetProxy(nil)
require.NoError(t, err)
require.NoError(err)
}

func TestAddDefaults(t *testing.T) {
require.Equal(t, 4, len(addDefaults([]string{})))
require.Equal(t, 4, len(addDefaults([]string{":path"})))
require.Equal(t, 4, len(addDefaults([]string{":path", ":authority"})))
require.Equal(t, 4, len(addDefaults([]string{":path", ":authority", ":method"})))
require.Equal(t, 4, len(addDefaults([]string{":path", ":authority", ":method", ":scheme"})))
require.Equal(t, 5, len(addDefaults([]string{":path", ":authority", ":method", ":scheme", "extra"})))
func (s *matchersTests) TestAddDefaults() {
require := require.New(s.T())
require.Equal(4, len(addDefaults([]string{})))
require.Equal(4, len(addDefaults([]string{":path"})))
require.Equal(4, len(addDefaults([]string{":path", ":authority"})))
require.Equal(4, len(addDefaults([]string{":path", ":authority", ":method"})))
require.Equal(4, len(addDefaults([]string{":path", ":authority", ":method", ":scheme"})))
require.Equal(5, len(addDefaults([]string{":path", ":authority", ":method", ":scheme", "extra"})))
}
5 changes: 3 additions & 2 deletions sdk/internal/recording/recording.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,8 +673,6 @@ func requestStart(url string, testId string, assetConfigLocation string) (*http.
return client.Do(req)
}

// Start optionally installs and starts a test proxy instance
// and tells the test proxy instance to begin accepting requests for a given test
func Start(t *testing.T, pathToRecordings string, options *RecordingOptions) error {
if options == nil {
options = defaultOptions()
Expand Down Expand Up @@ -796,6 +794,9 @@ func Stop(t *testing.T, options *RecordingOptions) error {
req.Header.Set(IDHeader, recTest.recordingId)
testSuite.Remove(t.Name())
resp, err := client.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
b, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
Expand Down
Loading

0 comments on commit 9301849

Please sign in to comment.