@@ -2,182 +2,16 @@ package openai //nolint:testpackage // testing private field
2
2
3
3
import (
4
4
"bytes"
5
- "context"
6
- "errors"
7
5
"fmt"
8
6
"io"
9
- "mime"
10
- "mime/multipart"
11
- "net/http"
12
7
"os"
13
8
"path/filepath"
14
- "strings"
15
9
"testing"
16
10
17
11
"github.com./sashabaranov/go-openai/internal/test"
18
12
"github.com./sashabaranov/go-openai/internal/test/checks"
19
13
)
20
14
21
- // TestAudio Tests the transcription and translation endpoints of the API using the mocked server.
22
- func TestAudio (t * testing.T ) {
23
- server := test .NewTestServer ()
24
- server .RegisterHandler ("/v1/audio/transcriptions" , handleAudioEndpoint )
25
- server .RegisterHandler ("/v1/audio/translations" , handleAudioEndpoint )
26
- // create the test server
27
- var err error
28
- ts := server .OpenAITestServer ()
29
- ts .Start ()
30
- defer ts .Close ()
31
-
32
- config := DefaultConfig (test .GetTestToken ())
33
- config .BaseURL = ts .URL + "/v1"
34
- client := NewClientWithConfig (config )
35
-
36
- testcases := []struct {
37
- name string
38
- createFn func (context.Context , AudioRequest ) (AudioResponse , error )
39
- }{
40
- {
41
- "transcribe" ,
42
- client .CreateTranscription ,
43
- },
44
- {
45
- "translate" ,
46
- client .CreateTranslation ,
47
- },
48
- }
49
-
50
- ctx := context .Background ()
51
-
52
- dir , cleanup := test .CreateTestDirectory (t )
53
- defer cleanup ()
54
-
55
- for _ , tc := range testcases {
56
- t .Run (tc .name , func (t * testing.T ) {
57
- path := filepath .Join (dir , "fake.mp3" )
58
- test .CreateTestFile (t , path )
59
-
60
- req := AudioRequest {
61
- FilePath : path ,
62
- Model : "whisper-3" ,
63
- }
64
- _ , err = tc .createFn (ctx , req )
65
- checks .NoError (t , err , "audio API error" )
66
- })
67
-
68
- t .Run (tc .name + " (with reader)" , func (t * testing.T ) {
69
- req := AudioRequest {
70
- FilePath : "fake.webm" ,
71
- Reader : bytes .NewBuffer ([]byte (`some webm binary data` )),
72
- Model : "whisper-3" ,
73
- }
74
- _ , err = tc .createFn (ctx , req )
75
- checks .NoError (t , err , "audio API error" )
76
- })
77
- }
78
- }
79
-
80
- func TestAudioWithOptionalArgs (t * testing.T ) {
81
- server := test .NewTestServer ()
82
- server .RegisterHandler ("/v1/audio/transcriptions" , handleAudioEndpoint )
83
- server .RegisterHandler ("/v1/audio/translations" , handleAudioEndpoint )
84
- // create the test server
85
- var err error
86
- ts := server .OpenAITestServer ()
87
- ts .Start ()
88
- defer ts .Close ()
89
-
90
- config := DefaultConfig (test .GetTestToken ())
91
- config .BaseURL = ts .URL + "/v1"
92
- client := NewClientWithConfig (config )
93
-
94
- testcases := []struct {
95
- name string
96
- createFn func (context.Context , AudioRequest ) (AudioResponse , error )
97
- }{
98
- {
99
- "transcribe" ,
100
- client .CreateTranscription ,
101
- },
102
- {
103
- "translate" ,
104
- client .CreateTranslation ,
105
- },
106
- }
107
-
108
- ctx := context .Background ()
109
-
110
- dir , cleanup := test .CreateTestDirectory (t )
111
- defer cleanup ()
112
-
113
- for _ , tc := range testcases {
114
- t .Run (tc .name , func (t * testing.T ) {
115
- path := filepath .Join (dir , "fake.mp3" )
116
- test .CreateTestFile (t , path )
117
-
118
- req := AudioRequest {
119
- FilePath : path ,
120
- Model : "whisper-3" ,
121
- Prompt : "用简体中文" ,
122
- Temperature : 0.5 ,
123
- Language : "zh" ,
124
- Format : AudioResponseFormatSRT ,
125
- }
126
- _ , err = tc .createFn (ctx , req )
127
- checks .NoError (t , err , "audio API error" )
128
- })
129
- }
130
- }
131
-
132
- // handleAudioEndpoint Handles the completion endpoint by the test server.
133
- func handleAudioEndpoint (w http.ResponseWriter , r * http.Request ) {
134
- var err error
135
-
136
- // audio endpoints only accept POST requests
137
- if r .Method != "POST" {
138
- http .Error (w , "method not allowed" , http .StatusMethodNotAllowed )
139
- }
140
-
141
- mediaType , params , err := mime .ParseMediaType (r .Header .Get ("Content-Type" ))
142
- if err != nil {
143
- http .Error (w , "failed to parse media type" , http .StatusBadRequest )
144
- return
145
- }
146
-
147
- if ! strings .HasPrefix (mediaType , "multipart" ) {
148
- http .Error (w , "request is not multipart" , http .StatusBadRequest )
149
- }
150
-
151
- boundary , ok := params ["boundary" ]
152
- if ! ok {
153
- http .Error (w , "no boundary in params" , http .StatusBadRequest )
154
- return
155
- }
156
-
157
- fileData := & bytes.Buffer {}
158
- mr := multipart .NewReader (r .Body , boundary )
159
- part , err := mr .NextPart ()
160
- if err != nil && errors .Is (err , io .EOF ) {
161
- http .Error (w , "error accessing file" , http .StatusBadRequest )
162
- return
163
- }
164
- if _ , err = io .Copy (fileData , part ); err != nil {
165
- http .Error (w , "failed to copy file" , http .StatusInternalServerError )
166
- return
167
- }
168
-
169
- if len (fileData .Bytes ()) == 0 {
170
- w .WriteHeader (http .StatusInternalServerError )
171
- http .Error (w , "received empty file data" , http .StatusBadRequest )
172
- return
173
- }
174
-
175
- if _ , err = w .Write ([]byte (`{"body": "hello"}` )); err != nil {
176
- http .Error (w , "failed to write body" , http .StatusInternalServerError )
177
- return
178
- }
179
- }
180
-
181
15
func TestAudioWithFailingFormBuilder (t * testing.T ) {
182
16
dir , cleanup := test .CreateTestDirectory (t )
183
17
defer cleanup ()
0 commit comments