1+ using Xunit ;
2+ using System . Net ;
3+ using Moq ;
4+ using System . Net . Http ;
5+ using System . Threading . Tasks ;
6+ using System . Threading ;
7+
8+ namespace Dtmcli . Tests
9+ {
10+ public class DtmClientTests
11+ {
12+ [ Fact ]
13+ public async void GenGid_Should_Succeed ( )
14+ {
15+ var factory = new Mock < IHttpClientFactory > ( ) ;
16+ var options = Microsoft . Extensions . Options . Options . Create ( new DtmCommon . DtmOptions { DtmUrl = "http://localhost:8080" } ) ;
17+ var mockHttpMessageHandler = new ClientMockHttpMessageHandler ( HttpStatusCode . OK , "{\" dtm_result\" :\" SUCCESS\" ,\" gid\" :\" 123\" }" ) ;
18+ factory . Setup ( x => x . CreateClient ( It . IsAny < string > ( ) ) ) . Returns ( new HttpClient ( mockHttpMessageHandler ) ) ;
19+
20+ var client = new DtmClient ( factory . Object , options ) ;
21+
22+ var res = await client . GenGid ( new CancellationToken ( ) ) ;
23+
24+ Assert . Equal ( "123" , res ) ;
25+ }
26+
27+ [ Fact ]
28+ public async void GenGid_Should_Throw_Failure_Exception ( )
29+ {
30+ var factory = new Mock < IHttpClientFactory > ( ) ;
31+ var options = Microsoft . Extensions . Options . Options . Create ( new DtmCommon . DtmOptions { DtmUrl = "http://localhost:8080" } ) ;
32+ var mockHttpMessageHandler = new ClientMockHttpMessageHandler ( HttpStatusCode . BadGateway , "" ) ;
33+ factory . Setup ( x => x . CreateClient ( It . IsAny < string > ( ) ) ) . Returns ( new HttpClient ( mockHttpMessageHandler ) ) ;
34+
35+ var client = new DtmClient ( factory . Object , options ) ;
36+
37+ await Assert . ThrowsAsync < DtmCommon . DtmException > ( async ( ) => await client . GenGid ( new CancellationToken ( ) ) ) ;
38+ }
39+
40+ [ Fact ]
41+ public async void TransRegisterBranch_Should_Succeed ( )
42+ {
43+ var factory = new Mock < IHttpClientFactory > ( ) ;
44+ var options = Microsoft . Extensions . Options . Options . Create ( new DtmCommon . DtmOptions { DtmUrl = "http://localhost:8080" } ) ;
45+ var mockHttpMessageHandler = new ClientMockHttpMessageHandler ( HttpStatusCode . OK , "" ) ;
46+ factory . Setup ( x => x . CreateClient ( It . IsAny < string > ( ) ) ) . Returns ( new HttpClient ( mockHttpMessageHandler ) ) ;
47+
48+ var client = new DtmClient ( factory . Object , options ) ;
49+
50+ var tb = new DtmCommon . TransBase ( ) { Gid = "123" , TransType = "tcc" } ;
51+
52+ await client . TransRegisterBranch ( tb , null , "OP" , new CancellationToken ( ) ) ;
53+ }
54+
55+ [ Fact ]
56+ public async void TransRegisterBranch_With_Added_Should_Succeed ( )
57+ {
58+ var factory = new Mock < IHttpClientFactory > ( ) ;
59+ var options = Microsoft . Extensions . Options . Options . Create ( new DtmCommon . DtmOptions { DtmUrl = "http://localhost:8080" } ) ;
60+ var mockHttpMessageHandler = new ClientMockHttpMessageHandler ( HttpStatusCode . OK , "" ) ;
61+ factory . Setup ( x => x . CreateClient ( It . IsAny < string > ( ) ) ) . Returns ( new HttpClient ( mockHttpMessageHandler ) ) ;
62+
63+ var client = new DtmClient ( factory . Object , options ) ;
64+
65+ var tb = new DtmCommon . TransBase ( ) { Gid = "123" , TransType = "tcc" } ;
66+ var added = new System . Collections . Generic . Dictionary < string , string > ( ) { { "a" , "b" } } ;
67+
68+ await client . TransRegisterBranch ( tb , added , "OP" , new CancellationToken ( ) ) ;
69+ }
70+
71+ [ Fact ]
72+ public async void TransRequestBranch_Should_Succeed ( )
73+ {
74+ var factory = new Mock < IHttpClientFactory > ( ) ;
75+ var options = Microsoft . Extensions . Options . Options . Create ( new DtmCommon . DtmOptions { DtmUrl = "http://localhost:8080" } ) ;
76+ var mockHttpMessageHandler = new ClientMockHttpMessageHandler ( HttpStatusCode . OK , "" ) ;
77+ factory . Setup ( x => x . CreateClient ( It . IsAny < string > ( ) ) ) . Returns ( new HttpClient ( mockHttpMessageHandler ) ) ;
78+
79+ var client = new DtmClient ( factory . Object , options ) ;
80+
81+ var tb = new DtmCommon . TransBase ( )
82+ {
83+ Gid = "123" ,
84+ TransType = "tcc" ,
85+ } ;
86+
87+ await client . TransRequestBranch ( tb , HttpMethod . Post , new { } , "00" , "try" , "http://www.baidu.com?a=1" , new CancellationToken ( ) ) ;
88+ }
89+
90+ [ Fact ]
91+ public async void TransRequestBranch_With_BranchHeaders_Should_Succeed ( )
92+ {
93+ var factory = new Mock < IHttpClientFactory > ( ) ;
94+ var options = Microsoft . Extensions . Options . Options . Create ( new DtmCommon . DtmOptions { DtmUrl = "http://localhost:8080" } ) ;
95+ var mockHttpMessageHandler = new ClientMockHttpMessageHandler ( HttpStatusCode . OK , "" ) ;
96+ factory . Setup ( x => x . CreateClient ( It . IsAny < string > ( ) ) ) . Returns ( new HttpClient ( mockHttpMessageHandler ) ) ;
97+
98+ var client = new DtmClient ( factory . Object , options ) ;
99+
100+ var tb = new DtmCommon . TransBase ( )
101+ {
102+ Gid = "123" ,
103+ TransType = "tcc" ,
104+ BranchHeaders = new System . Collections . Generic . Dictionary < string , string > { { "a" , "b" } }
105+ } ;
106+
107+ await client . TransRequestBranch ( tb , HttpMethod . Post , new { } , "00" , "try" , "http://www.baidu.com" , new CancellationToken ( ) ) ;
108+ }
109+
110+ #if NET5_0_OR_GREATER
111+ [ Fact ]
112+ public void TransBaseFromQuery_Should_Succeed ( )
113+ {
114+ var factory = new Mock < IHttpClientFactory > ( ) ;
115+ var options = Microsoft . Extensions . Options . Options . Create ( new DtmCommon . DtmOptions { DtmUrl = "http://localhost:8080" } ) ;
116+ var mockHttpMessageHandler = new ClientMockHttpMessageHandler ( HttpStatusCode . OK , "" ) ;
117+ factory . Setup ( x => x . CreateClient ( It . IsAny < string > ( ) ) ) . Returns ( new HttpClient ( mockHttpMessageHandler ) ) ;
118+
119+ var client = new DtmClient ( factory . Object , options ) ;
120+
121+ var dict = new System . Collections . Generic . Dictionary < string , Microsoft . Extensions . Primitives . StringValues > ( )
122+ {
123+ { "branch_id" , "11" } ,
124+ { "gid" , "1111" } ,
125+ { "op" , "try" } ,
126+ { "trans_type" , "tcc" } ,
127+ } ;
128+
129+ var qs = new Microsoft . AspNetCore . Http . QueryCollection ( dict ) ;
130+
131+ var tb = client . TransBaseFromQuery ( qs ) ;
132+
133+ Assert . Equal ( dict [ "op" ] , tb . Op ) ;
134+ Assert . Equal ( dict [ "gid" ] , tb . Gid ) ;
135+ Assert . Equal ( dict [ "trans_type" ] , tb . TransType ) ;
136+ Assert . Equal ( dict [ "branch_id" ] , tb . BranchIDGen . BranchID ) ;
137+ }
138+ #endif
139+ }
140+
141+ internal class ClientMockHttpMessageHandler : DelegatingHandler
142+ {
143+ private readonly HttpStatusCode _code ;
144+ private readonly string _msg ;
145+ public ClientMockHttpMessageHandler ( HttpStatusCode code , string msg )
146+ {
147+ this . _code = code ;
148+ this . _msg = msg ;
149+ }
150+
151+ protected override Task < HttpResponseMessage > SendAsync ( HttpRequestMessage request , CancellationToken cancellationToken )
152+ {
153+ var content = new StringContent ( _msg ) ;
154+ var resp = new HttpResponseMessage ( _code ) ;
155+ resp . Content = content ;
156+
157+ return Task . FromResult ( resp ) ;
158+ }
159+ }
160+ }
0 commit comments