appsignal 1.4.0.alpha.2 → 1.4.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +3 -1
  3. data/.travis.yml +3 -1
  4. data/CHANGELOG.md +38 -1
  5. data/Rakefile +29 -12
  6. data/benchmark.rake +3 -7
  7. data/ext/agent.yml +11 -11
  8. data/ext/appsignal_extension.c +364 -72
  9. data/ext/extconf.rb +2 -4
  10. data/gemfiles/resque.gemfile +1 -0
  11. data/lib/appsignal.rb +40 -30
  12. data/lib/appsignal/auth_check.rb +1 -1
  13. data/lib/appsignal/cli/diagnose.rb +4 -3
  14. data/lib/appsignal/cli/install.rb +16 -15
  15. data/lib/appsignal/config.rb +31 -31
  16. data/lib/appsignal/event_formatter.rb +1 -1
  17. data/lib/appsignal/extension.rb +6 -0
  18. data/lib/appsignal/garbage_collection_profiler.rb +47 -0
  19. data/lib/appsignal/hooks.rb +1 -0
  20. data/lib/appsignal/hooks/active_support_notifications.rb +43 -0
  21. data/lib/appsignal/integrations/capistrano/appsignal.cap +1 -1
  22. data/lib/appsignal/integrations/capistrano/capistrano_2_tasks.rb +2 -2
  23. data/lib/appsignal/integrations/mongo_ruby_driver.rb +1 -1
  24. data/lib/appsignal/integrations/object.rb +4 -4
  25. data/lib/appsignal/integrations/padrino.rb +1 -1
  26. data/lib/appsignal/integrations/sinatra.rb +1 -1
  27. data/lib/appsignal/integrations/webmachine.rb +2 -2
  28. data/lib/appsignal/js_exception_transaction.rb +7 -10
  29. data/lib/appsignal/marker.rb +3 -2
  30. data/lib/appsignal/rack/generic_instrumentation.rb +1 -1
  31. data/lib/appsignal/rack/sinatra_instrumentation.rb +13 -6
  32. data/lib/appsignal/rack/streaming_listener.rb +5 -3
  33. data/lib/appsignal/system.rb +36 -0
  34. data/lib/appsignal/transaction.rb +20 -20
  35. data/lib/appsignal/transmitter.rb +11 -7
  36. data/lib/appsignal/utils.rb +76 -2
  37. data/lib/appsignal/version.rb +1 -1
  38. data/spec/lib/appsignal/auth_check_spec.rb +0 -2
  39. data/spec/lib/appsignal/capistrano2_spec.rb +99 -79
  40. data/spec/lib/appsignal/capistrano3_spec.rb +57 -78
  41. data/spec/lib/appsignal/cli/diagnose_spec.rb +17 -15
  42. data/spec/lib/appsignal/cli/install_spec.rb +38 -20
  43. data/spec/lib/appsignal/cli/notify_of_deploy_spec.rb +2 -5
  44. data/spec/lib/appsignal/cli_spec.rb +2 -5
  45. data/spec/lib/appsignal/config_spec.rb +385 -158
  46. data/spec/lib/appsignal/event_formatter/action_view/render_formatter_spec.rb +1 -3
  47. data/spec/lib/appsignal/event_formatter/active_record/instantiation_formatter_spec.rb +0 -2
  48. data/spec/lib/appsignal/event_formatter/active_record/sql_formatter_spec.rb +0 -2
  49. data/spec/lib/appsignal/event_formatter/elastic_search/search_formatter_spec.rb +0 -2
  50. data/spec/lib/appsignal/event_formatter/faraday/request_formatter_spec.rb +0 -2
  51. data/spec/lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter_spec.rb +0 -2
  52. data/spec/lib/appsignal/event_formatter/moped/query_formatter_spec.rb +0 -2
  53. data/spec/lib/appsignal/event_formatter_spec.rb +0 -2
  54. data/spec/lib/appsignal/extension_spec.rb +7 -8
  55. data/spec/lib/appsignal/garbage_collection_profiler_spec.rb +71 -0
  56. data/spec/lib/appsignal/hooks/active_support_notifications_spec.rb +42 -0
  57. data/spec/lib/appsignal/hooks/celluloid_spec.rb +0 -2
  58. data/spec/lib/appsignal/hooks/data_mapper_spec.rb +0 -2
  59. data/spec/lib/appsignal/hooks/delayed_job_spec.rb +0 -2
  60. data/spec/lib/appsignal/hooks/mongo_ruby_driver_spec.rb +0 -2
  61. data/spec/lib/appsignal/hooks/net_http_spec.rb +0 -2
  62. data/spec/lib/appsignal/hooks/passenger_spec.rb +0 -2
  63. data/spec/lib/appsignal/hooks/puma_spec.rb +0 -2
  64. data/spec/lib/appsignal/hooks/rake_spec.rb +1 -2
  65. data/spec/lib/appsignal/hooks/redis_spec.rb +0 -2
  66. data/spec/lib/appsignal/hooks/sequel_spec.rb +19 -21
  67. data/spec/lib/appsignal/hooks/shoryuken_spec.rb +1 -4
  68. data/spec/lib/appsignal/hooks/sidekiq_spec.rb +2 -3
  69. data/spec/lib/appsignal/hooks/unicorn_spec.rb +0 -2
  70. data/spec/lib/appsignal/hooks/webmachine_spec.rb +4 -11
  71. data/spec/lib/appsignal/hooks_spec.rb +0 -2
  72. data/spec/lib/appsignal/integrations/data_mapper_spec.rb +0 -1
  73. data/spec/lib/appsignal/integrations/grape_spec.rb +1 -3
  74. data/spec/lib/appsignal/integrations/mongo_ruby_driver_spec.rb +1 -2
  75. data/spec/lib/appsignal/integrations/object_spec.rb +32 -3
  76. data/spec/lib/appsignal/integrations/padrino_spec.rb +4 -11
  77. data/spec/lib/appsignal/integrations/railtie_spec.rb +1 -3
  78. data/spec/lib/appsignal/integrations/resque_active_job_spec.rb +1 -3
  79. data/spec/lib/appsignal/integrations/resque_spec.rb +2 -4
  80. data/spec/lib/appsignal/integrations/sinatra_spec.rb +33 -8
  81. data/spec/lib/appsignal/integrations/webmachine_spec.rb +6 -15
  82. data/spec/lib/appsignal/js_exception_transaction_spec.rb +3 -5
  83. data/spec/lib/appsignal/marker_spec.rb +35 -48
  84. data/spec/lib/appsignal/minutely_spec.rb +0 -2
  85. data/spec/lib/appsignal/rack/generic_instrumentation_spec.rb +0 -2
  86. data/spec/lib/appsignal/rack/js_exception_catcher_spec.rb +0 -2
  87. data/spec/lib/appsignal/rack/rails_instrumentation_spec.rb +3 -5
  88. data/spec/lib/appsignal/rack/sinatra_instrumentation_spec.rb +47 -11
  89. data/spec/lib/appsignal/rack/streaming_listener_spec.rb +6 -7
  90. data/spec/lib/appsignal/system/container_spec.rb +67 -0
  91. data/spec/lib/appsignal/system_spec.rb +49 -0
  92. data/spec/lib/appsignal/transaction_spec.rb +30 -13
  93. data/spec/lib/appsignal/transmitter_spec.rb +53 -20
  94. data/spec/lib/appsignal/utils/gzip_spec.rb +10 -0
  95. data/spec/lib/appsignal/utils/params_sanitizer_spec.rb +0 -2
  96. data/spec/lib/appsignal/utils/query_params_sanitizer_spec.rb +0 -2
  97. data/spec/lib/appsignal/utils_spec.rb +59 -3
  98. data/spec/lib/appsignal_spec.rb +132 -58
  99. data/spec/spec_helper.rb +24 -116
  100. data/spec/support/fixtures/containers/cgroups/docker +14 -0
  101. data/spec/support/fixtures/containers/cgroups/docker_systemd +8 -0
  102. data/spec/support/fixtures/containers/cgroups/lxc +10 -0
  103. data/spec/support/fixtures/containers/cgroups/no_permission +0 -0
  104. data/spec/support/fixtures/containers/cgroups/none +1 -0
  105. data/spec/support/helpers/api_request_helper.rb +22 -0
  106. data/spec/support/helpers/dependency_helper.rb +61 -0
  107. data/spec/support/helpers/directory_helper.rb +27 -0
  108. data/spec/support/helpers/std_streams_helper.rb +35 -0
  109. data/spec/support/helpers/system_helpers.rb +24 -0
  110. data/spec/support/helpers/transaction_helpers.rb +7 -64
  111. data/spec/support/helpers/very_specific_error.rb +8 -0
  112. data/spec/support/mocks/fake_gc_profiler.rb +19 -0
  113. data/spec/support/project_fixture/config/appsignal.yml +10 -1
  114. metadata +60 -35
  115. data/circle.yml +0 -12
  116. data/lib/appsignal/subscriber.rb +0 -55
  117. data/lib/appsignal/update_active_support.rb +0 -20
  118. data/lib/vendor/active_support/notifications.rb +0 -212
  119. data/lib/vendor/active_support/notifications/fanout.rb +0 -157
  120. data/lib/vendor/active_support/notifications/instrumenter.rb +0 -73
  121. data/lib/vendor/active_support/per_thread_registry.rb +0 -53
  122. data/spec/lib/appsignal/subscriber_spec.rb +0 -160
  123. data/spec/lib/appsignal/update_active_support_spec.rb +0 -17
  124. data/spec/support/helpers/notification_helpers.rb +0 -14
