rack-spyup 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13a00d09007c24f2788b9b4ec6cc1bb932229d98
4
- data.tar.gz: 5080110619f8bf7b52517355f3a3506739719723
3
+ metadata.gz: 175c2c54f6ae21ef59b8775bda9aaf22f4eae6ed
4
+ data.tar.gz: 268aed09f1a0980a3b26fe1c068130f75ee032c9
5
5
  SHA512:
6
- metadata.gz: 8b5391158635c471a3df14d20ef7dc06346bbb4dff3e75fa30a232f21726d2bb407d9bdb2ff419b1cb67c10bd7a74c61029691e2e43b92bd453305f7710cdec9
7
- data.tar.gz: 5232d7b4c384b333f9f69299dde0dc1e141e72f68979eceec87cae439700e73377dcc3e1d2b723665454ae392a64596bbb65efe04387321f885a68dd83341a7e
6
+ metadata.gz: 2a638629936907de7a34e93e75be8cfa742bc0919e2e8b895283788e6789582e56a9c74251110e4033ab87998371f5939c6a835dd95407611cd42ddbd9c76429
7
+ data.tar.gz: 2afe651048012d42ad6d60ad6c29e956f9b313aad16f2bd3f729b3964ea1812e6971d4adb91c262e34624c693e68712961c337f894a2478acf8d5a5501870a3c
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format d
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Spying request and json response
4
4
 
