Skip to content

Commit 7940705

Browse files
committed
Added support for HTTP Header trigger
1 parent 08e8e42 commit 7940705

File tree

6 files changed

+198
-63
lines changed

6 files changed

+198
-63
lines changed

QueueIT.KnownUserV3.SDK.Tests/IntegrationConfig/IntegrationConfigHelpersTest.cs

Lines changed: 135 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
using QueueIT.KnownUserV3.SDK.IntegrationConfig;
2+
using Rhino.Mocks;
23
using System;
34
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
7-
using Rhino;
8-
using Rhino.Mocks;
5+
using System.Collections.Specialized;
96
using System.Web;
107
using Xunit;
118

@@ -45,7 +42,6 @@ public void Evaluate_StartsWith()
4542
Assert.False(ComparisonOperatorHelper.Evaluate(ComparisonOperatorType.StartsWith, true, true, "test1_test1_test", "Test1"));
4643
}
4744

48-
4945
[Fact]
5046
public void Evaluate_EndsWith()
5147
{
@@ -64,6 +60,7 @@ public void Evaluate_MatchesWith()
6460
Assert.False(ComparisonOperatorHelper.Evaluate(ComparisonOperatorType.MatchesWith, true, true, "test1_test1_testshop", ".*Shop.*"));
6561
}
6662
}
63+
6764
public class CookieValidatorHelperTest
6865
{
6966
[Fact]
@@ -135,7 +132,6 @@ public void Evaluate_Test()
135132
}
136133
}
137134

138-
139135
public class UserAgentValidatorHelperTest
140136
{
141137
[Fact]
@@ -154,7 +150,7 @@ public void Evaluate_Test()
154150
triggerPart.IsNegative = true;
155151
Assert.True(UserAgentValidatorHelper.Evaluate(triggerPart, "oglebot sample useraagent"));
156152

157-
153+
158154
triggerPart.ValueToCompare = "googlebot";
159155
triggerPart.Operator = ComparisonOperatorType.Contains;
160156
triggerPart.IsIgnoreCase = false;
@@ -171,6 +167,40 @@ public void Evaluate_Test()
171167
}
172168
}
173169

170+
public class HttpHeaderValidatorTest
171+
{
172+
[Fact]
173+
public void Evaluate_Test()
174+
{
175+
var triggerPart = new TriggerPart()
176+
{
177+
HttpHeaderName = "c1",
178+
Operator = ComparisonOperatorType.Contains,
179+
ValueToCompare = "1"
180+
};
181+
var httpHeaders = new NameValueCollection() { };
182+
Assert.False(HttpHeaderValidatorHelper.Evaluate(triggerPart, httpHeaders));
183+
184+
httpHeaders.Add("c5", "5");
185+
httpHeaders.Add("c1", "1");
186+
httpHeaders.Add("c2", "test");
187+
Assert.True(HttpHeaderValidatorHelper.Evaluate(triggerPart, httpHeaders));
188+
189+
triggerPart.ValueToCompare = "5";
190+
Assert.False(HttpHeaderValidatorHelper.Evaluate(triggerPart, httpHeaders));
191+
192+
triggerPart.ValueToCompare = "Test";
193+
triggerPart.IsIgnoreCase = true;
194+
triggerPart.HttpHeaderName = "c2";
195+
Assert.True(HttpHeaderValidatorHelper.Evaluate(triggerPart, httpHeaders));
196+
197+
triggerPart.ValueToCompare = "Test";
198+
triggerPart.IsIgnoreCase = true;
199+
triggerPart.IsNegative = true;
200+
triggerPart.HttpHeaderName = "c2";
201+
Assert.False(HttpHeaderValidatorHelper.Evaluate(triggerPart, httpHeaders));
202+
}
203+
}
174204

