public void testLoadPost() { storage = new LiteDBStorage(_testDbFilename); Assert.AreEqual(0, storage.TotalItems); var queue = new PersistentBlockingQueue(storage, new PayloadToJsonString()); var endpoint = new SnowplowHttpCollectorEndpoint(host: _collectorHostUri, port: 8080, protocol: HttpProtocol.HTTP, method: HttpMethod.POST, byteLimitPost: 100000); var emitter = new AsyncEmitter(endpoint: endpoint, queue: queue, sendLimit: 1000); var clientSession = new ClientSession(_testClientSessionFilename); Assert.IsFalse(tracker.Started); tracker.Start(emitter: emitter, clientSession: clientSession, trackerNamespace: "testNamespace", appId: "testAppId", encodeBase64: false, synchronous: false); Assert.IsTrue(tracker.Started); for (int i = 0; i < 100; i++) { tracker.Track(new Structured() .SetCategory("exampleCategory") .SetAction("exampleAction") .SetLabel("exampleLabel") .SetProperty("exampleProperty") .SetValue(17) .Build() ); } tracker.Flush(); tracker.Stop(); Assert.IsFalse(tracker.Started); }
private bool ensureTimingWorksGet(Tracker t, MockGet g, bool expectB64 = true) { if (!expectB64) { Assert.Fail("non b64 mode not supported"); } t.Track(new Timing().SetCategory("category").SetVariable("variable").SetTiming(5).SetLabel("label").Build()); t.Flush(); var actual = g.Queries[0]; var expectedJsonString = @"{""schema"":""iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0"",""data"":{""schema"":""iglu:com.snowplowanalytics.snowplow/timing/jsonschema/1-0-0"",""data"":{""category"":""category"",""label"":""label"",""timing"":5,""variable"":""variable""}}}"; var expectedB64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(expectedJsonString)); var expected = new Dictionary <string, string> { { "e", "ue" }, { "ue_px", expectedB64 } }; assertQueryContainsDefaults(actual); assertQueryContainsAll(actual, expected); return(true); }
private bool ensureScreenViewWorksGet(Tracker t, MockGet g, bool expectB64 = true) { if (!expectB64) { Assert.Fail("non b64 mode not supported"); } t.Track(new ScreenView().SetName("entry screen").SetId("0001").Build()); t.Flush(); var actual = g.Queries[0]; var expectedJsonString = @"{""schema"":""iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0"",""data"":{""schema"":""iglu:com.snowplowanalytics.snowplow/screen_view/jsonschema/1-0-0"",""data"":{""name"":""entry screen"",""id"":""0001""}}}"; var expectedB64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(expectedJsonString)); var expected = new Dictionary <string, string> { { "e", "ue" }, { "ue_px", expectedB64 } }; assertQueryContainsDefaults(actual); assertQueryContainsAll(actual, expected); return(true); }
private bool ensureStructEventsWorkGet(Tracker t, MockGet g) { t.Track(new Structured() .SetCategory("myCategory") .SetAction("myAction") .SetLabel("myLabel") .SetProperty("myProperty") .SetValue(17) .Build()); t.Flush(); var expectedRegex = new Regex( String.Format(@"http://snowplowanalytics.com/i\?e=se&se_ca={0}&se_ac={1}&se_la={2}&se_pr={3}&se_va={4}&eid=[^&]+&dtm=[^&]+&tv=[^&]+&tna=testNamespace&aid=testAppId&p=pc", Regex.Escape(WebUtility.UrlEncode("myCategory")), Regex.Escape(WebUtility.UrlEncode("myAction")), Regex.Escape(WebUtility.UrlEncode("myLabel")), Regex.Escape(WebUtility.UrlEncode("myProperty")), Regex.Escape(WebUtility.UrlEncode("17")) ) ); var actual = g.Queries[0]; Assert.IsTrue(expectedRegex.Match(actual).Success, String.Format("{0} doesn't match regex {1}", actual, expectedRegex.ToString())); return(true); }
private bool ensureSettersWorkGet(Tracker t, MockGet g) { t.SetPlatform(Platform.Mob); t.SetUserId("malcolm"); t.SetScreenResolution(100, 200); t.SetViewport(50, 60); t.SetColorDepth(24); t.SetTimezone("Europe London"); t.SetLang("en"); t.Track(new PageView() .SetPageUrl("http://www.example.com") .SetPageTitle("title page") .SetReferrer("http://www.referrer.com") .SetTrueTimestamp(1000000000000) .Build()); t.Flush(); var actual = g.Queries[0]; var expected = new Dictionary <string, string> { { "e", "pv" }, { "url", "http://www.example.com" }, { "page", "title page" }, { "refr", "http://www.referrer.com" }, { "tv", Version.VERSION }, { "tna", "testNamespace" }, { "aid", "testAppId" }, { "p", "mob" }, { "res", "100x200" }, { "vp", "50x60" }, { "cd", "24" }, { "tz", "Europe London" }, { "lang", "en" }, { "ttm", "1000000000000" } }; foreach (var k in expected.Keys) { var matcher = new Regex("[&?]" + k + "=(?<value>[^&]+)"); var match = matcher.Match(actual); Assert.IsTrue(match.Success, "Couldn't find key " + k + " in query " + actual); var found = match.Result("${value}"); string decoded = WebUtility.UrlDecode(found); Assert.AreEqual(expected[k], decoded, "key " + k + " has the wrong value " + decoded); } return(true); }
private bool ensureContextsWorkGet(Tracker t, MockGet g, bool useb64 = true) { Assert.IsTrue(useb64); var pageContext = new GenericContext() .SetSchema("iglu:com.snowplowanalytics.snowplow/page/jsonschema/1-0-0") .Add("type", "test") .Add("public", false) .Build(); var userContext = new GenericContext() .SetSchema("iglu:com.snowplowanalytics.snowplow/user/jsonschema/1-0-0") .Add("age", 40) .Add("name", "ned") .Build(); var contexts = new List <IContext> { pageContext, userContext }; t.Track(new PageView().SetPageUrl("http://www.example.com").SetCustomContext(contexts).Build()); t.Flush(); var actual = g.Queries[0]; var expected = new Dictionary <string, string> { { "e", "pv" }, { "url", "http://www.example.com" } }; var expectedJsonString = @"{""schema"":""iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-1"",""data"":[{""schema"":""iglu:com.snowplowanalytics.snowplow/page/jsonschema/1-0-0"",""data"":{""type"":""test"",""public"":false}},{""schema"":""iglu:com.snowplowanalytics.snowplow/user/jsonschema/1-0-0"",""data"":{""age"":40,""name"":""ned""}}]}"; var expectedB64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(expectedJsonString)); var extractCx = new Regex("cx=(?<cx>[^&]+)"); Assert.IsTrue(extractCx.IsMatch(actual), "couldn't find cx in query"); var match = extractCx.Match(actual); Assert.IsTrue(match.Success); var actualCx = match.Result("${cx}"); Assert.AreEqual(expectedB64, actualCx); return(true); }
public void testAsyncPostOnSuccess() { using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()) { ShimHttpWebRequest.AllInstances.GetResponse = fake; int successes = -1; var e = new AsyncEmitter("d3rkrsqld9gmqf.cloudfront.net", HttpProtocol.HTTP, null, HttpMethod.POST, 10, (successCount) => { successes = successCount; }); var t = new Tracker(e); t.TrackPageView("first"); t.TrackPageView("second"); t.Flush(true); Assert.AreEqual(2, successes); } }
public void testAsyncTrackPageView() { using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()) { ShimHttpWebRequest.AllInstances.GetResponse = fake; var t = new Tracker(new AsyncEmitter("d3rkrsqld9gmqf.cloudfront.net")); t.TrackPageView("http://www.example.com", "title page", "http://www.referrer.com"); var expected = new Dictionary <string, string> { { "e", "pv" }, { "url", "http://www.example.com" }, { "page", "title page" }, { "refr", "http://www.referrer.com" } }; t.Flush(true); checkResult(expected, payloads[payloads.Count - 1]); } }
public void testAsyncPostOnFailure() { using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()) { ShimHttpWebRequest.AllInstances.GetResponse = badFake; int?successes = null; List <Dictionary <string, string> > failureList = null; var e = new AsyncEmitter("d3rkrsqld9gmqf.cloudfront.net", HttpProtocol.HTTP, null, HttpMethod.POST, 10, null, (successCount, failures) => { successes = successCount; failureList = failures; }); var t = new Tracker(e); t.TrackPageView("first"); t.TrackPageView("second"); t.Flush(true); Assert.AreEqual(0, successes); Assert.AreEqual("first", failureList[0]["url"]); Assert.AreEqual("second", failureList[1]["url"]); } }
private bool ensureUnstructEventGet(Tracker t, MockGet g, bool expectBase64 = false) { var eventJson = new SelfDescribingJson( "iglu:com.acme/test/jsonschema/1-0-0", new Dictionary <string, string> { { "page", "testpage" }, { "user", "tester" } } ); t.Track(new SelfDescribing().SetEventData(eventJson).Build()); t.Flush(); var actual = g.Queries[0]; var expectedPayload = @"{""schema"":""iglu:com.snowplowanalytics.snowplow/unstruct_event/jsonschema/1-0-0"",""data"":{""schema"":""iglu:com.acme/test/jsonschema/1-0-0"",""data"":{""page"":""testpage"",""user"":""tester""}}}"; var expected = new Dictionary <string, string> { { "e", "ue" } }; if (expectBase64) { string base64encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(expectedPayload)); expected.Add("ue_px", base64encoded); } else { expected.Add("ue_pr", expectedPayload); } assertQueryContainsDefaults(actual); assertQueryContainsAll(actual, expected); return(true); }
// --- Event testers private bool ensurePageViewsWorkGet(Tracker t, MockGet g) { t.Track(new PageView() .SetPageUrl("http://www.example.com") .SetReferrer("http://www.referrer.com") .SetPageTitle("title page") .Build()); t.Flush(); var expectedRegex = new Regex( String.Format(@"http://snowplowanalytics.com/i\?e=pv&url={0}&page={1}&refr={2}&eid=[^&]+&dtm=[^&]+&tv=[^&]+&tna=testNamespace&aid=testAppId&p=pc", Regex.Escape(WebUtility.UrlEncode("http://www.example.com")), Regex.Escape(WebUtility.UrlEncode("title page")), Regex.Escape(WebUtility.UrlEncode("http://www.referrer.com")) ) ); var actual = g.Queries[0]; Assert.IsTrue(Uri.IsWellFormedUriString(actual, UriKind.Absolute)); Assert.IsTrue(expectedRegex.Match(actual).Success, String.Format("{0} doesn't match {1}", actual, expectedRegex.ToString())); return(true); }
public void testAsyncPostOnFailure() { using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()) { ShimHttpWebRequest.AllInstances.GetResponse = badFake; int? successes = null; List<Dictionary<string, string>> failureList = null; var e = new AsyncEmitter("d3rkrsqld9gmqf.cloudfront.net", HttpProtocol.HTTP, null, HttpMethod.POST, 10, null, (successCount, failures) => { successes = successCount; failureList = failures; }); var t = new Tracker(e); t.TrackPageView("first"); t.TrackPageView("second"); t.Flush(true); Assert.AreEqual(0, successes); Assert.AreEqual("first", failureList[0]["url"]); Assert.AreEqual("second", failureList[1]["url"]); } }
public void testAsyncTrackPageView() { using (Microsoft.QualityTools.Testing.Fakes.ShimsContext.Create()) { ShimHttpWebRequest.AllInstances.GetResponse = fake; var t = new Tracker(new AsyncEmitter("d3rkrsqld9gmqf.cloudfront.net")); t.TrackPageView("http://www.example.com", "title page", "http://www.referrer.com"); var expected = new Dictionary<string, string> { {"e", "pv"}, {"url", "http://www.example.com"}, {"page", "title page"}, {"refr", "http://www.referrer.com"} }; t.Flush(true); checkResult(expected, payloads[payloads.Count - 1]); } }
private bool ensureEcommerceTransactionWorksGet(Tracker t, MockGet g) { var hat = new EcommerceTransactionItem().SetSku("pbz0026").SetPrice(20).SetQuantity(1).Build(); var shirt = new EcommerceTransactionItem().SetSku("pbz0038").SetPrice(15).SetQuantity(1).SetName("shirt").SetCategory("clothing").Build(); var items = new List <EcommerceTransactionItem> { hat, shirt }; t.Track(new EcommerceTransaction() .SetOrderId("6a8078be") .SetTotalValue(35) .SetAffiliation("affiliation") .SetTaxValue(3) .SetShipping(0) .SetCity("Phoenix") .SetState("Arizona") .SetCountry("US") .SetCurrency("USD") .SetItems(items) .Build()); t.Flush(); // other events may exist - lets find our best guesses for each var transactionActual = (from item in g.Queries where item.Contains("Phoenix") select item).ToList()[0]; var hatActual = (from item in g.Queries where item.Contains("pbz0026") select item).ToList()[0]; var shirtActual = (from item in g.Queries where item.Contains("pbz0038") select item).ToList()[0]; var expectedTransaction = new Dictionary <string, string> { { "e", "tr" }, { "tr_id", "6a8078be" }, { "tr_tt", "35.00" }, { "tr_af", "affiliation" }, { "tr_tx", "3.00" }, { "tr_sh", "0.00" }, { "tr_ci", "Phoenix" }, { "tr_st", "Arizona" }, { "tr_co", "US" }, { "tr_cu", "USD" } }; assertQueryContainsDefaults(transactionActual); assertQueryContainsAll(transactionActual, expectedTransaction); var expectedHat = new Dictionary <string, string> { { "e", "ti" }, { "ti_id", "6a8078be" }, { "ti_sk", "pbz0026" }, { "ti_pr", "20.00" }, { "ti_qu", "1" }, { "ti_cu", "USD" } }; assertQueryContainsDefaults(hatActual); assertQueryContainsAll(hatActual, expectedHat); var expectedShirt = new Dictionary <string, string> { { "e", "ti" }, { "ti_id", "6a8078be" }, { "ti_sk", "pbz0038" }, { "ti_nm", "shirt" }, { "ti_ca", "clothing" }, { "ti_pr", "15.00" }, { "ti_qu", "1" }, { "ti_cu", "USD" }, }; assertQueryContainsDefaults(shirtActual); assertQueryContainsAll(shirtActual, expectedShirt); return(true); }