@@ -0,0 +1,67 @@
1
+ describe Appsignal::System::Container do
2
+ describe ".id" do
3
+ subject { described_class.id }
4
+
5
+ context "when in docker" do
6
+ context "when running normally" do
7
+ around { |example| recognize_as_container(:docker) { example.run } }
8
+
9
+ it "returns id" do
10
+ expect(subject).to eq("0c703b75cdeaad7c933aa68b4678cc5c37a12d5ef5d7cb52c9cefe684d98e575")
11
+ end
12
+ end
13
+
14
+ context "when running with systemd" do
15
+ around do |example|
16
+ # Fabricated example. I'm unable to set up an environment that
17
+ # produces such a cgroups file.
18
+ recognize_as_container(:docker_systemd) { example.run }
19
+ end
20
+
21
+ it "returns id" do
22
+ expect(subject).to eq("09f1c4d420025670a3633edbc9b31450f1d6b2ff87b5912a10c320ad398c7215")
23
+ end
24
+ end
25
+ end
26
+
27
+ context "when in LXC" do
28
+ around { |example| recognize_as_container(:lxc) { example.run } }
29
+
30
+ it "returns id" do
31
+ expect(subject).to eq("1a2e485e-3947-4bb6-8c24-8774f0859648")
32
+ end
33
+ end
34
+
35
+ context "when not in container" do
36
+ around { |example| recognize_as_container(:none) { example.run } }
37
+
38
+ it "returns nil" do
39
+ expect(subject).to be_nil
40
+ end
41
+ end
42
+
43
+ context "when no permission to read cgroup file" do
44
+ let(:out_stream) { StringIO.new }
45
+ let(:no_permission_file) do
46
+ File.join(fixtures_dir, 'containers', 'cgroups', 'no_permission')
47
+ end
48
+ before do
49
+ File.chmod 0333, no_permission_file
50
+ Appsignal.logger = Logger.new(out_stream)
51
+ end
52
+ around { |example| recognize_as_container(:no_permission) { example.run } }
53
+ after { File.chmod 0644, no_permission_file }
54
+
55
+ it "returns nil" do
56
+ expect(subject).to be_nil
57
+ end
58
+
59
+ it "logs the error" do
60
+ subject
61
+ expect(out_stream.string).to include \
62
+ "Unable to read '#{no_permission_file}' to determine cgroup",
63
+ "Permission denied"
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,49 @@
1
+ describe Appsignal::System do
2
+ describe ".container?" do
3
+ subject { described_class.container? }
4
+
5
+ context "when on Heroku" do
6
+ around { |example| recognize_as_heroku { example.run } }
7
+
8
+ it "returns true" do
9
+ expect(subject).to be_true
10
+ end
11
+ end
12
+
13
+ context "when in docker" do
14
+ around { |example| recognize_as_container(:docker) { example.run } }
15
+
16
+ it "returns true" do
17
+ expect(subject).to be_true
18
+ end
19
+ end
20
+
21
+ context "when not in container" do
22
+ around { |example| recognize_as_container(:none) { example.run } }
23
+
24
+ it "returns false" do
25
+ expect(subject).to be_false
26
+ end
27
+ end
28
+ end
29
+
30
+ describe ".heroku?" do
31
+ subject { described_class.heroku? }
32
+
33
+ context "when on Heroku" do
34
+ around { |example| recognize_as_heroku { example.run } }
35
+
36
+ it "returns true" do
37
+ expect(subject).to be_true
38
+ end
39
+ end
40
+
41
+ context "when not on Heroku" do
42
+ around { |example| recognize_as_container(:none) { example.run } }
43
+
44
+ it "returns false" do
45
+ expect(subject).to be_false
46
+ end
47
+ end
48
+ end
49
+ end
@@ -1,4 +1,4 @@
1
- require 'spec_helper'
1
+ require_relative '../../support/mocks/fake_gc_profiler'
2
2
 
