From 1b5b84aa609fa79b6fc2cb6dda1463384fe0e264 Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Tue, 23 Jul 2024 12:38:39 -0400 Subject: [PATCH] Fix unit tests --- spec/http_util.rb | 11 ++++++----- spec/impl/event_sender_spec.rb | 34 ++++++++++++++++++---------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/spec/http_util.rb b/spec/http_util.rb index aba5e9d8..a52e4bb6 100644 --- a/spec/http_util.rb +++ b/spec/http_util.rb @@ -9,7 +9,7 @@ class StubHTTPServer @@next_port = 50000 - def initialize(enable_compression = false) + def initialize(enable_compression: false) @port = StubHTTPServer.next_port @enable_compression = enable_compression begin @@ -78,9 +78,10 @@ def record_request(req, res) def await_request_with_body r = @requests_queue.pop - request, body = r[0], r[1] + request = r[0] + body = r[1] - return [request, body] unless @enable_compression + return [request, body.to_s] unless @enable_compression gz = Zlib::GzipReader.new(StringIO.new(body.to_s)) @@ -94,8 +95,8 @@ def method_missing(*) end end -def with_server(enable_compression = false) - server = StubHTTPServer.new(enable_compression) +def with_server(enable_compression: false) + server = StubHTTPServer.new(enable_compression: enable_compression) begin server.start yield server diff --git a/spec/impl/event_sender_spec.rb b/spec/impl/event_sender_spec.rb index 9383186d..e6a971bd 100644 --- a/spec/impl/event_sender_spec.rb +++ b/spec/impl/event_sender_spec.rb @@ -11,21 +11,23 @@ module Impl subject { EventSender } let(:sdk_key) { "sdk_key" } - let(:fake_data) { '{"things":[]}' } + let(:fake_data) { '{"things":[],"stuff":false,"other examples":["you", "me", "us", "we"]}' } - def make_sender_with_config(config = Config.new(logger: $null_log)) - subject.new(sdk_key, config, nil, 0.1) + def make_sender(config_options = {}) + config_options = {logger: $null_log}.merge(config_options) + subject.new(sdk_key, Config.new(config_options), nil, 0.1) end - def with_sender_and_server(enable_compression = false) - with_server(enable_compression) do |server| - config = Config.new(events_uri: server.base_uri.to_s, logger: $null_log) - yield make_sender_with_config(config), server + def with_sender_and_server(config_options = {}) + enable_compression = config_options[:compress_events] || false + with_server(enable_compression: enable_compression) do |server| + config_options[:events_uri] = server.base_uri.to_s + yield make_sender(config_options), server end end - it "sends analytic data with gzip compression" do - with_sender_and_server(true) do |es, server| + it "sends analytics event data without compression enabled" do + with_sender_and_server(compress_events: false) do |es, server| server.setup_ok_response("/bulk", "") result = es.send_event_data(fake_data, "", false) @@ -41,15 +43,16 @@ def with_sender_and_server(enable_compression = false) "content-type" => [ "application/json" ], "user-agent" => [ "RubyClient/" + LaunchDarkly::VERSION ], "x-launchdarkly-event-schema" => [ "4" ], - "x-launchdarkly-tags" => [ "application-id/id application-version/version" ], "connection" => [ "Keep-Alive" ], }) expect(req.header['x-launchdarkly-payload-id']).not_to eq [] + expect(req.header['content-encoding']).to eq [] + expect(req.header['content-length'][0].to_i).to eq fake_data.length end end - it "sends analytics event data" do - with_sender_and_server do |es, server| + it "sends analytics event data with compression enabled" do + with_sender_and_server(compress_events: true) do |es, server| server.setup_ok_response("/bulk", "") result = es.send_event_data(fake_data, "", false) @@ -62,13 +65,14 @@ def with_sender_and_server(enable_compression = false) expect(body).to eq fake_data expect(req.header).to include({ "authorization" => [ sdk_key ], + "content-encoding" => [ "gzip" ], "content-type" => [ "application/json" ], "user-agent" => [ "RubyClient/" + LaunchDarkly::VERSION ], "x-launchdarkly-event-schema" => [ "4" ], - "x-launchdarkly-tags" => [ "application-id/id application-version/version" ], "connection" => [ "Keep-Alive" ], }) expect(req.header['x-launchdarkly-payload-id']).not_to eq [] + expect(req.header['content-length'][0].to_i).to be > fake_data.length end end @@ -144,9 +148,7 @@ def with_sender_and_server(enable_compression = false) begin ENV["http_proxy"] = proxy.base_uri.to_s - config = Config.default - config.events_uri = fake_target_uri - es = make_sender_with_config(config) + es = make_sender(events_uri: fake_target_uri) result = es.send_event_data(fake_data, "", false)