2424import re
2525import redis
2626import json
27+ import base64
2728from snowplow_tracker import tracker , _version , consumer , subject
2829from httmock import all_requests , HTTMock
2930
31+ try :
32+ from urllib .parse import unquote_plus # Python 3
33+
34+ except ImportError :
35+ from urllib import unquote_plus # Python 2
36+
3037querystrings = ["" ]
3138
3239default_consumer = consumer .Consumer ("localhost" , protocol = "http" , port = 80 )
@@ -121,18 +128,52 @@ def test_integration_struct_event(self):
121128 def test_integration_unstruct_event_non_base64 (self ):
122129 t = tracker .Tracker (default_consumer , default_subject , encode_base64 = False )
123130 with HTTMock (pass_response_content ):
124- t .track_unstruct_event ({"schema" : "com.acme/viewed_product/json/2-0-2" , "data" : {"product_id" : "ASO01043" , "price$flt" : 49.95 , "walrus$tms" : int ( time . time () * 1000 ) }})
131+ t .track_unstruct_event ({"schema" : "com.acme/viewed_product/json/2-0-2" , "data" : {"product_id" : "ASO01043" , "price$flt" : 49.95 , "walrus$tms" : 1000 }})
125132 expected_fields = {"e" : "ue" }
126133 for key in expected_fields :
127134 self .assertEquals (from_querystring (key , querystrings [- 1 ]), expected_fields [key ])
135+ envelope_string = from_querystring ("ue_pr" , querystrings [- 1 ])
136+ envelope = json .loads (unquote_plus (envelope_string ))
137+ self .assertEquals (envelope , {
138+ "schema" : "com.snowplowanalytics/unstruct_event/json/1-0-0" ,
139+ "data" : {"schema" : "com.acme/viewed_product/json/2-0-2" , "data" : {"product_id" : "ASO01043" , "price$flt" : 49.95 , "walrus$tms" : 1000 }}
140+ })
128141
129142 def test_integration_unstruct_event_base64 (self ):
130- t = tracker .Tracker (default_consumer , default_subject )
143+ t = tracker .Tracker (default_consumer , default_subject , encode_base64 = True )
131144 with HTTMock (pass_response_content ):
132- t .track_unstruct_event ({"schema" : "com.acme/viewed_product/json/2-0-2" , "data" : {"product_id" : "ASO01043" , "price$flt" : 49.95 , "walrus$tms" : int ( time . time () * 1000 ) }})
145+ t .track_unstruct_event ({"schema" : "com.acme/viewed_product/json/2-0-2" , "data" : {"product_id" : "ASO01043" , "price$flt" : 49.95 , "walrus$tms" : 1000 }})
133146 expected_fields = {"e" : "ue" }
134147 for key in expected_fields :
135148 self .assertEquals (from_querystring (key , querystrings [- 1 ]), expected_fields [key ])
149+ envelope_string = from_querystring ("ue_px" , querystrings [- 1 ])
150+ envelope = json .loads ((base64 .urlsafe_b64decode (envelope_string )).decode ("utf-8" ))
151+ self .assertEquals (envelope , {
152+ "schema" : "com.snowplowanalytics/unstruct_event/json/1-0-0" ,
153+ "data" : {"schema" : "com.acme/viewed_product/json/2-0-2" , "data" : {"product_id" : "ASO01043" , "price$flt" : 49.95 , "walrus$tms" : 1000 }}
154+ })
155+
156+ def test_integration_context_non_base64 (self ):
157+ t = tracker .Tracker (default_consumer , default_subject , encode_base64 = False )
158+ with HTTMock (pass_response_content ):
159+ t .track_page_view ("localhost" , "local host" , None , [{"schema" : "com.example/user/json/2-0-3" , "data" : {"user_type" : "tester" }}])
160+ envelope_string = from_querystring ("co" , querystrings [- 1 ])
161+ envelope = json .loads (unquote_plus (envelope_string ))
162+ self .assertEquals (envelope , {
163+ "schema" : "com.snowplowanalytics/contexts/json/1-0-0" ,
164+ "data" :[{"schema" : "com.example/user/json/2-0-3" , "data" : {"user_type" : "tester" }}]
165+ })
166+
167+ def test_integration_context_base64 (self ):
168+ t = tracker .Tracker (default_consumer , default_subject , encode_base64 = True )
169+ with HTTMock (pass_response_content ):
170+ t .track_page_view ("localhost" , "local host" , None , [{"schema" : "com.example/user/json/2-0-3" , "data" : {"user_type" : "tester" }}])
171+ envelope_string = from_querystring ("cx" , querystrings [- 1 ])
172+ envelope = json .loads ((base64 .urlsafe_b64decode (envelope_string )).decode ("utf-8" ))
173+ self .assertEquals (envelope , {
174+ "schema" : "com.snowplowanalytics/contexts/json/1-0-0" ,
175+ "data" :[{"schema" : "com.example/user/json/2-0-3" , "data" : {"user_type" : "tester" }}]
176+ })
136177
137178 def test_integration_standard_nv_pairs (self ):
138179 s = subject .Subject ()
@@ -146,7 +187,7 @@ def test_integration_standard_nv_pairs(self):
146187 t = tracker .Tracker (consumer .Consumer ("localhost" ), s , "cf" , app_id = "angry-birds-android" )
147188 with HTTMock (pass_response_content ):
148189 t .track_page_view ("localhost" , "local host" )
149- expected_fields = {"tna" : "cf" , "evn" : "com.snowplowanalytics" , " res" : "100x200" ,
190+ expected_fields = {"tna" : "cf" , "res" : "100x200" ,
150191 "lang" : "en" , "aid" : "angry-birds-android" , "cd" : "24" , "tz" : "Europe+London" ,
151192 "p" : "mob" , "tv" : "py-" + _version .__version__ }
152193 for key in expected_fields :
0 commit comments