3
3
  class Smash < Hash
4
4
  def []=(key, val)
@@ -25,7 +25,7 @@ describe Appsignal::Transaction do
25
25
  context "class methods" do
26
26
  describe ".create" do
27
27
  it "should add the transaction to thread local" do
28
- Appsignal::Extension.should_receive(:start_transaction).with('1', 'http_request')
28
+ Appsignal::Extension.should_receive(:start_transaction).with('1', 'http_request', 0)
29
29
 
30
30
  created_transaction = Appsignal::Transaction.create('1', namespace, request, options)
31
31
 
@@ -122,10 +122,11 @@ describe Appsignal::Transaction do
122
122
 
123
123
  context "if a transaction is discarded" do
124
124
  it "should not complete the transaction" do
125
- Appsignal::Transaction.current.should_not_receive(:complete)
125
+ expect(Appsignal::Transaction.current.ext).to_not receive(:complete)
126
126
 
127
127
  Appsignal::Transaction.current.discard!
128
128
  expect(Appsignal::Transaction.current.discarded?).to be_true
129
+
129
130
  Appsignal::Transaction.complete_current!
130
131
 
131
132
  Thread.current[:appsignal_transaction].should be_nil
@@ -351,10 +352,10 @@ describe Appsignal::Transaction do
351
352
  end
