Skip to content

Commit 646989c

Browse files
coggsflcoggsflod
andauthored
Improve (#356) to support registration of wildcard URLs (#359)
* Improve (#356) to support registration of wildcard URLs * Add TestAzureChatCompletions & TestAzureChatCompletionsWithCustomDeploymentName * Remove TestAzureChatCompletionsWithCustomDeploymentName --------- Co-authored-by: coggsflod <[email protected]>
1 parent 3f4e3bb commit 646989c

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

chat_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ func TestChatCompletions(t *testing.T) {
6767
checks.NoError(t, err, "CreateChatCompletion error")
6868
}
6969

70+
func TestAzureChatCompletions(t *testing.T) {
71+
client, server, teardown := setupAzureTestServer()
72+
defer teardown()
73+
server.RegisterHandler("/openai/deployments/*", handleChatCompletionEndpoint)
74+
75+
_, err := client.CreateChatCompletion(context.Background(), ChatCompletionRequest{
76+
MaxTokens: 5,
77+
Model: GPT3Dot5Turbo,
78+
Messages: []ChatCompletionMessage{
79+
{
80+
Role: ChatMessageRoleUser,
81+
Content: "Hello!",
82+
},
83+
},
84+
})
85+
checks.NoError(t, err, "CreateAzureChatCompletion error")
86+
}
87+
7088
// handleChatCompletionEndpoint Handles the ChatGPT completion endpoint by the test server.
7189
func handleChatCompletionEndpoint(w http.ResponseWriter, r *http.Request) {
7290
var err error

internal/test/server.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"net/http"
66
"net/http/httptest"
7+
"regexp"
78
)
89

910
const testAPI = "this-is-my-secure-token-do-not-steal!!"
@@ -36,11 +37,14 @@ func (ts *ServerTest) OpenAITestServer() *httptest.Server {
3637
return
3738
}
3839

39-
handlerCall, ok := ts.handlers[r.URL.Path]
40-
if !ok {
41-
http.Error(w, "the resource path doesn't exist", http.StatusNotFound)
42-
return
40+
// Handle /path/* routes.
41+
for route, handler := range ts.handlers {
42+
pattern, _ := regexp.Compile(route)
43+
if pattern.MatchString(r.URL.Path) {
44+
handler(w, r)
45+
return
46+
}
4347
}
44-
handlerCall(w, r)
48+
http.Error(w, "the resource path doesn't exist", http.StatusNotFound)
4549
}))
4650
}

0 commit comments

Comments
 (0)