@@ -8,14 +8,18 @@ package azopenai_test
88
99import (
1010 "context"
11+ "fmt"
1112 "net/http"
13+ "net/http/httptest"
1214 "reflect"
1315 "testing"
1416
1517 "github.com/Azure/azure-sdk-for-go/sdk/ai/azopenai"
1618 "github.com/Azure/azure-sdk-for-go/sdk/azcore"
19+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1720 "github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
1821 "github.com/Azure/azure-sdk-for-go/sdk/internal/recording"
22+ "github.com/Azure/azure-sdk-for-go/sdk/internal/test/credential"
1923 "github.com/stretchr/testify/require"
2024)
2125
@@ -57,3 +61,93 @@ func TestClient_EmptyOptionsChecking(t *testing.T) {
5761 require .Emptyf (t , fields , "%T is ignored in our function signatures because it's empty" , v )
5862 }
5963}
64+
65+ func TestClient_InsecureHTTPAllowed (t * testing.T ) {
66+ const fakeID = "fake-id"
67+
68+ hf := func (resp http.ResponseWriter , req * http.Request ) {
69+ // _just_ enough of a response to prove we made it through the pipeline.
70+ _ , err := resp .Write ([]byte (fmt .Sprintf ("{ \" id\" : \" %s\" }" , fakeID )))
71+ require .NoError (t , err )
72+ }
73+
74+ urlCh := make (chan string )
75+
76+ go func () {
77+ // start an HTTP service
78+ server := httptest .NewServer (http .HandlerFunc (hf ))
79+ urlCh <- server .URL
80+ t .Cleanup (server .Close )
81+ }()
82+
83+ url := <- urlCh
84+ t .Logf (url )
85+
86+ t .Run ("DefaultsToHTTPSOnly" , func (t * testing.T ) {
87+ client , err := azopenai .NewClientForOpenAI (url , azcore .NewKeyCredential ("fake-key" ), nil )
88+ require .NoError (t , err )
89+
90+ resp , err := client .GetChatCompletions (context .Background (), azopenai.ChatCompletionsOptions {
91+ Messages : []azopenai.ChatRequestMessageClassification {},
92+ }, nil )
93+ require .Empty (t , resp )
94+ require .EqualError (t , err , "authenticated requests are not permitted for non TLS protected (https) endpoints" )
95+
96+ client , err = azopenai .NewClientWithKeyCredential (url , azcore .NewKeyCredential ("fake-key" ), nil )
97+ require .NoError (t , err )
98+
99+ resp , err = client .GetChatCompletions (context .Background (), azopenai.ChatCompletionsOptions {
100+ Messages : []azopenai.ChatRequestMessageClassification {},
101+ }, nil )
102+ require .Empty (t , resp )
103+ require .EqualError (t , err , "authenticated requests are not permitted for non TLS protected (https) endpoints" )
104+
105+ fakeCred := & credential.Fake {}
106+
107+ client , err = azopenai .NewClient (url , fakeCred , nil )
108+ require .NoError (t , err )
109+
110+ resp , err = client .GetChatCompletions (context .Background (), azopenai.ChatCompletionsOptions {
111+ Messages : []azopenai.ChatRequestMessageClassification {},
112+ }, nil )
113+ require .Empty (t , resp )
114+ require .EqualError (t , err , "authenticated requests are not permitted for non TLS protected (https) endpoints" )
115+ })
116+
117+ t .Run ("InsecureAllowCredentialWithHTTP" , func (t * testing.T ) {
118+ clientOptions := & azopenai.ClientOptions {
119+ ClientOptions : policy.ClientOptions {
120+ InsecureAllowCredentialWithHTTP : true ,
121+ },
122+ }
123+
124+ client , err := azopenai .NewClientForOpenAI (url , azcore .NewKeyCredential ("fake-key" ), clientOptions )
125+ require .NoError (t , err )
126+
127+ resp , err := client .GetChatCompletions (context .Background (), azopenai.ChatCompletionsOptions {
128+ Messages : []azopenai.ChatRequestMessageClassification {},
129+ }, nil )
130+ require .NoError (t , err )
131+ require .Equal (t , fakeID , * resp .ID )
132+
133+ client , err = azopenai .NewClientWithKeyCredential (url , azcore .NewKeyCredential ("fake-key" ), clientOptions )
134+ require .NoError (t , err )
135+
136+ resp , err = client .GetChatCompletions (context .Background (), azopenai.ChatCompletionsOptions {
137+ Messages : []azopenai.ChatRequestMessageClassification {},
138+ }, nil )
139+ require .NoError (t , err )
140+ require .Equal (t , fakeID , * resp .ID )
141+
142+ fakeCred := & credential.Fake {}
143+
144+ client , err = azopenai .NewClient (url , fakeCred , clientOptions )
145+ require .NoError (t , err )
146+
147+ resp , err = client .GetChatCompletions (context .Background (), azopenai.ChatCompletionsOptions {
148+ Messages : []azopenai.ChatRequestMessageClassification {},
149+ }, nil )
150+ require .NoError (t , err )
151+ require .Equal (t , fakeID , * resp .ID )
152+ })
153+ }
0 commit comments