352
353
 
353
354
  describe "set_sample_data" do
354
- it "should generate json and set the data" do
355
+ it "should set the data" do
355
356
  transaction.ext.should_receive(:set_sample_data).with(
356
357
  'params',
357
- '{"controller":"blog_posts","action":"show","id":"1"}'
358
+ Appsignal::Utils.data_generate({"controller" => "blog_posts", "action" => "show", "id" => "1"})
358
359
  ).once
359
360
 
360
361
  transaction.set_sample_data(
@@ -381,23 +382,29 @@ describe Appsignal::Transaction do
381
382
  it "should sample data" do
382
383
  transaction.ext.should_receive(:set_sample_data).with(
383
384
  'environment',
384
- "{\"CONTENT_LENGTH\":\"0\",\"REQUEST_METHOD\":\"GET\",\"SERVER_NAME\":\"example.org\",\"SERVER_PORT\":\"80\",\"PATH_INFO\":\"/blog\"}"
385
+ Appsignal::Utils.data_generate({
386
+ "CONTENT_LENGTH" => "0",
387
+ "REQUEST_METHOD" => "GET",
388
+ "SERVER_NAME" => "example.org",
389
+ "SERVER_PORT" => "80",
390
+ "PATH_INFO" => "/blog"
391
+ })
385
392
  ).once
386
393
  transaction.ext.should_receive(:set_sample_data).with(
387
394
  'session_data',
388
- "{}"
395
+ Appsignal::Utils.data_generate({})
389
396
  ).once
390
397
  transaction.ext.should_receive(:set_sample_data).with(
391
398
  'params',
392
- '{"controller":"blog_posts","action":"show","id":"1"}'
399
+ Appsignal::Utils.data_generate({"controller" => "blog_posts", "action" => "show", "id" => "1"})
393
400
  ).once
394
401
  transaction.ext.should_receive(:set_sample_data).with(
395
402
  'metadata',
396
- '{"key":"value"}'
403
+ Appsignal::Utils.data_generate({"key" => "value"})
397
404
  ).once
398
405
  transaction.ext.should_receive(:set_sample_data).with(
399
406
  'tags',
400
- "{}"
407
+ Appsignal::Utils.data_generate({})
401
408
  ).once
402
409
 
403
410
  transaction.sample_data
@@ -431,7 +438,7 @@ describe Appsignal::Transaction do
431
438
  transaction.ext.should_receive(:set_error).with(
432
439
  'RSpec::Mocks::Mock',
433
440
  'test message',
434
- "[\"line 1\"]"
441
+ Appsignal::Utils.data_generate(['line 1'])
435
442
  )
436
443
 
437
444
  transaction.set_error(error)
@@ -449,7 +456,7 @@ describe Appsignal::Transaction do
449
456
  transaction.ext.should_receive(:set_error).with(
450
457
  'RSpec::Mocks::Mock',
451
458
  '',
452
- "[\"line 1\"]"
459
+ Appsignal::Utils.data_generate(['line 1'])
453
460
  )
454
461
 
455
462
  transaction.set_error(error)
@@ -471,7 +478,8 @@ describe Appsignal::Transaction do
471
478
  'name',
472
479
  'title',
473
480
  'body',
474
- 1
481
+ 1,
482
+ 0
475
483
  )
