From 4748ba681440f71c89fad17415bd950956a2db04 Mon Sep 17 00:00:00 2001 From: Muhammad Othman Date: Mon, 14 Apr 2025 17:48:45 -0400 Subject: [PATCH 1/2] Enable MockHttpRequestFactory in non netframework targets --- .../3f30d84d-d6bc-4b07-8d9e-f1356be1d8ac.json | 8 +++ .../Custom/Runtime/CustomResponses.cs | 4 ++ .../Custom/Runtime/HttpHandlerTests.cs | 51 +++++++++++++++---- .../Custom/TestTools/MockWebResponse.cs | 28 +++++++++- 4 files changed, 80 insertions(+), 11 deletions(-) create mode 100644 generator/.DevConfigs/3f30d84d-d6bc-4b07-8d9e-f1356be1d8ac.json diff --git a/generator/.DevConfigs/3f30d84d-d6bc-4b07-8d9e-f1356be1d8ac.json b/generator/.DevConfigs/3f30d84d-d6bc-4b07-8d9e-f1356be1d8ac.json new file mode 100644 index 000000000000..23af0af2e266 --- /dev/null +++ b/generator/.DevConfigs/3f30d84d-d6bc-4b07-8d9e-f1356be1d8ac.json @@ -0,0 +1,8 @@ + +{ + "core": { + "changeLogMessages": ["Enable MockHttpRequestFactory in non netframework targets"], + "type": "patch", + "updateMinimum": false + } +} \ No newline at end of file diff --git a/sdk/test/UnitTests/Custom/Runtime/CustomResponses.cs b/sdk/test/UnitTests/Custom/Runtime/CustomResponses.cs index 636123b91694..5e93275b59e4 100644 --- a/sdk/test/UnitTests/Custom/Runtime/CustomResponses.cs +++ b/sdk/test/UnitTests/Custom/Runtime/CustomResponses.cs @@ -62,9 +62,13 @@ public static void SetResponse( AmazonServiceClient client, Func responseCreator) { +#if BCL var requestFactory = new HttpHandlerTests.MockHttpRequestFactory(); requestFactory.ResponseCreator = responseCreator; ReplaceHttpRequestHandler(client, requestFactory); +#else + throw new NotImplementedException(); +#endif } public static void ReplaceHttpRequestHandler( diff --git a/sdk/test/UnitTests/Custom/Runtime/HttpHandlerTests.cs b/sdk/test/UnitTests/Custom/Runtime/HttpHandlerTests.cs index ffdb5c72c0d9..a8b2c3200cec 100644 --- a/sdk/test/UnitTests/Custom/Runtime/HttpHandlerTests.cs +++ b/sdk/test/UnitTests/Custom/Runtime/HttpHandlerTests.cs @@ -10,6 +10,7 @@ using Amazon.Runtime.Internal.Util; using System.Net; using System.Reflection; +using System.Net.Http; using Amazon.S3; using Amazon.S3.Model; @@ -167,7 +168,12 @@ private ExecutionContext CreateExecutionContextForListBuckets() public class MockHttpRequestFactory : IHttpRequestFactory { public Action GetResponseAction { get; set; } + +#if BCL public Func ResponseCreator { get; set; } +#else + public Func ResponseCreator { get; set; } +#endif public MockHttpRequest LastCreatedRequest { get; private set; } @@ -201,23 +207,29 @@ public class MockHttpRequest : IHttpRequest public Uri RequestUri { get; set; } public Action GetResponseAction { get; set; } - public Func ResponseCreator { get; set; } public Version HttpProtocolVersion { get; set; } +#if BCL + public Func ResponseCreator { get; set; } + public MockHttpRequest(Uri requestUri, Action action, Func responseCreator = null) +#else + public Func ResponseCreator { get; set; } + + public MockHttpRequest(Uri requestUri, Action action, Func responseCreator = null) +#endif { this.RequestUri = requestUri; this.GetResponseAction = action; -#if BCL this.ResponseCreator = responseCreator ?? CreateResponse; -#else - throw new NotImplementedException(); -#endif } #if BCL private HttpWebResponse CreateResponse(MockHttpRequest request) +#else + private HttpResponseMessage CreateResponse(MockHttpRequest request) +#endif { // Extract the last segment of the URI, this is the custom URI // sent by the unit tests. @@ -227,10 +239,22 @@ private HttpWebResponse CreateResponse(MockHttpRequest request) if (response.StatusCode >= HttpStatusCode.OK && response.StatusCode <= (HttpStatusCode)299) return response; else - throw new HttpErrorResponseException(new HttpWebRequestResponseData(response)); - } +#if BCL + throw new HttpErrorResponseException(new HttpWebRequestResponseData(response)); +#else + { + var instance = Activator.CreateInstance( + typeof(HttpClientResponseData), + BindingFlags.Instance | BindingFlags.NonPublic, + binder: null, + args: new[] { response }, + culture: null + ); + throw new HttpErrorResponseException(instance as HttpClientResponseData); + } #endif - + } + public void ConfigureRequest(IRequestContext requestContext) { this.IsConfigureRequestCalled = true; @@ -250,14 +274,21 @@ public Stream GetRequestContent() public Amazon.Runtime.Internal.Transform.IWebResponseData GetResponse() { -#if BCL if (this.GetResponseAction!=null) this.GetResponseAction(); var response = ResponseCreator(this); +#if BCL return new HttpWebRequestResponseData(response); #else - throw new NotImplementedException(); + var instance = Activator.CreateInstance( + typeof(HttpClientResponseData), + BindingFlags.Instance | BindingFlags.NonPublic, + binder: null, + args: new[] { response }, + culture: null + ); + return instance as HttpClientResponseData; #endif } diff --git a/sdk/test/UnitTests/Custom/TestTools/MockWebResponse.cs b/sdk/test/UnitTests/Custom/TestTools/MockWebResponse.cs index 34d91ef0b24c..fb3752629d2e 100644 --- a/sdk/test/UnitTests/Custom/TestTools/MockWebResponse.cs +++ b/sdk/test/UnitTests/Custom/TestTools/MockWebResponse.cs @@ -7,11 +7,13 @@ using System.Net; using System.Reflection; using System.IO; +using System.Net.Http; namespace AWSSDK.UnitTests { public class MockWebResponse { +#if NETFRAMEWORK public static HttpWebResponse CreateFromResource(string resourceName) { var rawResponse = Utils.GetResourceText(resourceName); @@ -21,9 +23,33 @@ public static HttpWebResponse CreateFromResource(string resourceName) return Create(statusCode, response.Headers, response.Body); } +#else + public static HttpResponseMessage CreateFromResource(string resourceName) + { + var rawResponse = Utils.GetResourceText(resourceName); + + var response = ParseRawReponse(rawResponse); + var statusCode = ParseStatusCode(response.StatusLine); + var httpResponseMessage = new HttpResponseMessage(statusCode); + + if (response.Headers != null) + { + foreach (var header in response.Headers) + { + httpResponseMessage.Headers.TryAddWithoutValidation(header.Key, header.Value); + } + } + + var body = response.Body ?? string.Empty; + var responseBodyStream = Utils.CreateStreamFromString(body); + + httpResponseMessage.Content = new StreamContent(responseBodyStream); + return httpResponseMessage; + } +#endif public static HttpWebResponse Create(HttpStatusCode statusCode, - IDictionary headers, string body = null) + IDictionary headers, string body = null) { var type = typeof(HttpWebResponse); var assembly = Assembly.GetAssembly(type); From 6fab6aae5c2f49a81d4ecee44e01d0a9254582b0 Mon Sep 17 00:00:00 2001 From: Muhammad Othman Date: Wed, 16 Apr 2025 17:02:30 -0400 Subject: [PATCH 2/2] Remove unnecessary devconfig --- .../.DevConfigs/3f30d84d-d6bc-4b07-8d9e-f1356be1d8ac.json | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 generator/.DevConfigs/3f30d84d-d6bc-4b07-8d9e-f1356be1d8ac.json diff --git a/generator/.DevConfigs/3f30d84d-d6bc-4b07-8d9e-f1356be1d8ac.json b/generator/.DevConfigs/3f30d84d-d6bc-4b07-8d9e-f1356be1d8ac.json deleted file mode 100644 index 23af0af2e266..000000000000 --- a/generator/.DevConfigs/3f30d84d-d6bc-4b07-8d9e-f1356be1d8ac.json +++ /dev/null @@ -1,8 +0,0 @@ - -{ - "core": { - "changeLogMessages": ["Enable MockHttpRequestFactory in non netframework targets"], - "type": "patch", - "updateMinimum": false - } -} \ No newline at end of file