@@ -800,6 +800,98 @@ public void testResolveHostMetadataHostTypeUnified() throws IOException {
800800 }
801801 }
802802
803+ // --- resolveHostMetadata default_oidc_audience tests ---
804+
805+ @ Test
806+ public void testResolveHostMetadataSetsTokenAudienceFromDefaultOidcAudience () throws IOException {
807+ String response =
808+ "{\" oidc_endpoint\" :\" https://ws.databricks.com/oidc\" ,"
809+ + "\" account_id\" :\" "
810+ + DUMMY_ACCOUNT_ID
811+ + "\" ,"
812+ + "\" default_oidc_audience\" :\" https://ws.databricks.com/oidc/v1/token\" }" ;
813+ try (FixtureServer server =
814+ new FixtureServer ().with ("GET" , "/.well-known/databricks-config" , response , 200 )) {
815+ DatabricksConfig config = new DatabricksConfig ().setHost (server .getUrl ());
816+ config .resolve (emptyEnv ());
817+ config .resolveHostMetadata ();
818+ assertEquals ("https://ws.databricks.com/oidc/v1/token" , config .getTokenAudience ());
819+ }
820+ }
821+
822+ @ Test
823+ public void testResolveHostMetadataDefaultOidcAudiencePriorityOverAccountIdFallback ()
824+ throws IOException {
825+ // default_oidc_audience should take priority over the account_id fallback for account hosts
826+ String response =
827+ "{\" oidc_endpoint\" :\" https://acc.databricks.com/oidc/accounts/{account_id}\" ,"
828+ + "\" account_id\" :\" "
829+ + DUMMY_ACCOUNT_ID
830+ + "\" ,"
831+ + "\" default_oidc_audience\" :\" custom-audience\" }" ;
832+ try (FixtureServer server =
833+ new FixtureServer ().with ("GET" , "/.well-known/databricks-config" , response , 200 )) {
834+ DatabricksConfig config =
835+ new DatabricksConfig ()
836+ .setHost (server .getUrl ())
837+ .setExperimentalIsUnifiedHost (true )
838+ .setAccountId (DUMMY_ACCOUNT_ID );
839+ // Note: need two fixtures — resolve() consumes first one (unified host triggers
840+ // tryResolveHostMetadata)
841+ // Instead, don't set unified — just test direct call
842+ config = new DatabricksConfig ().setHost (server .getUrl ()).setAccountId (DUMMY_ACCOUNT_ID );
843+ config .resolve (emptyEnv ());
844+ config .resolveHostMetadata ();
845+ // Should use default_oidc_audience, NOT account_id
846+ assertEquals ("custom-audience" , config .getTokenAudience ());
847+ }
848+ }
849+
850+ @ Test
851+ public void testResolveHostMetadataDoesNotOverrideExistingTokenAudienceWithOidcAudience ()
852+ throws IOException {
853+ String response =
854+ "{\" oidc_endpoint\" :\" https://ws.databricks.com/oidc\" ,"
855+ + "\" account_id\" :\" "
856+ + DUMMY_ACCOUNT_ID
857+ + "\" ,"
858+ + "\" default_oidc_audience\" :\" metadata-audience\" }" ;
859+ try (FixtureServer server =
860+ new FixtureServer ().with ("GET" , "/.well-known/databricks-config" , response , 200 )) {
861+ DatabricksConfig config =
862+ new DatabricksConfig ().setHost (server .getUrl ()).setTokenAudience ("existing-audience" );
863+ config .resolve (emptyEnv ());
864+ config .resolveHostMetadata ();
865+ assertEquals ("existing-audience" , config .getTokenAudience ());
866+ }
867+ }
868+
869+ @ Test
870+ public void testResolveHostMetadataFallsBackToAccountIdWhenNoDefaultOidcAudience ()
871+ throws IOException {
872+ // When no default_oidc_audience, should fall back to account_id for account hosts.
873+ // Use unified host flag so getClientType() returns ACCOUNT (no workspaceId).
874+ String response =
875+ "{\" oidc_endpoint\" :\" https://acc.databricks.com/oidc/accounts/{account_id}\" ,"
876+ + "\" account_id\" :\" "
877+ + DUMMY_ACCOUNT_ID
878+ + "\" }" ;
879+ try (FixtureServer server =
880+ new FixtureServer ()
881+ .with ("GET" , "/.well-known/databricks-config" , response , 200 )
882+ .with ("GET" , "/.well-known/databricks-config" , response , 200 )) {
883+ DatabricksConfig config =
884+ new DatabricksConfig ()
885+ .setHost (server .getUrl ())
886+ .setAccountId (DUMMY_ACCOUNT_ID )
887+ .setExperimentalIsUnifiedHost (true );
888+ config .resolve (emptyEnv ());
889+ // resolve() with unified flag triggers tryResolveHostMetadata() consuming first fixture.
890+ // Now call again with second fixture to verify account_id fallback.
891+ assertEquals (DUMMY_ACCOUNT_ID , config .getTokenAudience ());
892+ }
893+ }
894+
803895 // --- discoveryUrl / OIDC endpoint tests ---
804896
805897 @ Test
0 commit comments