476
484
 
477
485
  transaction.finish_event(
@@ -487,6 +495,7 @@ describe Appsignal::Transaction do
487
495
  'name',
488
496
  '',
489
497
  '',
498
+ 0,
490
499
  0
491
500
  )
492
501
 
@@ -497,6 +506,14 @@ describe Appsignal::Transaction do
497
506
  nil
498
507
  )
499
508
  end
509
+
510
+ it "should add garbage collection time" do
511
+ allow_any_instance_of(Appsignal::GarbageCollectionProfiler)
512
+ .to receive(:internal_profiler)
513
+ .and_return(FakeGCProfiler.new(0.12345))
514
+
515
+ transaction.finish_event('name', nil, nil, nil)
516
+ end
500
517
  end
501
518
 
502
519
  describe "#record_event" do
@@ -1,13 +1,14 @@
1
- require 'spec_helper'
2
-
3
1
  describe Appsignal::Transmitter do
4
2
  let(:config) { project_fixture_config }
5
3
  let(:action) { 'action' }
4
+ let(:log) { StringIO.new }
6
5
  let(:instance) { Appsignal::Transmitter.new(action, config) }
6
+ before do
7
+ config.config_hash[:hostname] = 'app1.local'
8
+ config.logger = Logger.new(log)
9
+ end
7
10
 
8
11
  describe "#uri" do
9
- before { ENV['APPSIGNAL_HOSTNAME'] = 'app1.local' }
10
-
11
12
  subject { instance.uri.to_s }
12
13
 
13
14
  it { should include 'https://push.appsignal.com/1/action?' }
@@ -22,9 +23,11 @@ describe Appsignal::Transmitter do
22
23
  before do
