@@ -5,6 +5,7 @@ namespace Microsoft.Graph.Authentication.Test.Helpers
55{
66 using System ;
77 using System . Net . Http ;
8+ using Microsoft . Graph . Authentication . Core ;
89 using Microsoft . Graph . PowerShell . Authentication ;
910 using Microsoft . Graph . PowerShell . Authentication . Helpers ;
1011 using Xunit ;
@@ -71,5 +72,112 @@ public void GetGraphHttpClientShouldReturnHttpClientWithCustomerProvidedTimeout(
7172 // reset static instance.
7273 GraphSession . Reset ( ) ;
7374 }
75+
76+ [ Fact ]
77+ public void GetGraphHttpClientShouldReturnInSessionHttpClientWhenSessionHasAClient ( )
78+ {
79+ GraphSession . Initialize ( ( ) => new GraphSession ( ) ) ;
80+ GraphSession . Instance . AuthContext = new AuthContext
81+ {
82+ AuthType = AuthenticationType . UserProvidedAccessToken ,
83+ ContextScope = ContextScope . Process ,
84+ } ;
85+ GraphSession . Instance . GraphHttpClient = new HttpClient
86+ {
87+ BaseAddress = new Uri ( "https://test.contoso.com/v1.0/" )
88+ } ;
89+
90+ HttpClient httpClient = HttpHelpers . GetGraphHttpClient ( ) ;
91+
92+ Assert . NotNull ( httpClient ) ;
93+ Assert . NotNull ( GraphSession . Instance . GraphHttpClient ) ;
94+ Assert . Equal ( httpClient . BaseAddress , GraphSession . Instance . GraphHttpClient . BaseAddress ) ;
95+
96+ // reset static instance.
97+ GraphSession . Reset ( ) ;
98+ }
99+
100+ [ Fact ]
101+ public void GetGraphHttpClientShouldReturnNewHttpClientWhenSessionHasNoClient ( )
102+ {
103+ GraphSession . Initialize ( ( ) => new GraphSession ( ) ) ;
104+ GraphSession . Instance . AuthContext = new AuthContext
105+ {
106+ AuthType = AuthenticationType . UserProvidedAccessToken ,
107+ ContextScope = ContextScope . Process ,
108+ } ;
109+ GraphSession . Instance . GraphHttpClient = null ;
110+
111+ HttpClient httpClient = HttpHelpers . GetGraphHttpClient ( ) ;
112+
113+ Assert . NotNull ( httpClient ) ;
114+ Assert . NotNull ( GraphSession . Instance . GraphHttpClient ) ;
115+ Assert . Equal ( httpClient . BaseAddress , GraphSession . Instance . GraphHttpClient . BaseAddress ) ;
116+
117+ // reset static instance.
118+ GraphSession . Reset ( ) ;
119+ }
120+
121+ [ Fact ]
122+ public void GetGraphHttpClientShouldReturnNewHttpClientWhenSessionIsNew ( )
123+ {
124+ GraphSession . Initialize ( ( ) => new GraphSession ( ) ) ;
125+ GraphSession . Instance . AuthContext = new AuthContext
126+ {
127+ AuthType = AuthenticationType . UserProvidedAccessToken ,
128+ ContextScope = ContextScope . Process ,
129+ } ;
130+
131+ HttpClient httpClient = HttpHelpers . GetGraphHttpClient ( ) ;
132+
133+ Assert . NotNull ( httpClient ) ;
134+ Assert . NotNull ( GraphSession . Instance . GraphHttpClient ) ;
135+ Assert . Equal ( httpClient . BaseAddress , GraphSession . Instance . GraphHttpClient . BaseAddress ) ;
136+
137+ // reset static instance.
138+ GraphSession . Reset ( ) ;
139+ }
140+
141+ [ Fact ]
142+ public void GetGraphHttpClientShouldReturnNewHttpClientSignOutThenSignIn ( )
143+ {
144+ GraphSession . Initialize ( ( ) => new GraphSession ( ) ) ;
145+ GraphSession . Instance . AuthContext = new AuthContext
146+ {
147+ AuthType = AuthenticationType . UserProvidedAccessToken ,
148+ ContextScope = ContextScope . Process ,
149+ } ;
150+
151+ var dummyClient = new HttpClient
152+ {
153+ BaseAddress = new Uri ( "https://test.contoso.com/v1.0/" )
154+ } ;
155+ GraphSession . Instance . GraphHttpClient = dummyClient ;
156+ HttpClient httpClientAttempt1 = HttpHelpers . GetGraphHttpClient ( ) ;
157+
158+ // Mock sign out.
159+ Authenticator . LogOut ( GraphSession . Instance . AuthContext ) ;
160+ GraphSession . Instance . AuthContext = null ;
161+ GraphSession . Instance . GraphHttpClient = null ;
162+
163+ // Mock sign in.
164+ GraphSession . Initialize ( ( ) => new GraphSession ( ) ) ;
165+ GraphSession . Instance . AuthContext = new AuthContext
166+ {
167+ AuthType = AuthenticationType . UserProvidedAccessToken ,
168+ ContextScope = ContextScope . Process ,
169+ } ;
170+ HttpClient httpClientAttempt2 = HttpHelpers . GetGraphHttpClient ( ) ;
171+
172+ Assert . NotNull ( httpClientAttempt1 ) ;
173+ Assert . NotNull ( httpClientAttempt2 ) ;
174+ Assert . NotNull ( GraphSession . Instance . GraphHttpClient ) ;
175+ Assert . NotEqual ( httpClientAttempt2 . BaseAddress , httpClientAttempt1 . BaseAddress ) ;
176+ Assert . Equal ( httpClientAttempt1 . BaseAddress , dummyClient . BaseAddress ) ;
177+ Assert . Equal ( httpClientAttempt2 . BaseAddress , GraphSession . Instance . GraphHttpClient . BaseAddress ) ;
178+
179+ // reset static instance.
180+ GraphSession . Reset ( ) ;
181+ }
74182 }
75183}
0 commit comments