refactor: refactoring http request creation and sending (#395)

* refactoring http request creation and sending

* fix lint error

* increase the test coverage of client.go

* refactor: Change the style of HTTPRequestBuilder.Build func to one-argument-per-line.
This commit is contained in:
渡邉祐一 / Yuichi Watanabe
2023-06-22 18:57:52 +09:00
committed by GitHub
parent 157de0680f
commit f1b66967a4
20 changed files with 215 additions and 132 deletions

View File

@@ -1,6 +1,9 @@
package openai_test
import (
"os"
"time"
. "github.com/sashabaranov/go-openai"
"github.com/sashabaranov/go-openai/internal/test/checks"
@@ -56,3 +59,22 @@ func handleGetModelEndpoint(w http.ResponseWriter, _ *http.Request) {
resBytes, _ := json.Marshal(Model{})
fmt.Fprintln(w, string(resBytes))
}
func TestGetModelReturnTimeoutError(t *testing.T) {
client, server, teardown := setupOpenAITestServer()
defer teardown()
server.RegisterHandler("/v1/models/text-davinci-003", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(10 * time.Nanosecond)
})
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, time.Nanosecond)
defer cancel()
_, err := client.GetModel(ctx, "text-davinci-003")
if err == nil {
t.Fatal("Did not return error")
}
if !os.IsTimeout(err) {
t.Fatal("Did not return timeout error")
}
}