-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy pathRequestEventHandlerTests.cs
167 lines (147 loc) · 7.44 KB
/
RequestEventHandlerTests.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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos
{
using System;
using System.Collections.Specialized;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Collections;
using Microsoft.Azure.Cosmos.Common;
using Microsoft.Azure.Cosmos.Internal;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Collections;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
/// <summary>
/// Tests for <see cref="SendingRequestEventArgs"/> class.
/// </summary>
[TestClass]
public class RequestEventHandlerTests
{
private const string newHeaderKey = "NewlyAddedHeaderKey";
private const string newHeaderValue = "NewlyAddedHeaderValue";
/// <summary>
/// Tests that the event raised on SendingRequestEventArgs acts on the request before processing the request.
/// </summary>
[TestMethod]
public async Task TestServerStoreModelWithRequestEventHandler()
{
EventHandler<SendingRequestEventArgs> sendingRequest;
EventHandler<ReceivedResponseEventArgs> receivedResponse;
sendingRequest = this.SendingRequestEventHandler;
receivedResponse = this.ReceivedRequestEventHandler;
ServerStoreModel storeModel = new ServerStoreModel(GetMockStoreClient(), sendingRequest, receivedResponse);
using (new ActivityScope(Guid.NewGuid()))
{
using (DocumentServiceRequest request = DocumentServiceRequest.Create(
OperationType.Read,
ResourceType.Document,
AuthorizationTokenType.PrimaryMasterKey))
{
DocumentServiceResponse result = await storeModel.ProcessMessageAsync(request);
Assert.IsTrue(request.Headers.Get(newHeaderKey) != null);
}
}
}
/// <summary>
/// Tests when no event is raised before processing the request.
/// </summary>
[TestMethod]
public async Task TestServerStoreModelWithNoRequestEventHandler()
{
EventHandler<SendingRequestEventArgs> sendingRequest = null;
EventHandler<ReceivedResponseEventArgs> receivedResponse = null;
ServerStoreModel storeModel = new ServerStoreModel(GetMockStoreClient(), sendingRequest, receivedResponse);
using (new ActivityScope(Guid.NewGuid()))
{
using (DocumentServiceRequest request = DocumentServiceRequest.Create(
OperationType.Read,
ResourceType.Document,
AuthorizationTokenType.PrimaryMasterKey))
{
DocumentServiceResponse result = await storeModel.ProcessMessageAsync(request);
Assert.IsTrue(request.Headers.Get(newHeaderKey) == null);
}
}
}
private StoreClient GetMockStoreClient()
{
Mock<IAddressResolver> mockAddressCache = this.GetMockAddressCache();
AddressSelector addressSelector = new AddressSelector(mockAddressCache.Object, Protocol.Tcp);
TransportClient mockTransportClient = this.GetMockTransportClient();
ISessionContainer sessionContainer = new SessionContainer(string.Empty);
StoreReader storeReader = new StoreReader(mockTransportClient, addressSelector, new AddressEnumerator(), sessionContainer, false);
Mock<IAuthorizationTokenProvider> mockAuthorizationTokenProvider = new Mock<IAuthorizationTokenProvider>();
mockAuthorizationTokenProvider.Setup(provider => provider.AddSystemAuthorizationHeaderAsync(
It.IsAny<DocumentServiceRequest>(), It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns(Task.FromResult(0));
// setup max replica set size on the config reader
ReplicationPolicy replicationPolicy = new ReplicationPolicy();
replicationPolicy.MaxReplicaSetSize = 4;
Mock<IServiceConfigurationReader> mockServiceConfigReader = new Mock<IServiceConfigurationReader>();
mockServiceConfigReader.SetupGet(x => x.UserReplicationPolicy).Returns(replicationPolicy);
return new StoreClient(
mockAddressCache.Object,
sessionContainer,
mockServiceConfigReader.Object,
mockAuthorizationTokenProvider.Object,
Protocol.Tcp,
mockTransportClient);
}
private TransportClient GetMockTransportClient()
{
// create a mock TransportClient
Mock<TransportClient> mockTransportClient = new Mock<TransportClient>();
// setup mock to return respone
StoreResponse mockStoreResponse = new StoreResponse();
mockStoreResponse.Headers = new StoreResponseNameValueCollection
{
{ WFConstants.BackendHeaders.LSN, "110" },
{ WFConstants.BackendHeaders.ActivityId, "ACTIVITYID1_1" }
};
mockTransportClient.Setup(
client => client.InvokeResourceOperationAsync(
It.IsAny<TransportAddressUri>(),
It.IsAny<DocumentServiceRequest>()))
.ReturnsAsync(mockStoreResponse);
return mockTransportClient.Object;
}
private Mock<IAddressResolver> GetMockAddressCache()
{
// construct dummy rntbd URIs
AddressInformation[] addressInformation = new AddressInformation[3];
for (int i = 0; i <= 2; i++)
{
addressInformation[i] = new AddressInformation(
physicalUri: "rntbd://dummytenant.documents.azure.com:14003/apps/APPGUID/services/SERVICEGUID/partitions/PARTITIONGUID/replicas/"
+ i.ToString("G", CultureInfo.CurrentCulture) + (i == 0 ? "p" : "s") + "/",
isPrimary: i == 0,
protocol: Documents.Client.Protocol.Tcp,
isPublic: true);
}
Mock<IAddressResolver> mockAddressCache = new Mock<IAddressResolver>();
mockAddressCache.Setup(
cache => cache.ResolveAsync(
It.IsAny<DocumentServiceRequest>(),
It.IsAny<bool>(),
new CancellationToken()))
.ReturnsAsync(new PartitionAddressInformation(addressInformation));
return mockAddressCache;
}
private void SendingRequestEventHandler(object sender, SendingRequestEventArgs e)
{
Assert.IsFalse(e.IsHttpRequest());
e.DocumentServiceRequest.Headers.Add(newHeaderKey, newHeaderValue);
}
private void ReceivedRequestEventHandler(object sender, ReceivedResponseEventArgs e)
{
Assert.IsFalse(e.IsHttpResponse());
Assert.AreEqual(newHeaderValue, e.DocumentServiceRequest.Headers[newHeaderKey]);
Assert.AreEqual("ACTIVITYID1_1", e.DocumentServiceResponse.Headers[WFConstants.BackendHeaders.ActivityId]);
}
}
}