13
13
// limitations under the License.
14
14
15
15
import FirebaseAppCheckInterop
16
+ import FirebaseAuthInterop
16
17
import FirebaseCore
17
18
import XCTest
18
19
@@ -41,6 +42,7 @@ final class GenerativeModelTests: XCTestCase {
41
42
tools: nil ,
42
43
requestOptions: RequestOptions ( ) ,
43
44
appCheck: nil ,
45
+ auth: nil ,
44
46
urlSession: urlSession
45
47
)
46
48
}
@@ -182,6 +184,7 @@ final class GenerativeModelTests: XCTestCase {
182
184
tools: nil ,
183
185
requestOptions: RequestOptions ( ) ,
184
186
appCheck: nil ,
187
+ auth: nil ,
185
188
urlSession: urlSession
186
189
)
187
190
@@ -266,6 +269,7 @@ final class GenerativeModelTests: XCTestCase {
266
269
tools: nil ,
267
270
requestOptions: RequestOptions ( ) ,
268
271
appCheck: AppCheckInteropFake ( token: appCheckToken) ,
272
+ auth: nil ,
269
273
urlSession: urlSession
270
274
)
271
275
MockURLProtocol
@@ -285,6 +289,7 @@ final class GenerativeModelTests: XCTestCase {
285
289
tools: nil ,
286
290
requestOptions: RequestOptions ( ) ,
287
291
appCheck: AppCheckInteropFake ( error: AppCheckErrorFake ( ) ) ,
292
+ auth: nil ,
288
293
urlSession: urlSession
289
294
)
290
295
MockURLProtocol
@@ -297,6 +302,74 @@ final class GenerativeModelTests: XCTestCase {
297
302
_ = try await model. generateContent ( testPrompt)
298
303
}
299
304
305
+ func testGenerateContent_auth_validAuthToken( ) async throws {
306
+ let authToken = " test-valid-token "
307
+ model = GenerativeModel (
308
+ name: " my-model " ,
309
+ apiKey: " API_KEY " ,
310
+ tools: nil ,
311
+ requestOptions: RequestOptions ( ) ,
312
+ appCheck: nil ,
313
+ auth: AuthInteropFake ( token: authToken) ,
314
+ urlSession: urlSession
315
+ )
316
+ MockURLProtocol
317
+ . requestHandler = try httpRequestHandler (
318
+ forResource: " unary-success-basic-reply-short " ,
319
+ withExtension: " json " ,
320
+ authToken: authToken
321
+ )
322
+
323
+ _ = try await model. generateContent ( testPrompt)
324
+ }
325
+
326
+ func testGenerateContent_auth_nilAuthToken( ) async throws {
327
+ model = GenerativeModel (
328
+ name: " my-model " ,
329
+ apiKey: " API_KEY " ,
330
+ tools: nil ,
331
+ requestOptions: RequestOptions ( ) ,
332
+ appCheck: nil ,
333
+ auth: AuthInteropFake ( token: nil ) ,
334
+ urlSession: urlSession
335
+ )
336
+ MockURLProtocol
337
+ . requestHandler = try httpRequestHandler (
338
+ forResource: " unary-success-basic-reply-short " ,
339
+ withExtension: " json " ,
340
+ authToken: nil
341
+ )
342
+
343
+ _ = try await model. generateContent ( testPrompt)
344
+ }
345
+
346
+ func testGenerateContent_auth_authTokenRefreshError( ) async throws {
347
+ model = GenerativeModel (
348
+ name: " my-model " ,
349
+ apiKey: " API_KEY " ,
350
+ tools: nil ,
351
+ requestOptions: RequestOptions ( ) ,
352
+ appCheck: nil ,
353
+ auth: AuthInteropFake ( error: AuthErrorFake ( ) ) ,
354
+ urlSession: urlSession
355
+ )
356
+ MockURLProtocol
357
+ . requestHandler = try httpRequestHandler (
358
+ forResource: " unary-success-basic-reply-short " ,
359
+ withExtension: " json " ,
360
+ authToken: nil
361
+ )
362
+
363
+ do {
364
+ _ = try await model. generateContent ( testPrompt)
365
+ XCTFail ( " Should throw internalError(AuthErrorFake); no error. " )
366
+ } catch GenerateContentError . internalError( _ as AuthErrorFake ) {
367
+ //
368
+ } catch {
369
+ XCTFail ( " Should throw internalError(AuthErrorFake); error thrown: \( error) " )
370
+ }
371
+ }
372
+
300
373
func testGenerateContent_usageMetadata( ) async throws {
301
374
MockURLProtocol
302
375
. requestHandler = try httpRequestHandler (
@@ -598,6 +671,7 @@ final class GenerativeModelTests: XCTestCase {
598
671
tools: nil ,
599
672
requestOptions: requestOptions,
600
673
appCheck: nil ,
674
+ auth: nil ,
601
675
urlSession: urlSession
602
676
)
603
677
@@ -808,6 +882,7 @@ final class GenerativeModelTests: XCTestCase {
808
882
tools: nil ,
809
883
requestOptions: RequestOptions ( ) ,
810
884
appCheck: AppCheckInteropFake ( token: appCheckToken) ,
885
+ auth: nil ,
811
886
urlSession: urlSession
812
887
)
813
888
MockURLProtocol
@@ -828,6 +903,7 @@ final class GenerativeModelTests: XCTestCase {
828
903
tools: nil ,
829
904
requestOptions: RequestOptions ( ) ,
830
905
appCheck: AppCheckInteropFake ( error: AppCheckErrorFake ( ) ) ,
906
+ auth: nil ,
831
907
urlSession: urlSession
832
908
)
833
909
MockURLProtocol
@@ -972,6 +1048,7 @@ final class GenerativeModelTests: XCTestCase {
972
1048
tools: nil ,
973
1049
requestOptions: requestOptions,
974
1050
appCheck: nil ,
1051
+ auth: nil ,
975
1052
urlSession: urlSession
976
1053
)
977
1054
@@ -1048,6 +1125,7 @@ final class GenerativeModelTests: XCTestCase {
1048
1125
tools: nil ,
1049
1126
requestOptions: requestOptions,
1050
1127
appCheck: nil ,
1128
+ auth: nil ,
1051
1129
urlSession: urlSession
1052
1130
)
1053
1131
@@ -1067,7 +1145,8 @@ final class GenerativeModelTests: XCTestCase {
1067
1145
apiKey: " API_KEY " ,
1068
1146
tools: nil ,
1069
1147
requestOptions: RequestOptions ( ) ,
1070
- appCheck: nil
1148
+ appCheck: nil ,
1149
+ auth: nil
1071
1150
)
1072
1151
1073
1152
XCTAssertEqual ( model. modelResourceName, modelResourceName)
@@ -1081,7 +1160,8 @@ final class GenerativeModelTests: XCTestCase {
1081
1160
apiKey: " API_KEY " ,
1082
1161
tools: nil ,
1083
1162
requestOptions: RequestOptions ( ) ,
1084
- appCheck: nil
1163
+ appCheck: nil ,
1164
+ auth: nil
1085
1165
)
1086
1166
1087
1167
XCTAssertEqual ( model. modelResourceName, modelResourceName)
@@ -1095,7 +1175,8 @@ final class GenerativeModelTests: XCTestCase {
1095
1175
apiKey: " API_KEY " ,
1096
1176
tools: nil ,
1097
1177
requestOptions: RequestOptions ( ) ,
1098
- appCheck: nil
1178
+ appCheck: nil ,
1179
+ auth: nil
1099
1180
)
1100
1181
1101
1182
XCTAssertEqual ( model. modelResourceName, tunedModelResourceName)
@@ -1123,7 +1204,8 @@ final class GenerativeModelTests: XCTestCase {
1123
1204
withExtension ext: String ,
1124
1205
statusCode: Int = 200 ,
1125
1206
timeout: TimeInterval = URLRequest . defaultTimeoutInterval ( ) ,
1126
- appCheckToken: String ? = nil ) throws -> ( ( URLRequest ) throws -> (
1207
+ appCheckToken: String ? = nil ,
1208
+ authToken: String ? = nil ) throws -> ( ( URLRequest ) throws -> (
1127
1209
URLResponse ,
1128
1210
AsyncLineSequence < URL . AsyncBytes > ?
1129
1211
) ) {
@@ -1137,6 +1219,11 @@ final class GenerativeModelTests: XCTestCase {
1137
1219
XCTAssert ( apiClientTags. contains ( GenerativeAIService . languageTag) )
1138
1220
XCTAssert ( apiClientTags. contains ( GenerativeAIService . firebaseVersionTag) )
1139
1221
XCTAssertEqual ( request. value ( forHTTPHeaderField: " X-Firebase-AppCheck " ) , appCheckToken)
1222
+ if let authToken {
1223
+ XCTAssertEqual ( request. value ( forHTTPHeaderField: " Authorization " ) , " Firebase \( authToken) " )
1224
+ } else {
1225
+ XCTAssertNil ( request. value ( forHTTPHeaderField: " Authorization " ) )
1226
+ }
1140
1227
let response = try XCTUnwrap ( HTTPURLResponse (
1141
1228
url: requestURL,
1142
1229
statusCode: statusCode,
0 commit comments