1313// limitations under the License.
1414
1515import FirebaseAppCheckInterop
16+ import FirebaseAuthInterop
1617import FirebaseCore
1718import XCTest
1819
@@ -41,6 +42,7 @@ final class GenerativeModelTests: XCTestCase {
4142 tools: nil ,
4243 requestOptions: RequestOptions ( ) ,
4344 appCheck: nil ,
45+ auth: nil ,
4446 urlSession: urlSession
4547 )
4648 }
@@ -182,6 +184,7 @@ final class GenerativeModelTests: XCTestCase {
182184 tools: nil ,
183185 requestOptions: RequestOptions ( ) ,
184186 appCheck: nil ,
187+ auth: nil ,
185188 urlSession: urlSession
186189 )
187190
@@ -266,6 +269,7 @@ final class GenerativeModelTests: XCTestCase {
266269 tools: nil ,
267270 requestOptions: RequestOptions ( ) ,
268271 appCheck: AppCheckInteropFake ( token: appCheckToken) ,
272+ auth: nil ,
269273 urlSession: urlSession
270274 )
271275 MockURLProtocol
@@ -285,6 +289,7 @@ final class GenerativeModelTests: XCTestCase {
285289 tools: nil ,
286290 requestOptions: RequestOptions ( ) ,
287291 appCheck: AppCheckInteropFake ( error: AppCheckErrorFake ( ) ) ,
292+ auth: nil ,
288293 urlSession: urlSession
289294 )
290295 MockURLProtocol
@@ -297,6 +302,74 @@ final class GenerativeModelTests: XCTestCase {
297302 _ = try await model. generateContent ( testPrompt)
298303 }
299304
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+
300373 func testGenerateContent_usageMetadata( ) async throws {
301374 MockURLProtocol
302375 . requestHandler = try httpRequestHandler (
@@ -598,6 +671,7 @@ final class GenerativeModelTests: XCTestCase {
598671 tools: nil ,
599672 requestOptions: requestOptions,
600673 appCheck: nil ,
674+ auth: nil ,
601675 urlSession: urlSession
602676 )
603677
@@ -808,6 +882,7 @@ final class GenerativeModelTests: XCTestCase {
808882 tools: nil ,
809883 requestOptions: RequestOptions ( ) ,
810884 appCheck: AppCheckInteropFake ( token: appCheckToken) ,
885+ auth: nil ,
811886 urlSession: urlSession
812887 )
813888 MockURLProtocol
@@ -828,6 +903,7 @@ final class GenerativeModelTests: XCTestCase {
828903 tools: nil ,
829904 requestOptions: RequestOptions ( ) ,
830905 appCheck: AppCheckInteropFake ( error: AppCheckErrorFake ( ) ) ,
906+ auth: nil ,
831907 urlSession: urlSession
832908 )
833909 MockURLProtocol
@@ -972,6 +1048,7 @@ final class GenerativeModelTests: XCTestCase {
9721048 tools: nil ,
9731049 requestOptions: requestOptions,
9741050 appCheck: nil ,
1051+ auth: nil ,
9751052 urlSession: urlSession
9761053 )
9771054
@@ -1048,6 +1125,7 @@ final class GenerativeModelTests: XCTestCase {
10481125 tools: nil ,
10491126 requestOptions: requestOptions,
10501127 appCheck: nil ,
1128+ auth: nil ,
10511129 urlSession: urlSession
10521130 )
10531131
@@ -1067,7 +1145,8 @@ final class GenerativeModelTests: XCTestCase {
10671145 apiKey: " API_KEY " ,
10681146 tools: nil ,
10691147 requestOptions: RequestOptions ( ) ,
1070- appCheck: nil
1148+ appCheck: nil ,
1149+ auth: nil
10711150 )
10721151
10731152 XCTAssertEqual ( model. modelResourceName, modelResourceName)
@@ -1081,7 +1160,8 @@ final class GenerativeModelTests: XCTestCase {
10811160 apiKey: " API_KEY " ,
10821161 tools: nil ,
10831162 requestOptions: RequestOptions ( ) ,
1084- appCheck: nil
1163+ appCheck: nil ,
1164+ auth: nil
10851165 )
10861166
10871167 XCTAssertEqual ( model. modelResourceName, modelResourceName)
@@ -1095,7 +1175,8 @@ final class GenerativeModelTests: XCTestCase {
10951175 apiKey: " API_KEY " ,
10961176 tools: nil ,
10971177 requestOptions: RequestOptions ( ) ,
1098- appCheck: nil
1178+ appCheck: nil ,
1179+ auth: nil
10991180 )
11001181
11011182 XCTAssertEqual ( model. modelResourceName, tunedModelResourceName)
@@ -1123,7 +1204,8 @@ final class GenerativeModelTests: XCTestCase {
11231204 withExtension ext: String ,
11241205 statusCode: Int = 200 ,
11251206 timeout: TimeInterval = URLRequest . defaultTimeoutInterval ( ) ,
1126- appCheckToken: String ? = nil ) throws -> ( ( URLRequest ) throws -> (
1207+ appCheckToken: String ? = nil ,
1208+ authToken: String ? = nil ) throws -> ( ( URLRequest ) throws -> (
11271209 URLResponse ,
11281210 AsyncLineSequence < URL . AsyncBytes > ?
11291211 ) ) {
@@ -1137,6 +1219,11 @@ final class GenerativeModelTests: XCTestCase {
11371219 XCTAssert ( apiClientTags. contains ( GenerativeAIService . languageTag) )
11381220 XCTAssert ( apiClientTags. contains ( GenerativeAIService . firebaseVersionTag) )
11391221 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+ }
11401227 let response = try XCTUnwrap ( HTTPURLResponse (
11411228 url: requestURL,
11421229 statusCode: statusCode,
0 commit comments