-
Notifications
You must be signed in to change notification settings - Fork 64
/
HttpClientXRayTracingHandlerTests.cs
130 lines (106 loc) · 4.98 KB
/
HttpClientXRayTracingHandlerTests.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Amazon.XRay.Recorder.Core;
using Amazon.XRay.Recorder.Core.Internal.Entities;
using Amazon.XRay.Recorder.Core.Internal.Utils;
using Amazon.XRay.Recorder.Handlers.System.Net;
using Amazon.XRay.Recorder.UnitTests.Tools;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Amazon.XRay.Recorder.UnitTests
{
[TestClass]
public class HttpClientXRayTracingHandlerTests : TestBase
{
private const string URL = "https://httpbin.org/";
private const string URL404 = "https://httpbin.org/status/404";
private readonly HttpClient _httpClient;
private static AWSXRayRecorder _recorder;
public HttpClientXRayTracingHandlerTests()
{
_httpClient = new HttpClient(new HttpClientXRayTracingHandler(new HttpClientHandler()));
}
[TestInitialize]
public void TestInitialize()
{
_recorder = new AWSXRayRecorder();
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
}
[TestCleanup]
public new void TestCleanup()
{
base.TestCleanup();
_recorder.Dispose();
_recorder = null;
}
[TestMethod]
public async Task TestSendAsync()
{
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
var request = new HttpRequestMessage(HttpMethod.Get, URL);
using(await _httpClient.SendAsync(request)) {}
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();
var traceHeader = request.Headers.GetValues(TraceHeader.HeaderKey).SingleOrDefault();
Assert.IsNotNull(traceHeader);
var requestInfo = segment.Subsegments[0].Http["request"] as Dictionary<string, object>;
Assert.AreEqual(URL, requestInfo["url"]);
Assert.AreEqual("GET", requestInfo["method"]);
var responseInfo = segment.Subsegments[0].Http["response"] as Dictionary<string, object>;
Assert.AreEqual(200, responseInfo["status"]);
Assert.IsNotNull(responseInfo["content_length"]);
}
/// <summary>
/// Ensures that when tracing is disabled that HTTP requests can execute as normal.
/// See https://github.com/aws/aws-xray-sdk-dotnet/issues/57 for more information.
/// </summary>
[TestMethod]
public async Task TestXrayDisabledSendAsync()
{
_recorder = new MockAWSXRayRecorder() { IsTracingDisabledValue = true };
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
Assert.IsTrue(AWSXRayRecorder.Instance.IsTracingDisabled());
var request = new HttpRequestMessage(HttpMethod.Get, URL);
using (var response = await _httpClient.SendAsync(request))
{
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
[TestMethod]
public async Task Test404SendAsync()
{
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
var request = new HttpRequestMessage(HttpMethod.Get, URL404);
using(await _httpClient.SendAsync(request)) {}
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();
var traceHeader = request.Headers.GetValues(TraceHeader.HeaderKey).SingleOrDefault();
Assert.IsNotNull(traceHeader);
var requestInfo = segment.Subsegments[0].Http["request"] as Dictionary<string, object>;
Assert.AreEqual(URL404, requestInfo["url"]);
Assert.AreEqual("GET", requestInfo["method"]);
var responseInfo = segment.Subsegments[0].Http["response"] as Dictionary<string, object>;
Assert.AreEqual(404, responseInfo["status"]);
}
[TestMethod]
public async Task TestXrayContextMissingStrategySendAsync() // Test that respects ContextMissingStrategy
{
_recorder = new MockAWSXRayRecorder();
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
AWSXRayRecorder.Instance.ContextMissingStrategy = Core.Strategies.ContextMissingStrategy.LOG_ERROR;
Assert.IsFalse(AWSXRayRecorder.Instance.IsTracingDisabled());
_recorder.EndSegment();
// The test should not break. No segment is available in the context, however, since the context missing strategy is log error,
// no exception should be thrown by below code.
var request = new HttpRequestMessage(HttpMethod.Get, URL);
using (var response = await _httpClient.SendAsync(request))
{
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
}
}