175205
public class IntegrationEvaluatorTest
176206
{
@@ -196,7 +226,7 @@ public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched()
196226
ValidatorType= ValidatorType.CookieValidator
197227
},
198228
new TriggerPart() {
199-
229+
200230
ValidatorType= ValidatorType.UserAgentValidator,
201231
ValueToCompare= "test",
202232
Operator= ComparisonOperatorType.Contains
@@ -210,8 +240,13 @@ public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched()
210240

211241
var url = new Uri("http://test.tesdomain.com:8080/test?q=2");
212242

213-
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, new HttpCookieCollection(),string.Empty) == null);
243+
var httpRequestMock = MockRepository.GenerateMock<HttpRequestBase>();
244+
httpRequestMock.Stub(r => r.Cookies).Return(new HttpCookieCollection());
245+
httpRequestMock.Stub(r => r.UserAgent).Return(string.Empty);
246+
247+
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, httpRequestMock) == null);
214248
}
249+
215250
[Fact]
216251
public void GetMatchedIntegrationConfig_OneTrigger_And_Matched()
217252
{
@@ -252,10 +287,12 @@ public void GetMatchedIntegrationConfig_OneTrigger_And_Matched()
252287

253288

254289
var url = new Uri("http://test.tesdomain.com:8080/test?q=2");
255-
256290

257-
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri,
258-
new HttpCookieCollection() { new HttpCookie("c1", "Value1") }, string.Empty).Name == "integration1");
291+
var httpRequestMock = MockRepository.GenerateMock<HttpRequestBase>();
292+
httpRequestMock.Stub(r => r.Cookies).Return(new HttpCookieCollection() { new HttpCookie("c1", "Value1") });
293+
httpRequestMock.Stub(r => r.UserAgent).Return(string.Empty);
294+
295+
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, httpRequestMock).Name == "integration1");
259296
}
260297

261298
[Fact]
@@ -306,10 +343,71 @@ public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched_UserAgent()
306343

307344
var url = new Uri("http://test.tesdomain.com:8080/test?q=2");
308345

346+
var httpRequestMock = MockRepository.GenerateMock<HttpRequestBase>();
347+
httpRequestMock.Stub(r => r.Cookies).Return(new HttpCookieCollection() { new HttpCookie("c1", "Value1") });
348+
httpRequestMock.Stub(r => r.UserAgent).Return("bot.html google.com googlebot test");
309349

310-
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri,
311-
new HttpCookieCollection() { new HttpCookie("c1", "Value1") }, "bot.html google.com googlebot test")==null);
350+
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, httpRequestMock) == null);
312351
}
352+
353+
[Fact]
354+
public void GetMatchedIntegrationConfig_OneTrigger_And_NotMatched_HttpHeader()
355+
{
356+
var testObject = new IntegrationEvaluator();
357+
358+
var customerIntegration = new CustomerIntegration()
359+
{
360+
361+
Integrations = new List<IntegrationConfigModel> {
362+
new IntegrationConfigModel()
363+
{
364+
Name= "integration1",
365+
Triggers = new List<TriggerModel>() {
366+
new TriggerModel() {
367+
LogicalOperator = LogicalOperatorType.And,
368+
TriggerParts = new List<TriggerPart>() {
369+
new TriggerPart() {
370+
CookieName ="c1",
371+
Operator = ComparisonOperatorType.EqualS,
372+
IsIgnoreCase= true,
373+
ValueToCompare ="value1",
374+
ValidatorType= ValidatorType.CookieValidator
375+
},
376+
new TriggerPart() {
377+
UrlPart = UrlPartType.PageUrl,
378+
ValidatorType= ValidatorType.UrlValidator,
379+
ValueToCompare= "test",
380+
Operator= ComparisonOperatorType.Contains
381+
},
382+
new TriggerPart() {
383+
HttpHeaderName = "Akamai-bot",
384+
ValidatorType = ValidatorType.HttpHeaderValidator,
385+
ValueToCompare= "bot",
386+
Operator= ComparisonOperatorType.Contains,
387+
IsIgnoreCase= true,
388+
IsNegative= true
389+
}
390+
391+
}
392+
}
393+
}
394+
}
395+
396+
}
397+
};
398+
399+
400+
var url = new Uri("http://test.tesdomain.com:8080/test?q=2");
401+
402+
var httpRequestMock = MockRepository.GenerateMock<HttpRequestBase>();
403+
httpRequestMock.Stub(r => r.Cookies).Return(new HttpCookieCollection() { new HttpCookie("c1", "Value1") });
404+
var httpHeaders = new NameValueCollection();
405+
httpHeaders.Add("Akamai-bot", "bot");
406+
httpRequestMock.Stub(r => r.Headers).Return(httpHeaders);
407+
408+
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, httpRequestMock) == null);
409+
}
410+
313411
[Fact]
314412
public void GetMatchedIntegrationConfig_OneTrigger_Or_NotMatched()
315413
{
@@ -348,14 +446,15 @@ public void GetMatchedIntegrationConfig_OneTrigger_Or_NotMatched()
348446

349447
};
350448

