faraday-conductivity 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -31,9 +31,6 @@ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
31
31
 
32
32
  # provided by Faraday itself
33
33
  faraday.token_auth "secret"
34
-
35
-
36
- # provided by Faraday
37
34
  faraday.request :multipart
38
35
  faraday.request :url_encoded
39
36
 
@@ -59,7 +59,7 @@ module Faraday
59
59
 
60
60
  def format_headers(headers)
61
61
  length = headers.map {|k,v| k.to_s.size }.max
62
- headers.map { |name, value| "#{name.to_s.ljust(length)} : #{value.inspect}" }.join("\n")
62
+ headers.map { |name, value| "#{name.to_s.ljust(length)} : #{value}" }.join("\n")
63
63
  end
64
64
 
65
65
  end
@@ -1,6 +1,5 @@
1
1
  require "etc"
2
2
  require "socket"
3
- require "thread"
4
3
 
5
4
  module Faraday
6
5
  module Conductivity
@@ -10,17 +9,42 @@ module Faraday
10
9
  super(app)
11
10
  @name = options.fetch(:app) { "Faraday" }
12
11
  @version = options.fetch(:version) { "0.0" }
12
+ @environment = options.fetch(:environment) { Environment.new }
13
13
  end
14
14
 
15
15
  def call(env)
16
- login = Etc.getlogin
17
- hostname = Socket.gethostname
18
- pid = Process.pid
19
- user_agent = "#{@name}/#{@version} (#{hostname}; #{login}; #{pid}) #{RUBY_ENGINE}/#{RUBY_VERSION} (#{RUBY_PATCHLEVEL}; #{RUBY_PLATFORM})"
20
- env[:request_headers]['User-Agent'] ||= user_agent
16
+ env[:request_headers]['User-Agent'] = user_agent
21
17
  @app.call(env)
22
18
  end
23
19
 
20
+ def user_agent
21
+ [ app, ruby ].join(' ')
22
+ end
23
+
24
+ def app
25
+ "#{@name}/#{@version} (#{@environment.hostname}; #{@environment.login}; #{@environment.pid})"
26
+ end
27
+
28
+ def ruby
29
+ "#{RUBY_ENGINE}/#{RUBY_VERSION} (#{RUBY_PATCHLEVEL}; #{RUBY_PLATFORM})"
30
+ end
31
+
32
+ end
33
+
34
+ class Environment
35
+
36
+ def login
37
+ Etc.getlogin
38
+ end
39
+
40
+ def hostname
41
+ Socket.gethostname
42
+ end
43
+
44
+ def pid
45
+ Process.pid
46
+ end
47
+
24
48
  end
25
49
  end
26
50
  end
@@ -1,5 +1,5 @@
1
1
  module Faraday
