Skip to content

Commit

Permalink
add Excon adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
mrlewap committed Dec 13, 2017
1 parent 675f17f commit 8edae14
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/sniffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ def logger
require_relative "sniffer/adapters/curb_adapter"
require_relative "sniffer/adapters/ethon_adapter"
require_relative "sniffer/adapters/eventmachine_adapter"
require_relative "sniffer/adapters/excon_adapter"
3 changes: 2 additions & 1 deletion lib/sniffer/adapters/curb_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ def self.included(base)
end
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def http_with_sniffer(verb)
sniffer_request(verb)

Expand Down Expand Up @@ -47,6 +46,7 @@ def data_item
@data_item ||= Sniffer::DataItem.new if Sniffer.enabled?
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def sniffer_request(verb, *args)
return unless data_item

Expand All @@ -64,6 +64,7 @@ def sniffer_request(verb, *args)
Sniffer.store(data_item)
end

# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
def sniffer_response(timing)
return unless data_item

Expand Down
4 changes: 1 addition & 3 deletions lib/sniffer/adapters/ethon_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ def perform_with_sniffer

end

if Ethon.logger.debug?
Ethon.logger.debug { "ETHON: performed #{log_inspect}" }
end
Ethon.logger.debug { "ETHON: performed #{log_inspect}" } if Ethon.logger.debug?
complete

@return_code
Expand Down
4 changes: 1 addition & 3 deletions lib/sniffer/adapters/eventmachine_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,4 @@ def on_body_data_with_sniffer(data)
end
end

if defined?(::EventMachine)
EventMachine::HttpClient.send(:include, Sniffer::Adapters::EventMachineAdapter::Client)
end
EventMachine::HttpClient.send(:include, Sniffer::Adapters::EventMachineAdapter::Client) if defined?(::EventMachine)
50 changes: 50 additions & 0 deletions lib/sniffer/adapters/excon_adapter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module Sniffer
module Adapters
# Excon adapter
module ExconAdapter
def self.included(base)
base.class_eval do
alias_method :request_without_sniffer, :request_call
alias_method :request_call, :request_with_sniffer
end
end

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength
def request_with_sniffer(datum)
if Sniffer.enabled?
@data_item = Sniffer::DataItem.new
query = datum[:path] + query_string(datum)
@data_item.request = Sniffer::DataItem::Request.new(host: datum[:host],
method: datum[:method].to_sym,
query: query,
headers: datum[:headers],
body: datum[:body].to_s,
port: datum[:port])
Sniffer.store(@data_item)
end

bm = Benchmark.realtime do
@res = request_without_sniffer(datum)
end

if Sniffer.enabled?
response_for_sniffer = Excon::Response.new(response(@res)[:response])
@data_item.response = Sniffer::DataItem::Response.new(status: response_for_sniffer.status,
headers: response_for_sniffer.headers.collect.to_h,
body: response_for_sniffer
.body.force_encoding(Encoding::UTF_8),
timing: bm)

@data_item.log
end

@res
end
# rubocop:enable Metrics/AbcSize, Metrics/MethodLength
end
end
end

Excon::Connection.send(:include, Sniffer::Adapters::ExconAdapter) if defined?(::Excon::Connection)
1 change: 1 addition & 0 deletions sniffer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "ethon", ">= 0.11.0"
spec.add_development_dependency "typhoeus", ">= 0.9.0"
spec.add_development_dependency "em-http-request", ">= 1.1.0"
spec.add_development_dependency "excon", ">= 0.59.0"
end
18 changes: 18 additions & 0 deletions spec/sniffer/adapters/excon_adapter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe HTTP do
let(:headers) { { "accept-encoding" => "gzip;q=1.0,deflate;q=0.6,identity;q=0.3", "accept" => "*/*", "user-agent" => "Ruby", "host" => "localhost:4567" } }
let(:get_request) { Excon.get('http://localhost', port: 4567, query: { lang: 'ruby', author: 'matz' }, headers: headers) }
let(:post_request) { Excon.post('http://localhost', port: 4567, path: '/data', query: { lang: 'ruby' }, body: "author=Matz") }

it 'logs', enabled: true do
logger = double
Sniffer.config.logger = logger
expect(logger).to receive(:log).with(0, "{\"port\":4567,\"host\":\"localhost\",\"query\":\"/?author=matz&lang=ruby\",\"rq_accept_encoding\":\"gzip;q=1.0,deflate;q=0.6,identity;q=0.3\",\"rq_accept\":\"*/*\",\"rq_user_agent\":\"Ruby\",\"rq_host\":\"localhost:4567\",\"method\":\"get\",\"request_body\":\"\",\"status\":200,\"rs_content_type\":\"text/html;charset=utf-8\",\"rs_x_xss_protection\":\"1; mode=block\",\"rs_x_content_type_options\":\"nosniff\",\"rs_x_frame_options\":\"SAMEORIGIN\",\"rs_content_length\":\"2\",\"timing\":0.0006,\"response_body\":\"OK\"}")
get_request
end

it_behaves_like "a sniffered", 'excon'
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require "typhoeus"
require "ethon"
require "em-http-request"
require "excon"

require "sniffer"
require "pry-byebug"
Expand Down
1 change: 1 addition & 0 deletions spec/support/shared_examples_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
end

it 'stores JSON correctly', enabled: true do
skip "Not implemented in adapter" unless respond_to?(:post_json)
post_json
expect(data_first_item.to_h).to match_yaml_file("#{fldr}/json_response")
end
Expand Down
22 changes: 22 additions & 0 deletions spec/yaml/excon/get_response.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
:request:
:host: "localhost"
:query: "/?author=matz&lang=ruby"
:port: 4567
:headers:
accept-encoding: gzip;q=1.0,deflate;q=0.6,identity;q=0.3
accept: "*/*"
user-agent: Ruby
host: "localhost:4567"
:body: ""
:method: :get
:response:
:status: 200
:headers:
content-type: text/html;charset=utf-8
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
content-length: '2'
:body: OK
:timing: 0.0006
22 changes: 22 additions & 0 deletions spec/yaml/excon/post_response.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
:request:
:host: "localhost"
:query: /data?lang=ruby
:port: 4567
:headers:
user-agent: "excon/0.59.0"
host: "localhost:4567"
content-length: 11
:body: author=Matz
:method: :post
:port: 4567
:response:
:status: 201
:headers:
content-type: text/html;charset=utf-8
x-xss-protection: 1; mode=block
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
content-length: '7'
:body: Created
:timing: 0.0006

0 comments on commit 8edae14

Please sign in to comment.