351-
352449
var url = new Uri("http://test.tesdomain.com:8080/test?q=2");
353-
354450

451+
var httpRequestMock = MockRepository.GenerateMock<HttpRequestBase>();
452+
httpRequestMock.Stub(r => r.Cookies).Return(new HttpCookieCollection() { new HttpCookie("c2", "value1") });
453+
httpRequestMock.Stub(r => r.UserAgent).Return(string.Empty);
355454

356-
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri,
357-
new HttpCookieCollection() { new HttpCookie("c2", "value1") }, string.Empty) == null);
455+
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, httpRequestMock) == null);
358456
}
457+
359458
[Fact]
360459
public void GetMatchedIntegrationConfig_OneTrigger_Or_Matched()
361460
{
@@ -395,10 +494,10 @@ public void GetMatchedIntegrationConfig_OneTrigger_Or_Matched()
395494

396495
var url = new Uri("http://test.tesdomain.com:8080/test?q=2");
397496
var httpRequestMock = MockRepository.GenerateMock<HttpRequestBase>();
497+
httpRequestMock.Stub(r => r.Cookies).Return(new HttpCookieCollection() { new HttpCookie("c1", "value1") });
498+
httpRequestMock.Stub(r => r.UserAgent).Return(string.Empty);
398499

399-
400-
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri,
401-
new HttpCookieCollection() { new HttpCookie("c1", "value1") }, string.Empty).Name == "integration1");
500+
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, httpRequestMock).Name == "integration1");
402501
}
403502

404503
[Fact]
@@ -445,12 +544,14 @@ public void GetMatchedIntegrationConfig_TwoTriggers_Matched()
445544

446545
};
447546

448-
547+
449548
var url = new Uri("http://test.tesdomain.com:8080/test?q=2");
450549

550+
var httpRequestMock = MockRepository.GenerateMock<HttpRequestBase>();
551+
httpRequestMock.Stub(r => r.Cookies).Return(new HttpCookieCollection());
552+
httpRequestMock.Stub(r => r.UserAgent).Return(string.Empty);
451553

452-
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri,
453-
new HttpCookieCollection() { }, string.Empty).Name=="integration1", string.Empty);
554+
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, httpRequestMock).Name == "integration1", string.Empty);
454555
}
455556

456557
[Fact]
@@ -499,8 +600,11 @@ public void GetMatchedIntegrationConfig_TwoTriggers_NotMatched()
499600

500601
var url = new Uri("http://test.tesdomain.com:8080/test?q=2");
501602

603+
var httpRequestMock = MockRepository.GenerateMock<HttpRequestBase>();
604+
httpRequestMock.Stub(r => r.Cookies).Return(new HttpCookieCollection());
605+
httpRequestMock.Stub(r => r.UserAgent).Return(string.Empty);
502606

503-
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, new HttpCookieCollection() { }, string.Empty) ==null);
607+
Assert.True(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, httpRequestMock) == null);
504608
}
505609

506610
[Fact]
@@ -574,11 +678,12 @@ public void GetMatchedIntegrationConfig_ThreeIntegrationsInOrder_SecondMatched()
574678
};
575679

576680
var url = new Uri("http://test.tesdomain.com:8080/test?q=2");
681+
577682
var httpRequestMock = MockRepository.GenerateMock<HttpRequestBase>();
683+
httpRequestMock.Stub(r => r.Cookies).Return(new HttpCookieCollection() { new HttpCookie("c1") { Value = "Value1" } });
684+
httpRequestMock.Stub(r => r.UserAgent).Return(string.Empty);
578685

579-
Assert.False(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri,
580-
new HttpCookieCollection() { new HttpCookie("c1") { Value = "Value1" } }, string.Empty).Name=="integration2");
686+
Assert.False(testObject.GetMatchedIntegrationConfig(customerIntegration, url.AbsoluteUri, httpRequestMock).Name == "integration2");
581687
}
582688
}
583-
}
584-
689+
}

0 commit comments

Comments
 (0)