2
2
  module Conductivity
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+ require 'logger'
3
+
4
+ describe Faraday::Conductivity::ExtendedLogging do
5
+
6
+ subject(:log) { io.read }
7
+
8
+ it "includes the HTTP verb" do
9
+ log.should include "GET"
10
+ end
11
+
12
+ it "includes the request body" do
13
+ log.should include "the request body"
14
+ end
15
+
16
+ it "includes the request headers" do
17
+ log.should match %r"X-Foo\s+: bar"
18
+ end
19
+
20
+ it "includes the complete URL" do
21
+ log.should include "http://widgets.example.org/test"
22
+ end
23
+
24
+ it "includes the response status" do
25
+ log.should include "200"
26
+ end
27
+
28
+ it "includes the response time" do
29
+ log.should match(/\d+\.\d+ms/)
30
+ end
31
+
32
+ it "includes the response headers" do
33
+ log.should include "X-Bar : foo"
34
+ end
35
+
36
+ it "includes the response body" do
37
+ log.should include "the response body"
38
+ end
39
+
40
+ before do
41
+ perform_request
42
+ io.rewind
43
+ end
44
+
45
+ let(:io) { StringIO.new }
46
+ let(:logger) { Logger.new(io) }
47
+
48
+ def perform_request
49
+ connection.get("/test") do |request|
50
+ request.headers["X-Foo"] = "bar"
51
+ request.body = "the request body"
52
+ end
53
+ end
54
+
55
+ def connection
56
+ create_connection do |faraday|
57
+ faraday.use :extended_logging, :logger => logger
58
+ end
59
+ end
60
+
61
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe Faraday::Conductivity::Mimetype do
4
+
5
+ subject(:request_headers) { response.env[:request_headers] }
6
+
7
+ it "includes the mimetype specified" do
8
+ request_headers["Accept"].should eq mimetype
9
+ end
10
+
11
+ let(:mimetype) { "application/vnd.users-v2+json" }
12
+
13
+ def connection
14
+ create_connection do |faraday|
15
+ faraday.request :mimetype, :accept => mimetype
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe Faraday::Conductivity::RequestId do
4
+
5
+ subject(:request_headers) { response.env[:request_headers] }
6
+
7
+ it "includes the thread local variable" do
8
+ Thread.current[:request_id] = "my-request-id"
9
+ request_headers["X-Request-Id"].should eq "my-request-id"
10
+ end
11
+
12
+ it "doesn't add the header if there is no request id" do
13
+ Thread.current[:request_id] = nil
14
+ request_headers.should_not have_key "X-Request-Id"
15
+ end
16
+
17
+ def connection
18
+ create_connection do |faraday|
19
+ faraday.request :request_id
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Faraday::Conductivity::UserAgent do
4
+
5
+ subject(:user_agent) { response.env[:request_headers]["User-Agent"] }
6
+
7
+ it "includes the name and version of the app" do
8
+ user_agent.should start_with "MarketingSite/1.1"
9
+ end
10
+
11
+ it "includes the current ruby version" do
12
+ user_agent.should include RUBY_VERSION
13
+ end
14
+
15
+ it "includes the current ruby engine" do
16
+ user_agent.should include RUBY_ENGINE
17
+ end
18
+
19
+ it "includes the current ruby patch level" do
20
+ user_agent.should include RUBY_PATCHLEVEL.to_s
21
+ end
22
+
23
+ it "includes the platform" do
24
+ user_agent.should include RUBY_PLATFORM
25
+ end
26
+
27
+ it "includes the pid" do
28
+ user_agent.should include "1337"
29
+ end
30
+
31
+ it "includes the current host" do
32
+ user_agent.should include "my.hostname"
33
+ end
34
+
35
+ it "includes the current user name" do
36
+ user_agent.should include "linus"
37
+ end
38
+
39
+ let(:environment) {
40
+ double :environment, :login => "linus", :hostname => "my.hostname", :pid => 1337
41
+ }
42
+
43
+ def connection
44
+ create_connection do |faraday|
45
+ faraday.request :user_agent, :app => "MarketingSite", :version => "1.1", :environment => environment
46
+ end
47
+ end
48
+
49
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,36 @@
1
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
1
  require 'faraday/conductivity'
2
+
3
+ module SpecHelper
4
+
5
+ def response
6
+ connection.get("/test")
7
+ end
8
+
9
+ def create_connection
10
+ Faraday.new(url: "http://widgets.example.org") do |faraday|
11
+ yield faraday
12
+ faraday.adapter :test, stubs
13
+ end
14
+ end
15
+
16
+ def stubs
17
+ @stubs ||= create_stubs do |stub|
18
+ stub.get('/test') { |env| [200, {"X-Bar" => "foo"} , "the response body"] }
19
+ end
20
+ end
21
+
22
+ def create_stubs
23
+ Faraday::Adapter::Test::Stubs.new do |stub|
24
+ yield stub
25
+ end
26
+ end
27
+
28
+ end
29
+
30
+ RSpec.configure do |config|
31
+ config.treat_symbols_as_metadata_keys_with_true_values = true
32
+ config.run_all_when_everything_filtered = true
33
+ config.filter_run :focus
34
+ config.order = 'random'
35
+ config.include SpecHelper
36
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday-conductivity
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-23 00:00:00.000000000 Z
12
+ date: 2013-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -96,7 +96,10 @@ files:
96
96
  - lib/faraday/conductivity/request_id_filter.rb
