Skip to content

Commit 38dcfaa

Browse files
tests(testopenai): add image-generation cassette and request scaffolding
Signed-off-by: Hrushikesh Patil <[email protected]>
1 parent 5e76c62 commit 38dcfaa

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

tests/internal/testopenai/cassettes.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ const (
135135
// CassetteEmbeddingsBadRequest tests request with multiple validation errors.
136136
CassetteEmbeddingsBadRequest
137137

138+
// Cassettes for the OpenAI /v1/images/generations endpoint.
139+
140+
// CassetteImageGenerationBasic is a basic image generation request with model and prompt.
141+
CassetteImageGenerationBasic
142+
138143
// Cassettes for Azure OpenAI Service.
139144

140145
// CassetteAzureChatBasic is the same as CassetteChatBasic, except using
@@ -189,6 +194,8 @@ var stringValues = map[Cassette]string{
189194
CassetteEmbeddingsMaxTokens: "embeddings-max-tokens",
190195
CassetteEmbeddingsWhitespace: "embeddings-whitespace",
191196
CassetteEmbeddingsBadRequest: "embeddings-bad-request",
197+
198+
CassetteImageGenerationBasic: "image-generation-basic",
192199
}
193200

194201
// String returns the string representation of the cassette name.
@@ -213,6 +220,9 @@ func NewRequest(ctx context.Context, baseURL string, cassette Cassette) (*http.R
213220
} else if r, ok := embeddingsRequests[cassette]; ok {
214221
path := buildPath(cassette, "/embeddings", baseURL, r)
215222
return newRequest(ctx, cassette, path, r)
223+
} else if r, ok := imageRequests[cassette]; ok {
224+
path := buildPath(cassette, "/images/generations", baseURL, r)
225+
return newRequest(ctx, cassette, path, r)
216226
}
217227
return nil, fmt.Errorf("unknown cassette: %s", cassette)
218228
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
version: 2
3+
interactions:
4+
- id: 0
5+
request:
6+
proto: HTTP/1.1
7+
proto_major: 1
8+
proto_minor: 1
9+
content_length: 105
10+
host: api.openai.com
11+
body: "{\n \"model\": \"dall-e-2\",\n \"prompt\": \"A simple black-and-white line drawing of a cat playing with yarn\"\n}"
12+
headers:
13+
Accept-Encoding:
14+
- gzip
15+
Content-Length:
16+
- "105"
17+
Content-Type:
18+
- application/json
19+
User-Agent:
20+
- Go-http-client/1.1
21+
url: https://api.openai.com/v1/images/generations
22+
method: POST
23+
response:
24+
proto: HTTP/2.0
25+
proto_major: 2
26+
proto_minor: 0
27+
content_length: 544
28+
body: "{\n \"created\": 1759925706,\n \"data\": [\n {\n \"url\": \"https://oaidalleapiprodscus.blob.core.windows.net/private/org-lKxIBdltrjcFbzboW0t1hthB/user-EdKNxj0PjYS29CfjUcR77ybc/img-MigVWgAi6l5W3B31L6imFob3.png?st=2025-10-08T11%3A15%3A05Z&se=2025-10-08T13%3A15%3A05Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=52f8f7b3-ca8d-4b21-9807-8b9df114d84c&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2025-10-08T12%3A15%3A05Z&ske=2025-10-09T12%3A15%3A05Z&sks=b&skv=2024-08-04&sig=ex3I6RoFVH9hfNst8oloGtfBVA3o2aXvTI4cFtLVzlE%3D\"\n }\n ]\n}"
29+
headers:
30+
Alt-Svc:
31+
- h3=":443"; ma=86400
32+
Cf-Cache-Status:
33+
- DYNAMIC
34+
Cf-Ray:
35+
- 98b58f7bde648af7-BOM
36+
Content-Length:
37+
- "544"
38+
Content-Type:
39+
- application/json
40+
Date:
41+
- Wed, 08 Oct 2025 12:15:06 GMT
42+
Openai-Processing-Ms:
43+
- "13265"
44+
Openai-Project:
45+
- proj_Ro6stNUOKIiLVqRr1zdjaFoh
46+
Openai-Version:
47+
- "2020-10-01"
48+
Server:
49+
- cloudflare
50+
Strict-Transport-Security:
51+
- max-age=31536000; includeSubDomains; preload
52+
X-Content-Type-Options:
53+
- nosniff
54+
X-Envoy-Upstream-Service-Time:
55+
- "13269"
56+
X-Request-Id:
57+
- req_c643aaa7f52258b2192d4c133d117e43
58+
status: 200 OK
59+
code: 200
60+
duration: 13.632879292s
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright Envoy AI Gateway Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
// The full text of the Apache license is available in the LICENSE file at
4+
// the root of the repo.
5+
6+
package testopenai
7+
8+
// ImageCassettes returns a slice of all cassettes for image generation.
9+
func ImageCassettes() []Cassette {
10+
return cassettes(imageRequests)
11+
}
12+
13+
// imageGenerationRequest is a minimal request body for OpenAI image generation.
14+
// We avoid importing the OpenAI SDK in tests to keep dependencies light.
15+
type imageGenerationRequest struct {
16+
Model string `json:"model"`
17+
Prompt string `json:"prompt"`
18+
// Optional fields like size/quality/response_format can be added later if needed.
19+
}
20+
21+
// imageRequests contains the actual request body for each image generation cassette.
22+
var imageRequests = map[Cassette]*imageGenerationRequest{
23+
CassetteImageGenerationBasic: {
24+
Model: "dall-e-2",
25+
Prompt: "A simple black-and-white line drawing of a cat playing with yarn",
26+
},
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright Envoy AI Gateway Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
// The full text of the Apache license is available in the LICENSE file at
4+
// the root of the repo.
5+
6+
package testopenai
7+
8+
import (
9+
"net/http"
10+
"testing"
11+
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestImageCassettes(t *testing.T) {
16+
testCassettes(t, ImageCassettes(), imageRequests)
17+
}
18+
19+
func TestNewRequestImages(t *testing.T) {
20+
tests, err := buildTestCases(t, imageRequests)
21+
require.NoError(t, err)
22+
for i := range tests {
23+
// Currently only basic happy-path image generation cassette
24+
tests[i].expectedStatus = http.StatusOK
25+
}
26+
testNewRequest(t, tests)
27+
}

0 commit comments

Comments
 (0)