11//-----------------------------------------------------------------------------
2- // <copyright file="ApiException .cs" company="Dropbox Inc">
2+ // <copyright file="DropboxApiTests .cs" company="Dropbox Inc">
33// Copyright (c) Dropbox Inc. All rights reserved.
44// </copyright>
55//-----------------------------------------------------------------------------
@@ -9,6 +9,8 @@ namespace Dropbox.Api.Tests
99 using System ;
1010 using System . Collections . Generic ;
1111 using System . IO ;
12+ using System . Net ;
13+ using System . Net . Http ;
1214 using System . Text ;
1315 using System . Threading . Tasks ;
1416
@@ -118,6 +120,60 @@ public async Task TestDownload()
118120 Assert . AreEqual ( response . PathDisplay , "/Foo.txt" ) ;
119121 }
120122
123+ /// Test rate limit error handling.
124+ /// </summary>
125+ /// <returns>The <see cref="Task"/></returns>
126+ [ TestMethod ]
127+ public async Task TestRateLimit ( )
128+ {
129+ var body = "{\" error_summary\" : \" too_many_requests/..\" , \" error\" : {\" reason\" : {\" .tag\" : \" too_many_requests\" }, \" retry_after\" : 100}}" ;
130+ var mockResponse = new HttpResponseMessage ( ( HttpStatusCode ) 429 )
131+ {
132+ Content = new StringContent ( body , Encoding . UTF8 , "application/json" )
133+ } ;
134+
135+ mockResponse . Headers . Add ( "X-Dropbox-Request-Id" , "123" ) ;
136+
137+ var mockHandler = new MockHttpMessageHandler ( mockResponse ) ;
138+ var mockClient = new HttpClient ( mockHandler ) ;
139+ var client = new DropboxClient ( "dummy" , new DropboxClientConfig { HttpClient = mockClient } ) ;
140+ try
141+ {
142+ await client . Files . GetMetadataAsync ( "/a.txt" ) ;
143+ }
144+ catch ( RateLimitException ex )
145+ {
146+ Assert . AreEqual ( ( int ) ex . ErrorResponse . RetryAfter , 100 ) ;
147+ Assert . AreEqual ( ex . RetryAfter , 100 ) ;
148+ Assert . IsTrue ( ex . ErrorResponse . Reason . IsTooManyRequests ) ;
149+ }
150+ }
151+
152+ /// Test request id handling.
153+ /// </summary>
154+ /// <returns>The <see cref="Task"/></returns>
155+ [ TestMethod ]
156+ public async Task TestRequestId ( )
157+ {
158+ var funcs = new List < Func < Task > >
159+ {
160+ ( ) => Client . Files . GetMetadataAsync ( "/noob" ) , // 409
161+ ( ) => Client . Files . GetMetadataAsync ( "/" ) , // 400
162+ } ;
163+
164+ foreach ( var func in funcs )
165+ {
166+ try
167+ {
168+ await func ( ) ;
169+ }
170+ catch ( DropboxException ex )
171+ {
172+ Assert . IsTrue ( ex . ToString ( ) . Contains ( "Request Id" ) ) ;
173+ }
174+ }
175+ }
176+
121177 /// <summary>
122178 /// Converts string to a memory stream.
123179 /// </summary>
0 commit comments