97
97
  - lib/faraday/conductivity/user_agent.rb
98
98
  - lib/faraday/conductivity/version.rb
99
- - spec/faraday/conductivity_spec.rb
99
+ - spec/middleware/extended_logging_spec.rb
100
+ - spec/middleware/mimetype_spec.rb
101
+ - spec/middleware/request_id_spec.rb
102
+ - spec/middleware/user_agent_spec.rb
100
103
  - spec/spec_helper.rb
101
104
  homepage: https://github.com/yourkarma/faraday-conductivity
102
105
  licenses:
@@ -124,5 +127,8 @@ signing_key:
124
127
  specification_version: 3
125
128
  summary: Extra Faraday middleware, geared towards a service oriented architecture.
126
129
  test_files:
127
- - spec/faraday/conductivity_spec.rb
130
+ - spec/middleware/extended_logging_spec.rb
131
+ - spec/middleware/mimetype_spec.rb
132
+ - spec/middleware/request_id_spec.rb
133
+ - spec/middleware/user_agent_spec.rb
128
134
  - spec/spec_helper.rb
@@ -1,69 +0,0 @@
1
- require "logger"
2
- require "faraday/conductivity"
3
- require "faraday_middleware"
4
- require "json"
5
-
6
- # I'm sorry about the state of this test file.
7
- # This will be cleaned up, I promise.
8
-
9
- describe Faraday::Conductivity do
10
-
11
- it 'should have a version number' do
12
- Faraday::Conductivity::VERSION.should_not be_nil
13
- end
14
-
15
- example "user_agent" do
16
- connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
17
- faraday.request :user_agent, app: "MarketingSite", version: "1.1"
18
- end
19
- request = connection.get("/foobar")
20
- request.env[:request_headers]["User-Agent"].should match %r(^MarketingSite/1.1 \([^\s]+; [^\s]+; \d+\) ruby/1.9.3)
21
- end
22
-
23
- example "extended_logging" do
24
- io = StringIO.new
25
- dummy_logger = Logger.new(io)
26
- request_with do |faraday|
27
- faraday.use :extended_logging, logger: dummy_logger
28
- end
29
- io.rewind
30
- logged = io.read
31
- logged.should include "Started GET request"
32
- logged.should include "http://example.org/test"
33
- logged.should include "the dummy response"
34
- logged.should include "the request body"
35
- logged.should include "X-Response-Header"
36
- logged.should include "header-value"
37
- # puts logged
38
- end
39
-
40
- example "request id" do
41
- Thread.current[:request_id] = "my-request-id"
42
- response = request_with do |faraday|
43
- faraday.request :request_id
44
- end
45
- response.env[:request_headers]["X-Request-Id"].should eq "my-request-id"
46
- end
47
-
48
- example "mimetype" do
49
- mimetype = "application/vnd.users-v2+json"
50
- response = request_with do |faraday|
51
- faraday.request :mimetype, accept: mimetype
52
- end
53
- response.env[:request_headers]["Accept"].should eq mimetype
54
- end
55
-
56
- def request_with
57
- stubs = Faraday::Adapter::Test::Stubs.new do |stub|
58
- stub.get('/test') { |env| [200, {"X-Response-Header" => "header-value"}, {foo:"the dummy response"}.to_json] }
59
- end
60
- connection = Faraday.new(url: "http://example.org/") do |faraday|
61
- yield faraday
62
- faraday.adapter :test, stubs
63
- end
64
- connection.get("/test") do |req|
65
- req.body = "the request body"
66
- end
67
- end
68
-
69
- end