Skip to content

Commit

Permalink
SetBodilessMatcher & SetDefaultMatcher take RecordingOptions (#22405)
Browse files Browse the repository at this point in the history
  • Loading branch information
chlowell authored Feb 20, 2024
1 parent 9732d67 commit bf6e7f3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion sdk/internal/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Release History

## 1.5.3 (Unreleased)
## 1.6.0 (Unreleased)

### Features Added
* Options types for `SetBodilessMatcher` and `SetDefaultMatcher` now embed `RecordingOptions`

### Breaking Changes

Expand Down
17 changes: 13 additions & 4 deletions sdk/internal/recording/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,30 @@ import (
"fmt"
"io"
"net/http"
"reflect"
"strings"
"testing"
)

// Optional parameters for the SetBodilessMatcher operation
type MatcherOptions struct {
RecordingOptions
}

// SetBodilessMatcher adjusts the "match" operation to exclude the body when matching a request to a recording's entries.
// Pass in `nil` for `t` if you want the bodiless matcher to apply everywhere
func SetBodilessMatcher(t *testing.T, options *MatcherOptions) error {
f := false
return SetDefaultMatcher(t, &SetDefaultMatcherOptions{
CompareBodies: &f,
})
o := SetDefaultMatcherOptions{CompareBodies: &f}
if options != nil {
o.RecordingOptions = options.RecordingOptions
}
return SetDefaultMatcher(t, &o)
}

type SetDefaultMatcherOptions struct {
RecordingOptions

CompareBodies *bool
ExcludedHeaders []string
IgnoredHeaders []string
Expand All @@ -53,6 +59,9 @@ func (s *SetDefaultMatcherOptions) fillOptions() {
if s.IgnoreQueryOrdering == nil {
s.IgnoreQueryOrdering = &f
}
if reflect.ValueOf(s.RecordingOptions).IsZero() {
s.RecordingOptions = *defaultOptions()
}
}

func addDefaults(added []string) []string {
Expand All @@ -77,7 +86,7 @@ func SetDefaultMatcher(t *testing.T, options *SetDefaultMatcherOptions) error {
return nil
}
options.fillOptions()
url := fmt.Sprintf("%s/Admin/SetMatcher", defaultOptions().baseURL())
url := fmt.Sprintf("%s/Admin/SetMatcher", options.baseURL())
req, err := http.NewRequest("POST", url, http.NoBody)
if err != nil {
panic(err)
Expand Down
22 changes: 22 additions & 0 deletions sdk/internal/recording/matchers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,35 @@ package recording
import (
"bytes"
"net/http"
"net/url"
"os"
"strconv"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/internal/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

func TestSetMatcherRecordingOptions(t *testing.T) {
srv, close := mock.NewServer()
defer close()
srv.SetResponse(mock.WithStatusCode(http.StatusOK))
parsed, err := url.Parse(srv.URL())
require.NoError(t, err)
port, err := strconv.ParseInt(parsed.Port(), 10, 0)
require.NoError(t, err)
ro := RecordingOptions{ProxyPort: int(port)}
t.Run("SetBodilessMatcher", func(t *testing.T) {
err := SetBodilessMatcher(t, &MatcherOptions{RecordingOptions: ro})
require.NoError(t, err)
})
t.Run("SetDefaultMatcher", func(t *testing.T) {
err = SetDefaultMatcher(nil, &SetDefaultMatcherOptions{RecordingOptions: ro})
require.NoError(t, err)
})
}

type matchersTests struct {
suite.Suite
proxy *TestProxyInstance
Expand Down
2 changes: 1 addition & 1 deletion sdk/internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ const (
Module = "internal"

// Version is the semantic version (see http://semver.org) of this module.
Version = "v1.5.3"
Version = "v1.6.0"
)

0 comments on commit bf6e7f3

Please sign in to comment.