-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathHttpWebRequestTracingExtensionTests.cs
232 lines (190 loc) · 9.02 KB
/
HttpWebRequestTracingExtensionTests.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//-----------------------------------------------------------------------------
// <copyright file="HttpWebRequestTracingExtensionTests.cs" company="Amazon.com">
// Copyright 2016 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.
// </copyright>
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Net;
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 HttpWebRequestTracingExtensionTests : TestBase
{
private const string URL = "https://httpbin.org/";
private const string URL404 = "https://httpbin.org/status/404";
private static AWSXRayRecorder _recorder;
[TestInitialize]
public void TestInitialize()
{
_recorder = new AWSXRayRecorder();
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
}
[TestCleanup]
public new void TestCleanup()
{
base.TestCleanup();
_recorder.Dispose();
_recorder = null;
}
[TestMethod]
public void TestGetResponseTraced()
{
var request = (HttpWebRequest)WebRequest.Create(URL);
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
using (request.GetResponseTraced())
{
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();
Assert.IsNotNull(request.Headers[TraceHeader.HeaderKey]);
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 void TestXrayDisabledGetResponseTraced()
{
_recorder = new MockAWSXRayRecorder() { IsTracingDisabledValue = true };
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
Assert.IsTrue(AWSXRayRecorder.Instance.IsTracingDisabled());
var request = (HttpWebRequest)WebRequest.Create(URL);
using (var response = request.GetResponseTraced() as HttpWebResponse)
{
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
[TestMethod]
public async Task TestGetAsyncResponseTraced()
{
var request = (HttpWebRequest)WebRequest.Create(URL);
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
using (await request.GetAsyncResponseTraced())
{
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();
Assert.IsNotNull(request.Headers[TraceHeader.HeaderKey]);
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 TestXrayDisabledGetAsyncResponseTraced()
{
_recorder = new MockAWSXRayRecorder() { IsTracingDisabledValue = true };
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
Assert.IsTrue(AWSXRayRecorder.Instance.IsTracingDisabled());
var request = (HttpWebRequest)WebRequest.Create(URL);
using (var response = await request.GetAsyncResponseTraced() as HttpWebResponse)
{
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
#if !NETFRAMEWORK
[TestMethod]
public void TestExceptionGetResponseTraced()
{
var request = (HttpWebRequest)WebRequest.Create(URL404);
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
try
{
using (request.GetResponseTraced()) {}
Assert.Fail();
}
catch (WebException) //expected
{
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();
Assert.IsNotNull(request.Headers[TraceHeader.HeaderKey]);
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"]);
Assert.IsNotNull(responseInfo["content_length"]);
var subsegment = segment.Subsegments[0];
Assert.IsTrue(subsegment.HasError);
Assert.IsFalse(subsegment.HasFault);
}
}
[TestMethod]
public async Task TestExceptionGetAsyncResponseTraced()
{
var request = (HttpWebRequest)WebRequest.Create(URL404);
AWSXRayRecorder.Instance.BeginSegment("parent", TraceId);
try
{
using (await request.GetAsyncResponseTraced()) {}
Assert.Fail();
}
catch (WebException) //expected
{
var segment = AWSXRayRecorder.Instance.TraceContext.GetEntity();
AWSXRayRecorder.Instance.EndSegment();
Assert.IsNotNull(request.Headers[TraceHeader.HeaderKey]);
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"]);
Assert.IsNotNull(responseInfo["content_length"]);
var subsegment = segment.Subsegments[0];
Assert.IsTrue(subsegment.HasError);
Assert.IsFalse(subsegment.HasFault);
}
}
#endif
[TestMethod]
public async Task TestContextMissingStrategyGetAsyncResponseTraced()
{
_recorder = new AWSXRayRecorder();
AWSXRayRecorder.InitializeInstance(recorder: _recorder);
Assert.IsFalse(AWSXRayRecorder.Instance.IsTracingDisabled());
AWSXRayRecorder.Instance.ContextMissingStrategy = Core.Strategies.ContextMissingStrategy.LOG_ERROR;
_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 = (HttpWebRequest)WebRequest.Create(URL);
using (var response = await request.GetAsyncResponseTraced() as HttpWebResponse)
{
Assert.IsNotNull(response);
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
}
}
}
}