* move error_accumulator into internal pkg (#304) * move error_accumulator into internal pkg (#304) * add a test for ErrTooManyEmptyStreamMessages in stream_reader (#304)
This commit is contained in:
committed by
GitHub
parent
fa694c61c2
commit
1394329e44
21
internal/test/failer.go
Normal file
21
internal/test/failer.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package test
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrTestErrorAccumulatorWriteFailed = errors.New("test error accumulator failed")
|
||||
)
|
||||
|
||||
type FailingErrorBuffer struct{}
|
||||
|
||||
func (b *FailingErrorBuffer) Write(_ []byte) (n int, err error) {
|
||||
return 0, ErrTestErrorAccumulatorWriteFailed
|
||||
}
|
||||
|
||||
func (b *FailingErrorBuffer) Len() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (b *FailingErrorBuffer) Bytes() []byte {
|
||||
return []byte{}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package test
|
||||
import (
|
||||
"github.com/sashabaranov/go-openai/internal/test/checks"
|
||||
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
@@ -27,3 +28,26 @@ func CreateTestDirectory(t *testing.T) (path string, cleanup func()) {
|
||||
|
||||
return path, func() { os.RemoveAll(path) }
|
||||
}
|
||||
|
||||
// TokenRoundTripper is a struct that implements the RoundTripper
|
||||
// interface, specifically to handle the authentication token by adding a token
|
||||
// to the request header. We need this because the API requires that each
|
||||
// request include a valid API token in the headers for authentication and
|
||||
// authorization.
|
||||
type TokenRoundTripper struct {
|
||||
Token string
|
||||
Fallback http.RoundTripper
|
||||
}
|
||||
|
||||
// RoundTrip takes an *http.Request as input and returns an
|
||||
// *http.Response and an error.
|
||||
//
|
||||
// It is expected to use the provided request to create a connection to an HTTP
|
||||
// server and return the response, or an error if one occurred. The returned
|
||||
// Response should have its Body closed. If the RoundTrip method returns an
|
||||
// error, the Client's Get, Head, Post, and PostForm methods return the same
|
||||
// error.
|
||||
func (t *TokenRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
req.Header.Set("Authorization", "Bearer "+t.Token)
|
||||
return t.Fallback.RoundTrip(req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user