Skip to content

Commit b35f15d

Browse files
committed
Update RateLimitException and tests.
1 parent 54d9036 commit b35f15d

File tree

15 files changed

+278
-135
lines changed

15 files changed

+278
-135
lines changed

Dropbox.Api.Tests/Dropbox.Api.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<Reference Include="System.Core">
4343
<RequiredTargetFramework>3.5</RequiredTargetFramework>
4444
</Reference>
45+
<Reference Include="System.Net.Http" />
4546
</ItemGroup>
4647
<Choose>
4748
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
@@ -57,6 +58,7 @@
5758
</Choose>
5859
<ItemGroup>
5960
<Compile Include="DropboxApiTests.cs" />
61+
<Compile Include="MockHttpMessageHandler.cs" />
6062
<Compile Include="Properties\AssemblyInfo.cs" />
6163
</ItemGroup>
6264
<ItemGroup>

Dropbox.Api.Tests/DropboxApiTests.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//-----------------------------------------------------------------------------
2+
// <copyright file="MockHttpMessageHandler.cs" company="Dropbox Inc">
3+
// Copyright (c) Dropbox Inc. All rights reserved.
4+
// </copyright>
5+
//-----------------------------------------------------------------------------
6+
7+
namespace Dropbox.Api.Tests
8+
{
9+
using System.Net.Http;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
13+
public class MockHttpMessageHandler : HttpMessageHandler
14+
{
15+
/// <summary>
16+
/// The fake response.
17+
/// </summary>
18+
private readonly HttpResponseMessage response;
19+
20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="MockHttpMessageHandler"/> class.
22+
/// </summary>
23+
/// <param name="response">The mock response.</param>
24+
public MockHttpMessageHandler(HttpResponseMessage response)
25+
{
26+
this.response = response;
27+
}
28+
29+
/// <summary>
30+
/// The send async override.
31+
/// </summary>
32+
/// <param name="request">The request.</param>
33+
/// <param name="cancellationToken">The cancellation token.</param>
34+
/// <returns>The response.</returns>
35+
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
36+
{
37+
return Task.FromResult(this.response);
38+
}
39+
}
40+
}

Dropbox.Api/ApiException.cs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,10 @@ public sealed class ApiException<TError> : StructuredException<TError>
2121
/// <summary>
2222
/// Initializes a new instance of the <see cref="ApiException{TError}"/> class.
2323
/// </summary>
24+
/// <param name="requestId">The Dropbox request id.</param>
2425
/// <remarks>This constructor is only used when decoded from JSON.</remarks>
25-
public ApiException()
26-
: this(default(TError))
27-
{
28-
}
29-
30-
/// <summary>
31-
/// Initializes a new instance of the <see cref="ApiException{TError}"/> class.
32-
/// </summary>
33-
/// <param name="errorResponse">The error response.</param>
34-
public ApiException(TError errorResponse)
35-
: this(errorResponse, null, null)
36-
{
37-
}
38-
39-
/// <summary>
40-
/// Initializes a new instance of the <see cref="ApiException{TError}"/> class.
41-
/// </summary>
42-
/// <param name="errorResponse">The error response.</param>
43-
/// <param name="message">The message.</param>
44-
public ApiException(TError errorResponse, string message)
45-
: this(errorResponse, message, null)
46-
{
47-
}
48-
49-
/// <summary>
50-
/// Initializes a new instance of the <see cref="ApiException{TError}"/> class.
51-
/// </summary>
52-
/// <param name="errorResponse">The error response.</param>
53-
/// <param name="message">The message.</param>
54-
/// <param name="inner">The inner.</param>
55-
public ApiException(TError errorResponse, string message, Exception inner)
56-
: base(errorResponse, message, inner)
26+
internal ApiException(string requestId)
27+
: base(requestId)
5728
{
5829
}
5930
}

Dropbox.Api/AuthException.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,12 @@ namespace Dropbox.Api
1414
/// </summary>
1515
public sealed partial class AuthException : StructuredException<AuthError>
1616
{
17-
/// <summary>
18-
/// <para>Initializes a new instance of the <see cref="AuthException"/> class.</para>
19-
/// </summary>
20-
public AuthException()
21-
{
22-
}
23-
2417
/// <summary>
2518
/// <para>Decode from given json.</para>
2619
/// </summary>
27-
internal static AuthException Decode(string json)
20+
internal static AuthException Decode(string json, sys.Func<AuthException> exceptionFunc)
2821
{
29-
return StructuredException<AuthError>.Decode<AuthException>(json, AuthError.Decoder);
22+
return StructuredException<AuthError>.Decode<AuthException>(json, AuthError.Decoder, exceptionFunc);
3023
}
3124
}
3225
}

Dropbox.Api/Dropbox.Api.Portable.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
<Compile Include="Properties\PropertyGroupTemplate.cs" />
200200
<Compile Include="Properties\PropertyTemplateError.cs" />
201201
<Compile Include="Properties\PropertyType.cs" />
202+
<Compile Include="RateLimitException.cs" />
202203
<Compile Include="Sharing\AccessLevel.cs" />
203204
<Compile Include="Sharing\AclUpdatePolicy.cs" />
204205
<Compile Include="Sharing\AddFileMemberArgs.cs" />

Dropbox.Api/Dropbox.Api.Portable40.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
<Compile Include="Properties\PropertyGroupTemplate.cs" />
224224
<Compile Include="Properties\PropertyTemplateError.cs" />
225225
<Compile Include="Properties\PropertyType.cs" />
226+
<Compile Include="RateLimitException.cs" />
226227
<Compile Include="Sharing\AccessLevel.cs" />
227228
<Compile Include="Sharing\AclUpdatePolicy.cs" />
228229
<Compile Include="Sharing\AddFileMemberArgs.cs" />

Dropbox.Api/Dropbox.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
<Compile Include="Properties\PropertyGroupTemplate.cs" />
205205
<Compile Include="Properties\PropertyTemplateError.cs" />
206206
<Compile Include="Properties\PropertyType.cs" />
207+
<Compile Include="RateLimitException.cs" />
207208
<Compile Include="Sharing\AccessLevel.cs" />
208209
<Compile Include="Sharing\AclUpdatePolicy.cs" />
209210
<Compile Include="Sharing\AddFileMemberArgs.cs" />

0 commit comments

Comments
 (0)