@@ -39,6 +39,7 @@ public async Task BaiduIpLocatorProvider_Ok()
3939 option . Value . IpLocatorOptions . ProviderName = nameof ( BaiduIpLocatorProvider ) ;
4040 await provider . Locate ( "223.91.188.112" ) ;
4141 Assert . Equal ( "美国" , result ) ;
42+ Assert . Null ( provider . LastError ) ;
4243 }
4344
4445 [ Fact ]
@@ -85,6 +86,19 @@ public async Task Fetch_Result_Fail()
8586 Assert . Null ( result ) ;
8687 }
8788
89+ [ Fact ]
90+ public async Task Fetch_Exception ( )
91+ {
92+ var factory = Context . Services . GetRequiredService < IHttpClientFactory > ( ) ;
93+ var option = Context . Services . GetRequiredService < IOptions < BootstrapBlazorOptions > > ( ) ;
94+ option . Value . IpLocatorOptions . EnableCache = false ;
95+ var logger = Context . Services . GetRequiredService < ILogger < MockErrorProvider > > ( ) ;
96+ var provider = new MockErrorProvider ( factory , option , logger ) ;
97+ var result = await provider . Locate ( "223.91.188.112" ) ;
98+ Assert . Null ( result ) ;
99+ Assert . NotNull ( provider . LastError ) ;
100+ }
101+
88102 class MockProviderFetchError ( IHttpClientFactory httpClientFactory , IOptions < BootstrapBlazorOptions > option , ILogger < MockProviderFetchError > logger ) : BaiduIpLocatorProvider ( httpClientFactory , option , logger )
89103 {
90104 protected override Task < string ? > Fetch ( string url , HttpClient client , CancellationToken token ) => throw new InvalidOperationException ( ) ;
@@ -113,57 +127,41 @@ class MockProvider(IHttpClientFactory httpClientFactory, IOptions<BootstrapBlazo
113127 }
114128 }
115129
116- class MockHttpNullMessageHandler : HttpMessageHandler
130+ class MockErrorProvider ( IHttpClientFactory httpClientFactory , IOptions < BootstrapBlazorOptions > option , ILogger < MockErrorProvider > logger ) : BaiduIpLocatorProvider ( httpClientFactory , option , logger )
117131 {
118- protected override Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
119- {
120- return Task . FromResult ( new HttpResponseMessage ( HttpStatusCode . OK )
121- {
122- Content = new StringContent ( "null" )
123- } ) ;
124- }
125- }
126-
127- class MockHttpSuccessMessageHandler : HttpMessageHandler
128- {
129- protected override Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
132+ protected override Task < string ? > Fetch ( string url , HttpClient client , CancellationToken token )
130133 {
131- return Task . FromResult ( new HttpResponseMessage ( HttpStatusCode . OK )
132- {
133- Content = new StringContent ( "{\" status\" :\" 0\" ,\" t\" :\" \" ,\" set_cache_time\" :\" \" ,\" data\" :[{\" ExtendedLocation\" :\" \" ,\" OriginQuery\" :\" 20.205.243.166\" ,\" appinfo\" :\" \" ,\" disp_type\" :0,\" fetchkey\" :\" 20.205.243.166\" ,\" location\" :\" 美国\" ,\" origip\" :\" 20.205.243.166\" ,\" origipquery\" :\" 20.205.243.166\" ,\" resourceid\" :\" 6006\" ,\" role_id\" :0,\" shareImage\" :1,\" showLikeShare\" :1,\" showlamp\" :\" 1\" ,\" titlecont\" :\" IP地址查询\" ,\" tplt\" :\" ip\" }]}" )
134- } ) ;
134+ client = new HttpClient ( new MockHttpExceptionMessageHandler ( ) , true ) ;
135+ return base . Fetch ( url , client , token ) ;
135136 }
136137 }
137138
138- class MockHttpSuccessMessageHandlerV2 : HttpMessageHandler
139+ class MockHttpExceptionMessageHandler : HttpMessageHandler
139140 {
140141 protected override Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
141142 {
142- return Task . FromResult ( new HttpResponseMessage ( HttpStatusCode . OK )
143- {
144- Content = new StringContent ( "{\" code\" :\" Success\" ,\" data\" : {\" country\" : \" 中国\" , \" prov\" :\" 省份\" , \" city\" :\" 城市\" , \" district\" :\" 区县\" , \" isp\" : \" 测试\" }}" )
145- } ) ;
143+ throw new Exception ( "error test" ) ;
146144 }
147145 }
148146
149- class MockHttpSuccessMessageHandlerJuHe : HttpMessageHandler
147+ class MockHttpNullMessageHandler : HttpMessageHandler
150148 {
151149 protected override Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
152150 {
153151 return Task . FromResult ( new HttpResponseMessage ( HttpStatusCode . OK )
154152 {
155- Content = new StringContent ( "{ \" reason \" : \" 查询成功 \" , \" error_code \" : 0, \" result \" : { \" country \" : \" 中国 \" , \" prov \" : \" 省份 \" , \" city \" : \" 城市 \" , \" district \" : \" 区县 \" , \" isp \" : \" 测试 \" }} " )
153+ Content = new StringContent ( "null " )
156154 } ) ;
157155 }
158156 }
159157
160- class MockHttpFailedMessageHandlerJuHe : HttpMessageHandler
158+ class MockHttpSuccessMessageHandler : HttpMessageHandler
161159 {
162160 protected override Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
163161 {
164162 return Task . FromResult ( new HttpResponseMessage ( HttpStatusCode . OK )
165163 {
166- Content = new StringContent ( "{\" reason \" : \" 错误的请求KEY \" , \" error_code \" : 10001, \" result \" : null }" )
164+ Content = new StringContent ( "{\" status \" :\" 0 \" ,\" t \" :\" \" , \" set_cache_time \" :\" \" , \" data \" :[{ \" ExtendedLocation \" : \" \" , \" OriginQuery \" : \" 20.205.243.166 \" , \" appinfo \" : \" \" , \" disp_type \" :0, \" fetchkey \" : \" 20.205.243.166 \" , \" location \" : \" 美国 \" , \" origip \" : \" 20.205.243.166 \" , \" origipquery \" : \" 20.205.243.166 \" , \" resourceid \" : \" 6006 \" , \" role_id \" :0, \" shareImage \" :1, \" showLikeShare \" :1, \" showlamp \" : \" 1 \" , \" titlecont \" : \" IP地址查询 \" , \" tplt \" : \" ip \" }] }" )
167165 } ) ;
168166 }
169167 }
0 commit comments