5
+ [![wercker status](https://app.wercker.com/status/badde83f7252323fde185c68212600ba/m "wercker status")](https://app.wercker.com/project/bykey/badde83f7252323fde185c68212600ba)
6
+
5
7
  ## Installation
6
8
 
7
9
  Add this line to your application's Gemfile:
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.pattern = "spec/**/*_spec.rb"
6
+ end
@@ -1,14 +1,17 @@
1
1
  require "rack/spyup/version"
2
+ require "rack/spyup/configuration"
2
3
  require "logger"
3
4
 
4
5
  module Rack
5
6
  class SpyUp
6
- def initialize(app, &configure)
7
+ def initialize(app, &instance_configure)
7
8
  @app = app
8
- configure.call(self) if block_given?
9
+ @colorize = true
10
+ @logger = self.class.config.logger
11
+ instance_configure.call(self) if block_given?
9
12
  end
10
13
  attr_reader :app
11
- attr_accessor :logger
14
+ attr_accessor :logger, :colorize
12
15
 
13
16
  def call(env)
14
17
  return @app.call(env) if ignore_case?(env)
@@ -26,6 +29,16 @@ module Rack
26
29
  return res.finish
27
30
  end
28
31
 
32
+ class << self
33
+ def config(&global_configure)
34
+ @__config ||= Rack::SpyUp::Configuration.default
35
+ if block_given?
36
+ global_configure.call(@__config)
37
+ end
38
+ @__config
39
+ end
40
+ end
41
+
29
42
  private
30
43
  def ignore_case?(env)
31
44
  # TODO more options to omit logging
@@ -102,9 +115,10 @@ Body:
102
115
  end
103
116
 
104
117
  def pretty_json(json)
105
- CodeRay.scan(JSON.pretty_unparse(JSON.parse(json)), :json)
106
- .terminal
107
- .gsub(/^/m, "\t")
118
+ formatted = JSON.pretty_unparse(JSON.parse(json))
119
+ formatted = CodeRay.scan(formatted, :json).terminal if colorize
120
+
121
+ formatted.gsub(/^/m, "\t")
108
122
  end
109
123
 
110
124
  def compose_response_headers(res)
@@ -117,15 +131,20 @@ Body:
117
131
  end
118
132
 
119
133
  def red(key)
134
+ return key unless colorize
120
135
  "\e[31m#{key}\e[0m"
121
136
  end
122
137
 
123
138
  def cyan(value)
139
+ return value unless colorize
124
140
  "\e[36m#{value}\e[0m"
125
141
  end
126
142
 
127
143
  def gray(value)
144
+ return value unless colorize
128
145
  "\e[37m#{value}\e[0m"
129
146
  end
130
147
  end
131
148
  end
149
+
150
+ require "rack/spyup/railtie" if defined?(Rails)
@@ -0,0 +1,15 @@
1
+ require 'rack/spyup'
2
+ module Rack
3
+ class SpyUp
4
+ class Configuration
5
+ attr_accessor :enabled_environments, :logger
6
+
7
+ def self.default
8
+ new.tap do |config|
9
+ config.enabled_environments = %w(development)
10
+ config.logger = nil
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ require 'rails/railtie'
2
+ require 'rack/spyup'
3
+
4
+ module Rack
5
+ class SpyUp
6
+ class Railtie < ::Rails::Railtie
7
+ config.rack_spyup = Rack::SpyUp.config
8
+
9
+ initializer "rack_spyup.initialize_middleware" do |app|
10
+ config = Rack::SpyUp.config
11
+ if Rails.env.to_s.in?(config.enabled_environments.map(&:to_s))
12
+ _logger = if config.logger && config.logger.respond_to?(:call)
13
+ config.logger.call()
14
+ elsif config.logger
15
+ config.logger
16
+ else
17
+ Rails.logger
18
+ end
19
+
20
+ app.middleware.insert 0, Rack::SpyUp do |mw|
21
+ mw.logger = _logger
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class SpyUp
3
- VERSION = "0.0.2"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -24,4 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "bundler", "~> 1.3"
25
25
  spec.add_development_dependency "rake"
26
26
  spec.add_development_dependency "pry"
27
+ spec.add_development_dependency "rspec", "~> 2.14.0"
28
+ spec.add_development_dependency "rack-test"
27
29
  end
@@ -0,0 +1,150 @@
1
+ require 'spec_helper'
2
+ require 'stringio'
3
+ require 'json'
4
+
5
+ describe Rack::SpyUp do
6
+ describe "should not smoke" do
7
+ before do
8
+ mock_app do
9
+ use Rack::SpyUp do |mw|
10
+ mw.logger = Logger.null_logger
11
+ end
12
+ run lambda {|env| [200, {"Content-Type" => "text/html"}, ["OK"]]}
13
+ end
14
+ end
15
+
16
+ it "should be OK" do
17
+ expect {
18
+ get "/"
19
+ }.not_to raise_error
20
+ end
21
+
22
+ it "should not affect response" do
23
+ get "/"
24
+ expect(last_response.body).to eq("OK")
25
+ end
26
+ end
27
+
28
+ describe "options" do
29
+ before do
30
+ @output = StringIO.new
31
+ end
32
+
33
+ it "should contain colored log" do
34
+ _logger = Logger.new(@output)
35
+ mock_app do
36
+ use Rack::SpyUp do |mw|
37
+ mw.logger = _logger
38
+ end
39
+ run lambda {|env| [200, {"Content-Type" => "text/html"}, ["OK"]]}
40
+ end
41
+
42
+ get "/hello"
43
+ @output.rewind
44
+ expect(@output.read).to match(/\e\[\d+m/)
45
+ end
46
+
47
+ it "should not contain colored log when configured" do
48
+ _logger = Logger.new(@output)
49
+ mock_app do
50
+ use Rack::SpyUp do |mw|
51
+ mw.logger = _logger
52
+ mw.colorize = false
53
+ end
54
+ run lambda {|env| [200, {"Content-Type" => "text/html"}, ["OK"]]}
55
+ end
56
+
57
+ get "/hello"
58
+ @output.rewind
59
+ expect(@output.read).not_to match(/\e\[\d+m/)
60
+ end
61
+ end
62
+
63
+ describe "should display request info" do
64
+ before do
65
+ @output = StringIO.new
66
+ _logger = Logger.new(@output)
67
+
68
+ mock_app do
69
+ use Rack::SpyUp do |mw|
70
+ mw.logger = _logger
71
+ mw.colorize = false
72
+ end
73
+ run lambda {|env| [200, {"Content-Type" => "text/html"}, ["OK"]]}
74
+ end
75
+ end
76
+
77
+ it "should print request info" do
78
+ header "Host", "udzura.example.jp"
79
+ get "/hello"
80
+ @output.rewind
81
+ expect(@output.read).to match(%r|GET http://udzura.example.jp/hello|)
82
+ end
83
+
84
+ it "should contain custom header info" do
85
+ header "X-Forwarder-For", "192.0.2.1,192.0.2.2"
86
+ header "X-Foobar", "true"
87
+ get "/"
88
+ @output.rewind
89
+ printed = @output.read
90
+
91
+ expect(printed).to match(/X-Forwarder-For: 192.0.2.1,192.0.2.2/)
92
+ expect(printed).to match(/X-Foobar: true/)
93
+ end
94
+ end
95
+
96
+ describe "should display response info" do
97
+ before do
98
+ @output = StringIO.new
99
+ @logger = Logger.new(@output)
100
+ end
101
+
102
+ let(:body) do
103
+ {response: "OK"}.to_json
104
+ end
105
+
106
+ it "should print response info" do
107
+ _logger = @logger
108
+ _body = body
109
+
110
+ mock_app do
111
+ use Rack::SpyUp do |mw|
112
+ mw.logger = _logger
113
+ mw.colorize = false
114
+ end
115
+ run lambda {|env| [200, {"Content-Type" => "application/json"}, [_body]]}
116
+ end
117
+
118
+ get "/index.json"
119
+ @output.rewind
120
+ response = @output.read
121
+
122
+ expect(response).to match(/Content-Type: application\/json/)
123
+ expect(response).to match(/Content-Length: #{body.length}/)
124
+ expect(response).to match(/Body:\s+{\s+"response": "OK"\s+}/m)
125
+ end
126
+
127
+ it "should contain custom response info, such as header" do
128
+ _logger = @logger
129
+ _body = body
130
+
131
+ mock_app do
132
+ use Rack::SpyUp do |mw|
133
+ mw.logger = _logger
134
+ mw.colorize = false
135
+ end
136
+ run lambda {|env|
137
+ [200,
138
+ {"Content-Type" => "application/json",
139
+ "X-Powerd-By" => "PHP/4.1.10"},
140
+ [_body]]}
141
+ end
142
+
143
+ get "/index.json"
144
+ @output.rewind
145
+ response = @output.read
146
+
147
+ expect(response).to match(/X-Powerd-By: PHP\/4.1.10/)
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,36 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'rack-spyup'
8
+
9
+ RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ # config.filter_run :focus
13
+
14
+ # Run specs in random order to surface order dependencies. If you find an
15
+ # order dependency and want to debug it, you can fix the order by providing
16
+ # the seed, which is printed after each run.
17
+ # --seed 1234
18
+ config.order = 'random'
19
+
20
+ require 'rack/test'
21
+ config.include Rack::Test::Methods
22
+ config.include Module.new {
23
+ def app
24
+ @app
25
+ end
26
+
27
+ def mock_app &builder
28
+ @app = Rack::Builder.new &builder
29
+ end
30
+ }
31
+ end
32
+
33
+ # extensions...
34
+ def Logger.null_logger
35
+ Logger.new(File.open(IO::NULL, 'w'))
36
+ end
@@ -0,0 +1,15 @@
1
+ box: wercker/ruby
2
+ # Build definition
3
+ # See the Ruby section on the wercker devcenter:
4
+ # http://devcenter.wercker.com/articles/languages/ruby.html
5
+ build:
6
+ # The steps that will be executed on build
7
+ steps:
8
+ # A step that executes `bundle install` command
9
+ - bundle-install
10
+
11
+ # A custom script step, name value is used in the UI
12
+ # and the code value contains the command that get executed
13
+ - script:
14
+ name: Running rake spec
15
+ code: bundle exec rake spec
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-spyup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Uchio KONDO
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-04 00:00:00.000000000 Z
11
+ date: 2013-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -80,6 +80,34 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 2.14.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 2.14.0
97
+ - !ruby/object:Gem::Dependency
98
+ name: rack-test
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
83
111
  description: Spying requests and responses with rack power
84
112
  email:
85
113
  - udzura@udzura.jp
@@ -89,6 +117,7 @@ extensions: []
89
117
  extra_rdoc_files: []
90
118
  files:
91
119
  - .gitignore
120
+ - .rspec
92
121
  - Gemfile
93
122
  - LICENSE.txt
94
123
  - README.md
@@ -100,8 +129,13 @@ files:
100
129
  - examples/config.ru
101
130
  - lib/rack-spyup.rb
102
131
  - lib/rack/spyup.rb
132
+ - lib/rack/spyup/configuration.rb
133
+ - lib/rack/spyup/railtie.rb
103
134
  - lib/rack/spyup/version.rb
104
135
  - rack-spyup.gemspec
136
+ - spec/lib/rack-spyup_spec.rb
137
+ - spec/spec_helper.rb
138
+ - wercker.yml
105
139
  homepage: https://github.com/udzura/rack-spyup
106
140
  licenses:
107
141
  - MIT
@@ -127,4 +161,6 @@ signing_key:
127
161
  specification_version: 4
128
162
  summary: Spying requests and responses with rack power, like json, msgpack, xml, and
129
163
  so on...
130
- test_files: []
164
+ test_files:
165
+ - spec/lib/rack-spyup_spec.rb
166
+ - spec/spec_helper.rb