-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from microsoft/feat/middleware-with-options
Adds middleware with overridable options
- Loading branch information
Showing
4 changed files
with
179 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package nethttplibrary | ||
|
||
import ( | ||
abstractions "github.com/microsoft/kiota-abstractions-go" | ||
"github.com/stretchr/testify/assert" | ||
nethttp "net/http" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestGetDefaultMiddleWareWithMultipleOptions(t *testing.T) { | ||
retryOptions := RetryHandlerOptions{ | ||
ShouldRetry: func(delay time.Duration, executionCount int, request *nethttp.Request, response *nethttp.Response) bool { | ||
return false | ||
}, | ||
} | ||
redirectHandlerOptions := RedirectHandlerOptions{ | ||
MaxRedirects: defaultMaxRedirects, | ||
ShouldRedirect: func(req *nethttp.Request, res *nethttp.Response) bool { | ||
return true | ||
}, | ||
} | ||
compressionOptions := NewCompressionOptions(false) | ||
parametersNameDecodingOptions := ParametersNameDecodingOptions{ | ||
Enable: true, | ||
ParametersToDecode: []byte{'-', '.', '~', '$'}, | ||
} | ||
userAgentHandlerOptions := UserAgentHandlerOptions{ | ||
Enabled: true, | ||
ProductName: "kiota-go", | ||
ProductVersion: "1.1.0", | ||
} | ||
headersInspectionOptions := HeadersInspectionOptions{ | ||
RequestHeaders: abstractions.NewRequestHeaders(), | ||
ResponseHeaders: abstractions.NewResponseHeaders(), | ||
} | ||
options, err := GetDefaultMiddlewaresWithOptions(&retryOptions, | ||
&redirectHandlerOptions, | ||
&compressionOptions, | ||
¶metersNameDecodingOptions, | ||
&userAgentHandlerOptions, | ||
&headersInspectionOptions, | ||
) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
if len(options) != 6 { | ||
t.Errorf("expected 6 middleware, got %v", len(options)) | ||
} | ||
|
||
for _, element := range options { | ||
switch v := element.(type) { | ||
case *CompressionHandler: | ||
assert.Equal(t, v.options.ShouldCompress(), compressionOptions.ShouldCompress()) | ||
} | ||
} | ||
} | ||
|
||
func TestGetDefaultMiddleWareWithInvalidOption(t *testing.T) { | ||
chaosOptions := ChaosHandlerOptions{ | ||
ChaosPercentage: 101, | ||
ChaosStrategy: Random, | ||
} | ||
_, err := GetDefaultMiddlewaresWithOptions(&chaosOptions) | ||
|
||
assert.Equal(t, err.Error(), "unsupported option type") | ||
} | ||
|
||
func TestGetDefaultMiddleWareWithOptions(t *testing.T) { | ||
compression := NewCompressionOptions(false) | ||
options, err := GetDefaultMiddlewaresWithOptions(&compression) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
if len(options) != 6 { | ||
t.Errorf("expected 6 middleware, got %v", len(options)) | ||
} | ||
|
||
for _, element := range options { | ||
switch v := element.(type) { | ||
case *CompressionHandler: | ||
assert.Equal(t, v.options.ShouldCompress(), compression.ShouldCompress()) | ||
} | ||
} | ||
} | ||
|
||
func TestGetDefaultMiddlewares(t *testing.T) { | ||
options := GetDefaultMiddlewares() | ||
if len(options) != 6 { | ||
t.Errorf("expected 6 middleware, got %v", len(options)) | ||
} | ||
|
||
for _, element := range options { | ||
switch v := element.(type) { | ||
case *CompressionHandler: | ||
assert.True(t, v.options.ShouldCompress()) | ||
} | ||
} | ||
} |