|
| 1 | +package openai |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestOpenAIFullURL(t *testing.T) { |
| 9 | + cases := []struct { |
| 10 | + Name string |
| 11 | + Suffix string |
| 12 | + Expect string |
| 13 | + }{ |
| 14 | + { |
| 15 | + "ChatCompletionsURL", |
| 16 | + "/chat/completions", |
| 17 | + "https://api.openai.com/v1/chat/completions", |
| 18 | + }, |
| 19 | + { |
| 20 | + "CompletionsURL", |
| 21 | + "/completions", |
| 22 | + "https://api.openai.com/v1/completions", |
| 23 | + }, |
| 24 | + } |
| 25 | + |
| 26 | + for _, c := range cases { |
| 27 | + t.Run(c.Name, func(t *testing.T) { |
| 28 | + az := DefaultConfig("dummy") |
| 29 | + cli := NewClientWithConfig(az) |
| 30 | + actual := cli.fullURL(c.Suffix) |
| 31 | + if actual != c.Expect { |
| 32 | + t.Errorf("Expected %s, got %s", c.Expect, actual) |
| 33 | + } |
| 34 | + t.Logf("Full URL: %s", actual) |
| 35 | + }) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestRequestAuthHeader(t *testing.T) { |
| 40 | + cases := []struct { |
| 41 | + Name string |
| 42 | + APIType APIType |
| 43 | + HeaderKey string |
| 44 | + Token string |
| 45 | + Expect string |
| 46 | + }{ |
| 47 | + { |
| 48 | + "OpenAIDefault", |
| 49 | + "", |
| 50 | + "Authorization", |
| 51 | + "dummy-token-openai", |
| 52 | + "Bearer dummy-token-openai", |
| 53 | + }, |
| 54 | + { |
| 55 | + "OpenAI", |
| 56 | + APITypeOpenAI, |
| 57 | + "Authorization", |
| 58 | + "dummy-token-openai", |
| 59 | + "Bearer dummy-token-openai", |
| 60 | + }, |
| 61 | + { |
| 62 | + "AzureAD", |
| 63 | + APITypeAzureAD, |
| 64 | + "Authorization", |
| 65 | + "dummy-token-azure", |
| 66 | + "Bearer dummy-token-azure", |
| 67 | + }, |
| 68 | + { |
| 69 | + "Azure", |
| 70 | + APITypeAzure, |
| 71 | + AzureAPIKeyHeader, |
| 72 | + "dummy-api-key-here", |
| 73 | + "dummy-api-key-here", |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + for _, c := range cases { |
| 78 | + t.Run(c.Name, func(t *testing.T) { |
| 79 | + az := DefaultConfig(c.Token) |
| 80 | + az.APIType = c.APIType |
| 81 | + |
| 82 | + cli := NewClientWithConfig(az) |
| 83 | + req, err := cli.newStreamRequest(context.Background(), "POST", "/chat/completions", nil) |
| 84 | + if err != nil { |
| 85 | + t.Errorf("Failed to create request: %v", err) |
| 86 | + } |
| 87 | + actual := req.Header.Get(c.HeaderKey) |
| 88 | + if actual != c.Expect { |
| 89 | + t.Errorf("Expected %s, got %s", c.Expect, actual) |
| 90 | + } |
| 91 | + t.Logf("%s: %s", c.HeaderKey, actual) |
| 92 | + }) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestAzureFullURL(t *testing.T) { |
| 97 | + cases := []struct { |
| 98 | + Name string |
| 99 | + BaseURL string |
| 100 | + Engine string |
| 101 | + Expect string |
| 102 | + }{ |
| 103 | + { |
| 104 | + "AzureBaseURLWithSlashAutoStrip", |
| 105 | + "https://httpbin.org/", |
| 106 | + "chatgpt-demo", |
| 107 | + "https://httpbin.org/" + |
| 108 | + "openai/deployments/chatgpt-demo" + |
| 109 | + "/chat/completions?api-version=2023-03-15-preview", |
| 110 | + }, |
| 111 | + { |
| 112 | + "AzureBaseURLWithoutSlashOK", |
| 113 | + "https://httpbin.org", |
| 114 | + "chatgpt-demo", |
| 115 | + "https://httpbin.org/" + |
| 116 | + "openai/deployments/chatgpt-demo" + |
| 117 | + "/chat/completions?api-version=2023-03-15-preview", |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + for _, c := range cases { |
| 122 | + t.Run(c.Name, func(t *testing.T) { |
| 123 | + az := DefaultAzureConfig("dummy", c.BaseURL, c.Engine) |
| 124 | + cli := NewClientWithConfig(az) |
| 125 | + // /openai/deployments/{engine}/chat/completions?api-version={api_version} |
| 126 | + actual := cli.fullURL("/chat/completions") |
| 127 | + if actual != c.Expect { |
| 128 | + t.Errorf("Expected %s, got %s", c.Expect, actual) |
| 129 | + } |
| 130 | + t.Logf("Full URL: %s", actual) |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
0 commit comments