| 1 |
//go:build go1.10 |
| 2 |
// +build go1.10 |
| 3 |
|
| 4 |
package transcribestreamingservice |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"io/ioutil" |
| 9 |
"net/http" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/aws/aws-sdk-go/aws" |
| 14 |
"github.com/aws/aws-sdk-go/aws/credentials" |
| 15 |
"github.com/aws/aws-sdk-go/aws/session" |
| 16 |
) |
| 17 |
|
| 18 |
type roundTripFunc func(req *http.Request) *http.Response |
| 19 |
|
| 20 |
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { |
| 21 |
return f(req), nil |
| 22 |
} |
| 23 |
|
| 24 |
func newTestClient(fn roundTripFunc) *http.Client { |
| 25 |
return &http.Client{ |
| 26 |
Transport: fn, |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
func TestStartStreamTranscription_Error(t *testing.T) { |
| 31 |
cfg := &aws.Config{ |
| 32 |
Region: aws.String("us-west-2"), |
| 33 |
Credentials: credentials.AnonymousCredentials, |
| 34 |
HTTPClient: newTestClient(func(req *http.Request) *http.Response { |
| 35 |
return &http.Response{ |
| 36 |
StatusCode: http.StatusBadRequest, |
| 37 |
Body: ioutil.NopCloser(bytes.NewReader([]byte("{ \"code\" : \"BadRequestException\" }"))), |
| 38 |
Header: http.Header{}, |
| 39 |
} |
| 40 |
}), |
| 41 |
} |
| 42 |
sess, err := session.NewSession(cfg) |
| 43 |
|
| 44 |
svc := New(sess) |
| 45 |
resp, err := svc.StartStreamTranscription(&StartStreamTranscriptionInput{ |
| 46 |
LanguageCode: aws.String(LanguageCodeEnUs), |
| 47 |
MediaEncoding: aws.String(MediaEncodingPcm), |
| 48 |
MediaSampleRateHertz: aws.Int64(int64(16000)), |
| 49 |
}) |
| 50 |
if err == nil { |
| 51 |
t.Fatalf("expect error, got none") |
| 52 |
} else { |
| 53 |
if e, a := "BadRequestException", err.Error(); !strings.Contains(a, e) { |
| 54 |
t.Fatalf("expected error to be %v, got %v", e, a) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
n, err := resp.GetStream().inputWriter.Write([]byte("text")) |
| 59 |
if err == nil { |
| 60 |
t.Fatalf("expected error stating write on closed pipe, got none") |
| 61 |
} |
| 62 |
|
| 63 |
if e, a := "write on closed pipe", err.Error(); !strings.Contains(a, e) { |
| 64 |
t.Fatalf("expected error to contain %v, got error as %v", e, a) |
| 65 |
} |
| 66 |
|
| 67 |
if e, a := 0, n; e != a { |
| 68 |
t.Fatalf("expected %d bytes to be written on inputWriter, but %v bytes were written", e, a) |
| 69 |
} |
| 70 |
} |