23
24
  stub_request(
24
25
  :post,
25
- "https://push.appsignal.com/1/action?api_key=abc&environment=production&gem_version=#{Appsignal::VERSION}&hostname=#{Socket.gethostname}&name=TestApp"
26
+ "https://push.appsignal.com/1/action?api_key=abc"\
27
+ "&environment=production&gem_version=#{Appsignal::VERSION}"\
28
+ "&hostname=#{config.config_hash[:hostname]}&name=TestApp"
26
29
  ).with(
27
- :body => Zlib::Deflate.deflate("{\"the\":\"payload\"}", Zlib::BEST_SPEED),
30
+ :body => Appsignal::Utils::Gzip.compress("{\"the\":\"payload\"}"),
28
31
  :headers => {
29
32
  'Content-Encoding' => 'gzip',
30
33
  'Content-Type' => 'application/json; charset=UTF-8',
@@ -33,20 +36,57 @@ describe Appsignal::Transmitter do
33
36
  :status => 200
34
37
  )
35
38
  end
36
-
37
39
  subject { instance.transmit(:the => :payload) }
38
40
 
39
41
  it { should eq '200' }
40
- end
41
42
 
42
- describe "#http_post" do
43
- before do
44
- Socket.stub(:gethostname => 'app1.local')
43
+ context "with ca_file_path config option set" do
44
+ context "when not existing file" do
45
+ before do
46
+ config.config_hash[:ca_file_path] = File.join(resources_dir, "cacert.pem")
47
+ end
48
+
49
+ it "ignores the config and logs a warning" do
50
+ expect(subject).to eq '200'
51
+ expect(log.string).to_not include "Ignoring non-existing or unreadable " \
52
+ "`ca_file_path`: #{config[:ca_file_path]}"
53
+ end
54
+ end
55
+
56
+ context "when not existing file" do
57
+ before do
58
+ config.config_hash[:ca_file_path] = File.join(tmp_dir, "ca_file_that_does_not_exist")
59
+ end
60
+
61
+ it "ignores the config and logs a warning" do
62
+ expect(subject).to eq '200'
63
+ expect(log.string).to include "Ignoring non-existing or unreadable " \
64
+ "`ca_file_path`: #{config[:ca_file_path]}"
65
+ end
66
+ end
67
+
68
+ context "when not readable file" do
69
+ let(:file) { File.join(tmp_dir, "ca_file") }
70
+ before do
71
+ config.config_hash[:ca_file_path] = file
72
+ File.open(file, "w") { |f| f.chmod 0000 }
73
+ end
74
+
75
+ it "ignores the config and logs a warning" do
76
+ expect(subject).to eq '200'
77
+ expect(log.string).to include "Ignoring non-existing or unreadable " \
78
+ "`ca_file_path`: #{config[:ca_file_path]}"
79
+ end
80
+
81
+ after { File.delete file }
82
+ end
45
83
  end
84
+ end
46
85
 
86
+ describe "#http_post" do
47
87
  subject { instance.send(:http_post, 'the' => 'payload') }
48
88
 
49
- its(:body) { should eq Zlib::Deflate.deflate("{\"the\":\"payload\"}", Zlib::BEST_SPEED) }
89
+ its(:body) { should eq Appsignal::Utils::Gzip.compress("{\"the\":\"payload\"}") }
50
90
  its(:path) { should eq instance.uri.request_uri }
51
91
 
52
92
  it "should have the correct headers" do
@@ -55,13 +95,6 @@ describe Appsignal::Transmitter do
55
95
  end
56
96
  end
57
97
 
58
- describe ".CA_FILE_PATH" do
59
- subject { Appsignal::Transmitter::CA_FILE_PATH }
60
-
61
- it { should include('resources/cacert.pem') }
62
- it("should exist") { File.exist?(subject).should be_true }
63
- end
64
-
65
98
  describe "#http_client" do
66
99
  subject { instance.send(:http_client) }
67
100
 
@@ -80,7 +113,7 @@ describe Appsignal::Transmitter do
80
113
  its(:proxy?) { should be_false }
81
114
  its(:use_ssl?) { should be_true }
82
115
  its(:verify_mode) { should eq OpenSSL::SSL::VERIFY_PEER }
83
- its(:ca_file) { Appsignal::Transmitter::CA_FILE_PATH }
116
+ its(:ca_file) { should eq config[:ca_file_path] }
84
117
  end
85
118
 
86
119
  context "with a proxy" do
@@ -0,0 +1,10 @@
1
+ describe Appsignal::Utils::Gzip do
2
+ describe ".compress" do
3
+ let(:value) { "foo" }
4
+ subject { described_class.compress(value).force_encoding("UTF-8") }
5
+
6
+ it "returns a gziped value" do
7
+ expect(subject).to eq("x\u0001K\xCB\xCF\a\u0000\u0002\x82\u0001E")
8
+ end
9
+ end
10
+ end
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  describe Appsignal::Utils::ParamsSanitizer do
4
2
  let(:file) { uploaded_file }
5
3
  let(:params) do
@@ -1,5 +1,3 @@
1
- require 'spec_helper'
2
-
3
1
  describe Appsignal::Utils::QueryParamsSanitizer do
4
2
  describe ".sanitize" do
5
3
  context "when only_top_level = true" do
@@ -1,8 +1,64 @@
1
1
  # encoding: UTF-8
2
2
 
3
- require 'spec_helper'
4
-
5
3
  describe Appsignal::Utils do
4
+ describe ".data_generate" do
5
+ subject { Appsignal::Utils.data_generate(body) }
6
+
7
+ context "with a valid hash body" do
8
+ let(:body) do
9
+ {
10
+ 'the' => 'payload',
11
+ 'int' => 1,
12
+ 'float' => 1.0,
13
+ 1 => true,
14
+ nil => 'test',
15
+ :foo => [1, 2, 'three', {'foo' => 'bar'}],
16
+ 'bar' => nil,
17
+ 'baz' => {'foo' => 'bʊr', 'arr' => [1, 2]}
18
+ }
19
+ end
20
+
21
+ it { should eq Appsignal::Utils.data_generate(body) }
22
+ it { should_not eq Appsignal::Utils.data_generate({}) }
23
+ it { should_not eq 'a string' }
24
+ its(:to_s) { should eq %({"":"test","1":true,"bar":null,"baz":{"arr":[1,2],"foo":"bʊr"},"float":1.0,"foo":[1,2,"three",{"foo":"bar"}],"int":1,"the":"payload"}) }
25
+ end
26
+
27
+ context "with a valid array body" do
28
+ let(:body) do
29
+ [1, 'string', 10, {'foo' => 'bʊr'}]
30
+ end
31
+
32
+ its(:to_s) { should eq %([1,\"string\",10,{\"foo\":\"bʊr\"}]) }
33
+ end
34
+
35
+ context "with a body that contains strings with invalid utf-8 content" do
36
+ let(:string_with_invalid_utf8) { [0x61, 0x61, 0x85].pack('c*') }
37
+ let(:body) { {
38
+ 'field_one' => [0x61, 0x61].pack('c*'),
39
+ :field_two => string_with_invalid_utf8,
40
+ 'field_three' => [
41
+ 'one', string_with_invalid_utf8
42
+ ],
43
+ 'field_four' => {
44
+ 'one' => string_with_invalid_utf8
45
+ }
46
+ } }
47
+
48
+ its(:to_s) { should eq %({"field_four":{"one":"aa�"},"field_one":"aa","field_three":["one","aa�"],"field_two":"aa�"}) }
49
+ end
50
+
51
+ context "with an invalid body" do
52
+ let(:body) { 'body' }
53
+
54
+ it "should raise a type error" do
55
+ expect {
56
+ subject
57
+ }.to raise_error TypeError
58
+ end
59
+ end
60
+ end
61
+
6
62
  describe ".json_generate" do
7
63
  subject { Appsignal::Utils.json_generate(body) }
8
64
 
@@ -14,7 +70,7 @@ describe Appsignal::Utils do
14
70
  nil => 'test',
15
71
  :foo => [1, 2, 'three'],
16
72
  'bar' => nil,
17
- 'baz' => { 'foo' => 'bar' }
73
+ 'baz' => {'foo' => 'bar'}
18
74
  }
19
75
  end
20
76