1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
package polly
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/polly/types"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
"io/ioutil"
"testing"
)
func TestPresignOpSynthesizeSpeechInput(t *testing.T) {
cases := map[string]struct {
LexiconNames []string
OutputFormat types.OutputFormat
SampleRate *string
Text *string
TextType types.TextType
VoiceID types.VoiceId
ExpectStream string
Error error
ExpectError bool
}{
"Single LexiconNames": {
LexiconNames: []string{"abc"},
OutputFormat: types.OutputFormatMp3,
SampleRate: aws.String("128"),
Text: aws.String("Test"),
TextType: types.TextTypeText,
VoiceID: types.VoiceIdAmy,
ExpectStream: "LexiconNames=abc&OutputFormat=mp3&SampleRate=128&Text=Test&TextType=text&VoiceId=Amy",
},
"Multiple LexiconNames": {
LexiconNames: []string{"abc", "mno"},
OutputFormat: types.OutputFormatMp3,
SampleRate: aws.String("128"),
Text: aws.String("Test"),
TextType: types.TextTypeText,
VoiceID: types.VoiceIdAmy,
ExpectStream: "LexiconNames=abc&LexiconNames=mno&OutputFormat=mp3&SampleRate=128&Text=Test&TextType=text&VoiceId=Amy",
},
"Text needs parsing": {
LexiconNames: []string{"abc", "mno"},
OutputFormat: types.OutputFormatMp3,
SampleRate: aws.String("128"),
Text: aws.String("Test /Text"),
TextType: types.TextTypeText,
VoiceID: types.VoiceIdAmy,
ExpectStream: "LexiconNames=abc&LexiconNames=mno&OutputFormat=mp3&SampleRate=128&Text=Test+%2FText&TextType=text&VoiceId=Amy",
},
"Next serializer return error": {
Error: fmt.Errorf("next handler return error"),
ExpectError: true,
},
}
for name, c := range cases {
t.Run(name, func(t *testing.T) {
req := smithyhttp.NewStackRequest().(*smithyhttp.Request)
var updatedRequest *smithyhttp.Request
param := &SynthesizeSpeechInput{
LexiconNames: c.LexiconNames,
OutputFormat: c.OutputFormat,
SampleRate: c.SampleRate,
Text: c.Text,
TextType: c.TextType,
VoiceId: c.VoiceID,
}
m := presignOpSynthesizeSpeechInput{}
_, _, err := m.HandleSerialize(context.Background(),
middleware.SerializeInput{
Request: req,
Parameters: param,
},
middleware.SerializeHandlerFunc(func(ctx context.Context, input middleware.SerializeInput) (
out middleware.SerializeOutput, metadata middleware.Metadata, err error) {
updatedRequest = input.Request.(*smithyhttp.Request)
return out, metadata, c.Error
}),
)
if err != nil && !c.ExpectError {
t.Fatalf("expect no error, got %v", err)
} else if err != nil != c.ExpectError {
t.Fatalf("expect error but got nil")
}
stream := updatedRequest.GetStream()
b, _ := ioutil.ReadAll(stream)
if e, a := c.ExpectStream, string(b); e != a {
t.Errorf("expect request stream value %v, got %v", e, a)
}
})
}
}
|