This repository has been archived by the owner on Feb 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
client_track_test.rb
305 lines (260 loc) · 10.3 KB
/
client_track_test.rb
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
require 'helper'
class ClientTrackTest < MiniTest::Test
def setup
@stubs = Faraday::Adapter::Test::Stubs.new do |stub|
end
@heap = HeapAPI::Client.new
@heap.app_id = 'test-app-id'
@heap.faraday_adapter = :test
@heap.faraday_adapter_args = [@stubs]
end
def teardown
@stubs.verify_stubbed_calls
end
def test_track_without_app_id
@heap.app_id = nil
exception = assert_raises RuntimeError do
@heap.track 'test_track_without_app_id', 'test-identity'
end
assert_equal RuntimeError, exception.class
assert_equal 'Heap app_id not set', exception.message
end
def test_track_without_event_name
exception = assert_raises ArgumentError do
@heap.track '', 'test-identity'
end
assert_equal ArgumentError, exception.class
assert_equal 'Missing or empty event name', exception.message
end
def test_track_with_long_event_name
exception = assert_raises ArgumentError do
@heap.track 'A' * 1025, 'test-identity'
end
assert_equal ArgumentError, exception.class
assert_equal 'Event name too long', exception.message
end
def test_track_with_long_string_identity
long_identity = 'A' * 256
exception = assert_raises ArgumentError do
@heap.track 'some-event', long_identity
end
assert_equal ArgumentError, exception.class
assert_equal "Identity field too long; " +
'256 is above the 255-character limit', exception.message
end
def test_track_with_long_symbol_identity
long_identity = ('A' * 256).to_sym
exception = assert_raises ArgumentError do
@heap.track 'some-event', long_identity
end
assert_equal ArgumentError, exception.class
assert_equal "Identity field too long; " +
'256 is above the 255-character limit', exception.message
end
def test_track_with_array_identity
exception = assert_raises ArgumentError do
@heap.track 'test_track_with_array_property_value', []
end
assert_equal ArgumentError, exception.class
assert_equal 'Unsupported type for identity value []', exception.message
end
def test_track_with_invalid_property_object
exception = assert_raises ArgumentError do
@heap.track 'test_track_with_long_property_name', 'test-identity', false
end
assert_equal ArgumentError, exception.class
assert_equal 'Properties object does not implement #each',
exception.message
end
def test_track_with_long_property_name
long_name = 'A' * 1025
exception = assert_raises ArgumentError do
@heap.track 'test_track_with_long_property_name', 'test-identity',
long_name => 'value'
end
assert_equal ArgumentError, exception.class
assert_equal "Property name #{long_name} too long; " +
"1025 is above the 1024-character limit", exception.message
end
def test_track_with_long_string_property_value
long_value = 'A' * 1025
exception = assert_raises ArgumentError do
@heap.track 'test_track_with_long_string_property_value',
'test-identity', 'long_value_name' => long_value
end
assert_equal ArgumentError, exception.class
assert_equal "Property long_value_name value \"#{long_value}\" too " +
'long; 1025 is above the 1024-character limit', exception.message
end
def test_track_with_long_symbol_property_value
long_value = ('A' * 1025).to_sym
exception = assert_raises ArgumentError do
@heap.track 'test_track_with_long_symbol_property_value',
'test-identity', 'long_value_name' => long_value
end
assert_equal ArgumentError, exception.class
assert_equal "Property long_value_name value :#{long_value} too long; " +
'1025 is above the 1024-character limit', exception.message
end
def test_track_with_array_property_value
exception = assert_raises ArgumentError do
@heap.track 'test_track_with_array_property_value', 'test-identity',
'array_value_name' => []
end
assert_equal ArgumentError, exception.class
assert_equal 'Unsupported type for property array_value_name value []',
exception.message
end
def test_track_with_non_date_timestamp
exception = assert_raises ArgumentError do
@heap.track 'test_track_with_array_property_value', 'test-identity', {}, :timestamp => 'foobar'
end
assert_equal ArgumentError, exception.class
assert_equal 'Unsupported timestamp format foobar. Must be iso8601 or unix epoch milliseconds.',
exception.message
end
def test_track_with_array_timestamp
exception = assert_raises ArgumentError do
@heap.track 'test_track_with_array_property_value', 'test-identity', {}, :timestamp => []
end
assert_equal ArgumentError, exception.class
assert_equal 'Unsupported timestamp format []. Must be iso8601 or unix epoch milliseconds.',
exception.message
end
def test_track_with_array_idempotency_key
exception = assert_raises ArgumentError do
@heap.track 'test_track_with_array_property_value', 'test-identity', {}, :idempotency_key => []
end
assert_equal ArgumentError, exception.class
assert_equal 'Unsupported idempotency key format for []. Must be string or integer',
exception.message
end
def test_track
@stubs.post '/api/track' do |env|
golden_body = {
'app_id' => 'test-app-id',
'identity' => 'test-identity',
'event' => 'test_track',
}
assert_equal 'application/json', env[:request_headers]['Content-Type']
assert_equal @heap.user_agent, env[:request_headers]['User-Agent']
assert_equal golden_body, JSON.parse(env[:body])
[200, { 'Content-Type' => 'text/plain; encoding=utf8' }, '']
end
assert_equal @heap, @heap.track('test_track', 'test-identity')
end
def test_track_with_integer_identity
@stubs.post '/api/track' do |env|
golden_body = {
'app_id' => 'test-app-id',
'identity' => '123456789',
'event' => 'test_track',
}
assert_equal 'application/json', env[:request_headers]['Content-Type']
assert_equal @heap.user_agent, env[:request_headers]['User-Agent']
assert_equal golden_body, JSON.parse(env[:body])
[200, { 'Content-Type' => 'text/plain; encoding=utf8' }, '']
end
assert_equal @heap, @heap.track('test_track', 123456789)
end
def test_track_with_properties
@stubs.post '/api/track' do |env|
golden_body = {
'app_id' => 'test-app-id',
'identity' => 'test-identity',
'event' => 'test_track_with_properties',
'properties' => { 'foo' => 'bar', 'heap' => 'hurray' }
}
assert_equal 'application/json', env[:request_headers]['Content-Type']
assert_equal @heap.user_agent, env[:request_headers]['User-Agent']
assert_equal golden_body, JSON.parse(env[:body])
[200, { 'Content-Type' => 'text/plain; encoding=utf8' }, '']
end
assert_equal @heap, @heap.track('test_track_with_properties',
'test-identity','foo' => 'bar', :heap => :hurray)
end
def test_track_with_timestamp
@stubs.post '/api/track' do |env|
golden_body = {
'app_id' => 'test-app-id',
'identity' => 'test-identity',
'event' => 'test_track_with_timestamp',
'properties' => {},
'timestamp' => '1524038400000'
}
assert_equal 'application/json', env[:request_headers]['Content-Type']
assert_equal @heap.user_agent, env[:request_headers]['User-Agent']
assert_equal golden_body, JSON.parse(env[:body])
[200, { 'Content-Type' => 'text/plain; encoding=utf8' }, '']
end
assert_equal @heap, @heap.track('test_track_with_timestamp',
'test-identity', {}, :timestamp => Time.parse("2018-04-18 08:00:00 UTC"))
end
def test_track_with_iso8601_timestamp
timestamp = "2018-04-18T22:42:38+03:00"
@stubs.post '/api/track' do |env|
golden_body = {
'app_id' => 'test-app-id',
'identity' => 'test-identity',
'event' => 'test_track_with_iso8601_timestamp',
'properties' => {},
'timestamp' => timestamp
}
assert_equal 'application/json', env[:request_headers]['Content-Type']
assert_equal @heap.user_agent, env[:request_headers]['User-Agent']
assert_equal golden_body, JSON.parse(env[:body])
[200, { 'Content-Type' => 'text/plain; encoding=utf8' }, '']
end
assert_equal @heap, @heap.track('test_track_with_iso8601_timestamp',
'test-identity', {}, :timestamp => timestamp)
end
def test_track_with_idempotency_key
@stubs.post '/api/track' do |env|
golden_body = {
'app_id' => 'test-app-id',
'identity' => 'test-identity',
'event' => 'test_track_with_idempotency_key',
'properties' => {},
'idempotency_key' => 'foobar35214532512'
}
assert_equal 'application/json', env[:request_headers]['Content-Type']
assert_equal @heap.user_agent, env[:request_headers]['User-Agent']
assert_equal golden_body, JSON.parse(env[:body])
[200, { 'Content-Type' => 'text/plain; encoding=utf8' }, '']
end
assert_equal @heap, @heap.track('test_track_with_idempotency_key',
'test-identity', {}, :idempotency_key => 'foobar35214532512')
end
def test_track_error
@stubs.post '/api/track' do |env|
[400, { 'Content-Type' => 'text/plain; encoding=utf8' }, 'Bad request']
end
exception = assert_raises HeapAPI::Error do
@heap.track('test-identity', 'test_track')
end
assert_equal HeapAPI::ApiError, exception.class
assert_equal 'Heap API server error: 400 Bad request', exception.message
assert_kind_of Faraday::Response, exception.response
assert_equal 400, exception.response.status
end
def test_track_integration
@heap.app_id = '3000610572'
@heap.faraday_adapter = :net_http
@heap.faraday_adapter_args = []
assert_equal @heap, @heap.track('test_track_integration', 'test-identity',
'language' => 'ruby', 'project' => 'heap/heap-ruby')
end
def test_track_error_integration
@heap.faraday_adapter = :net_http
@heap.faraday_adapter_args = []
assert_raises HeapAPI::Error do
@heap.track('test_track_integration', 'test-identity',
'language' => 'ruby', 'project' => 'heap/heap-ruby')
end
end
def test_track_with_stubbed_connection
@heap.stubbed = true
assert_equal @heap, @heap.add_user_properties('test-identity',
'foo' => 'bar', :heap => :hurray)
end
end