-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMockNetworkErrorGenerator.cs
More file actions
55 lines (48 loc) · 1.78 KB
/
MockNetworkErrorGenerator.cs
File metadata and controls
55 lines (48 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Contentstack.Management.Core.Exceptions;
namespace Contentstack.Management.Core.Unit.Tests.Mokes
{
/// <summary>
/// Utility to generate various network error exceptions for testing.
/// </summary>
public static class MockNetworkErrorGenerator
{
public static SocketException CreateSocketException(SocketError errorCode)
{
return new SocketException((int)errorCode);
}
public static HttpRequestException CreateHttpRequestExceptionWithSocketException(SocketError socketError)
{
var socketException = CreateSocketException(socketError);
return new HttpRequestException("Network error", socketException);
}
public static TaskCanceledException CreateTaskCanceledExceptionTimeout()
{
var cts = new CancellationTokenSource();
return new TaskCanceledException("Operation timed out", null, cts.Token);
}
public static TaskCanceledException CreateTaskCanceledExceptionUserCancellation()
{
var cts = new CancellationTokenSource();
cts.Cancel();
return new TaskCanceledException("User cancelled", null, cts.Token);
}
public static TimeoutException CreateTimeoutException()
{
return new TimeoutException("Operation timed out");
}
public static ContentstackErrorException CreateContentstackErrorException(HttpStatusCode statusCode)
{
return new ContentstackErrorException
{
StatusCode = statusCode,
Message = $"HTTP {statusCode} error"
};
}
}
}