//-----------------------------------------------------------------------------
//
// Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// A copy of the License is located at
//
// http://aws.amazon.com/apache2.0
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using Amazon.Runtime;
using Amazon.Runtime.Internal;
using Amazon.Runtime.Internal.Transform;
using Amazon.Util;
namespace Amazon.XRay.Recorder.UnitTests.Tools
{
public static class CustomResponses
{
#if NETFRAMEWORK
public static void SetResponse(
AmazonServiceClient client, string requestId = null, string s3ExtendedRequestId = null, string content = null, bool isOK = true)
{
var response = Create(content, requestId, s3ExtendedRequestId, isOK);
SetResponse(client, response);
}
public static void SetResponse(
AmazonServiceClient client,
Func responseCreator)
{
var pipeline = client
.GetType()
.GetProperty("RuntimePipeline", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.GetValue(client, null)
as RuntimePipeline;
var requestFactory = new MockHttpRequestFactory();
requestFactory.ResponseCreator = responseCreator;
var httpHandler = new HttpHandler(requestFactory, client);
pipeline.ReplaceHandler>(httpHandler);
}
private static Func Create(
string content, string requestId, string s3ExtendedRequestId, bool isOK)
{
var status = isOK ? HttpStatusCode.OK : HttpStatusCode.NotFound;
return (request) =>
{
Dictionary headers = new Dictionary(StringComparer.Ordinal);
if (!string.IsNullOrEmpty(requestId))
{
headers.Add(HeaderKeys.RequestIdHeader, requestId);
}
// https://aws.amazon.com/premiumsupport/knowledge-center/s3-request-id-values/
if (!string.IsNullOrEmpty(s3ExtendedRequestId))
{
headers.Add(HeaderKeys.XAmzId2Header, s3ExtendedRequestId);
}
var response = MockWebResponse.Create(status, headers, content);
if (isOK)
{
return response;
}
throw new HttpErrorResponseException(new HttpWebRequestResponseData(response));
};
}
#else
public static void SetResponse(AmazonServiceClient client, string requestId = null, string s3ExtendedRequestId = null, string content = null, bool isOK = true)
{
var response = Create(content, requestId, s3ExtendedRequestId, isOK);
SetResponse(client, response);
}
public static void SetResponse(AmazonServiceClient client, Func responseCreator)
{
var pipeline = client
.GetType()
.GetProperty("RuntimePipeline", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic)
.GetValue(client, null)
as RuntimePipeline;
var requestFactory = new MockHttpRequestFactory();
requestFactory.ResponseCreator = responseCreator;
var httpHandler = new HttpHandler(requestFactory, client);
pipeline.ReplaceHandler>(httpHandler);
}
private static Func Create(
string content, string requestId, string s3ExtendedRequestId, bool isOK)
{
var status = isOK ? HttpStatusCode.OK : HttpStatusCode.NotFound;
return (request) =>
{
Dictionary headers = new Dictionary(StringComparer.Ordinal);
if (!string.IsNullOrEmpty(requestId))
{
headers.Add(HeaderKeys.RequestIdHeader, requestId);
}
// https://aws.amazon.com/premiumsupport/knowledge-center/s3-request-id-values/
if (!string.IsNullOrEmpty(s3ExtendedRequestId))
{
headers.Add(HeaderKeys.XAmzId2Header, s3ExtendedRequestId);
}
var response = MockWebResponse.Create(status, headers, content);
if (isOK)
{
return response;
}
throw new HttpErrorResponseException(CustomWebResponse.GenerateWebResponse(response));
};
}
#endif
}
}