@@ -981,6 +981,54 @@ func TestOpenAIGatewayServiceForwardImages_APIKeyGenerationUsesConfiguredV1BaseU
981981 require .Equal (t , "aGVsbG8=" , gjson .Get (rec .Body .String (), "data.0.b64_json" ).String ())
982982}
983983
984+ func TestOpenAIGatewayServiceForwardImages_APIKeyNonStreamDetachesUpstreamContext (t * testing.T ) {
985+ gin .SetMode (gin .TestMode )
986+ body := []byte (`{"model":"gpt-image-2","prompt":"draw a cat","response_format":"b64_json"}` )
987+
988+ parentCtx , cancel := context .WithCancel (context .Background ())
989+ cancel ()
990+
991+ req := httptest .NewRequest (http .MethodPost , "/v1/images/generations" , bytes .NewReader (body )).WithContext (parentCtx )
992+ req .Header .Set ("Content-Type" , "application/json" )
993+ rec := httptest .NewRecorder ()
994+ c , _ := gin .CreateTestContext (rec )
995+ c .Request = req
996+
997+ upstream := & httpUpstreamRecorder {
998+ resp : & http.Response {
999+ StatusCode : http .StatusOK ,
1000+ Header : http.Header {
1001+ "Content-Type" : []string {"application/json" },
1002+ "X-Request-Id" : []string {"req_img_apikey_detached" },
1003+ },
1004+ Body : io .NopCloser (strings .NewReader (`{"created":1710000007,"data":[{"b64_json":"aGVsbG8="}]}` )),
1005+ },
1006+ }
1007+ svc := & OpenAIGatewayService {
1008+ cfg : & config.Config {},
1009+ httpUpstream : upstream ,
1010+ }
1011+ parsed , err := svc .ParseOpenAIImagesRequest (c , body )
1012+ require .NoError (t , err )
1013+
1014+ account := & Account {
1015+ ID : 6 ,
1016+ Name : "openai-apikey" ,
1017+ Platform : PlatformOpenAI ,
1018+ Type : AccountTypeAPIKey ,
1019+ Credentials : map [string ]any {
1020+ "api_key" : "test-api-key" ,
1021+ },
1022+ }
1023+
1024+ result , err := svc .ForwardImages (c .Request .Context (), c , account , body , parsed , "" )
1025+ require .NoError (t , err )
1026+ require .NotNil (t , result )
1027+ require .NotNil (t , upstream .lastReq )
1028+ require .NoError (t , upstream .lastReq .Context ().Err ())
1029+ require .Equal (t , 1 , result .ImageCount )
1030+ }
1031+
9841032func TestOpenAIGatewayServiceForwardImages_APIKeyStreamJSONResponseBillsImage (t * testing.T ) {
9851033 gin .SetMode (gin .TestMode )
9861034 body := []byte (`{"model":"gpt-image-2","prompt":"draw a cat","stream":true,"response_format":"b64_json"}` )
0 commit comments