-
Notifications
You must be signed in to change notification settings - Fork 26
/
audiospeech_test.go
56 lines (51 loc) · 1.43 KB
/
audiospeech_test.go
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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package openai_test
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
func TestAudioSpeechNewWithOptionalParams(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("abc"))
}))
defer server.Close()
baseURL := server.URL
client := openai.NewClient(
option.WithBaseURL(baseURL),
option.WithAPIKey("My API Key"),
)
resp, err := client.Audio.Speech.New(context.TODO(), openai.AudioSpeechNewParams{
Input: openai.F("input"),
Model: openai.F(openai.SpeechModelTTS1),
Voice: openai.F(openai.AudioSpeechNewParamsVoiceAlloy),
ResponseFormat: openai.F(openai.AudioSpeechNewParamsResponseFormatMP3),
Speed: openai.F(0.250000),
})
if err != nil {
var apierr *openai.Error
if errors.As(err, &apierr) {
t.Log(string(apierr.DumpRequest(true)))
}
t.Fatalf("err should be nil: %s", err.Error())
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
var apierr *openai.Error
if errors.As(err, &apierr) {
t.Log(string(apierr.DumpRequest(true)))
}
t.Fatalf("err should be nil: %s", err.Error())
}
if !bytes.Equal(b, []byte("abc")) {
t.Fatalf("return value not %s: %s", "abc", b)
}
}