-
Notifications
You must be signed in to change notification settings - Fork 865
/
Copy pathCustomResponses.cs
94 lines (86 loc) · 3.09 KB
/
CustomResponses.cs
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Amazon.Runtime;
using System.IO;
using AWSSDK_DotNet.UnitTests;
using Amazon.S3.Model;
using Amazon.S3.Model.Internal.MarshallTransformations;
using Amazon.Runtime.Internal.Util;
using System.Threading;
using System.Net;
using Amazon.Runtime.Internal.Auth;
using Amazon.Runtime.Internal;
using Amazon.Runtime.Internal.Transform;
using Amazon.S3;
using Amazon;
using Amazon.Util;
using AWSSDK_DotNet.UnitTests.TestTools;
using ServiceClientGenerator;
using System.Reflection;
using System.Security.Cryptography;
namespace AWSSDK.UnitTests
{
public static class CustomResponses
{
public static void SetResponse(
AmazonServiceClient client,
string content, string requestId, bool isOK)
{
var response = CreateResponseCreator(content, requestId, isOK);
SetResponse(client, response);
}
public static Func<HttpHandlerTests.MockHttpRequest, HttpWebResponse> CreateResponseCreator(
string content, string requestId, bool isOK)
{
var status = isOK ? HttpStatusCode.OK : HttpStatusCode.NotFound;
return (request) =>
{
Dictionary<string, string> headers = new Dictionary<string, string>(StringComparer.Ordinal);
if (!string.IsNullOrEmpty(requestId))
headers.Add(HeaderKeys.RequestIdHeader, requestId);
var response = MockWebResponse.Create(status, headers, content);
if (isOK)
return response;
#if BCL
throw new HttpErrorResponseException(new HttpWebRequestResponseData(response));
#else
throw new NotImplementedException();
#endif
};
}
public static void SetResponse(
AmazonServiceClient client,
Func<HttpHandlerTests.MockHttpRequest, HttpWebResponse> responseCreator)
{
#if BCL
var requestFactory = new HttpHandlerTests.MockHttpRequestFactory();
requestFactory.ResponseCreator = responseCreator;
ReplaceHttpRequestHandler(client, requestFactory);
#else
throw new NotImplementedException();
#endif
}
public static void ReplaceHttpRequestHandler<T>(
AmazonServiceClient client,
IHttpRequestFactory<T> httpRequestFactory)
{
var httpHandler = new HttpHandler<T>(httpRequestFactory, client);
ReplaceHttpHandler(client, httpHandler);
}
public static void ReplaceHttpHandler<T>(
AmazonServiceClient client,
HttpHandler<T> httpHandler)
{
var pipeline = client
.GetType()
.GetProperty("RuntimePipeline", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.GetValue(client, null)
as RuntimePipeline;
pipeline.ReplaceHandler<HttpHandler<T>>(httpHandler);
}
}
}