capybara-json 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ -c
2
+ -t wip
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm gemset use capybara-json
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - head
6
+ gemfile:
7
+ - Gemfile
8
+ - Gemfile.capybara.lt.1.0
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in capybara-json.gemspec
4
+ gem 'capybara', '> 1.0.0'
5
+ gemspec
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in capybara-json.gemspec
4
+ gem 'capybara', '< 1.0.0'
5
+ gemspec
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # capybara-json
2
+
3
+ capybara-json provides the same interface to testing JSON API (both local and remote)
4
+
5
+ Capybara is an acceptance test framework, and it has no interest with client error(4xx response).
6
+ testing web application
7
+
8
+ ## USAGE
9
+ require 'capybara/json'
10
+ Capybara.current_driver = :rack_test_json
11
+
12
+ include Capybara::Json
13
+ post '/', { "this is" => "json" } # POST { "this is": "json" }
14
+ body #=> parsed json response
15
+ source #=> raw response body
16
+
17
+ ## ROADMAP
18
+
19
+ * 0.0.1
20
+ * create :rack_test_json driver which supports normal json response (2xx, 3xx)
21
+ * 0.0.2
22
+ * create :httpclient driver for remote testing
23
+ * 0.0.3
24
+ * create :httpclient_json driver with the same interface with :rack_test_json in normal json response
25
+ * 0.1.0
26
+ * ensure :rack_test_json and :httpclient_json has the same interface in error response (4xx, 5xx)
27
+ * 0.2.0
28
+ * add jsonpath? interface to search response
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,31 @@
1
+ # -*- encoding: utf-8 -*-
2
+ Gem::Specification.new do |s|
3
+ s.name = "capybara-json"
4
+ s.version = File.read(File.expand_path('VERSION', File.dirname(__FILE__))).chomp
5
+ s.authors = ["okitan"]
6
+ s.email = ["okitakunio@gmail.com"]
7
+ s.homepage = ""
8
+ s.summary = %q{for testing json-api with capybara}
9
+ s.description = %q{for testing json-api with capybara}
10
+
11
+ s.rubyforge_project = "capybara-json"
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+
18
+ [ [ 'capybara' ],
19
+ [ 'multi_json' ],
20
+ ].each do |gem, version|
21
+ s.add_runtime_dependency gem, version
22
+ end
23
+
24
+ [ [ 'rspec', '~> 2.8.0.rc' ],
25
+ [ 'sinatra' ],
26
+ [ 'yajl-ruby' ],
27
+ [ 'autowatchr' ],
28
+ ].each do |gem, version|
29
+ s.add_development_dependency gem, version
30
+ end
31
+ end
@@ -0,0 +1,2 @@
1
+ require 'capybara/json'
2
+
@@ -0,0 +1,35 @@
1
+ require 'capybara'
2
+ require 'capybara/dsl'
3
+
4
+ module Capybara
5
+ module Json
6
+ def self.included(base)
7
+ base.__send__(:include, Capybara::DSL) unless base < Capybara or base < Capybara::DSL
8
+ base.extend(self)
9
+ end
10
+
11
+ %w[ get delete ].each do |method|
12
+ module_eval %{
13
+ def #{method}(path, params = {}, env = {})
14
+ page.driver.#{method}(path, params, env)
15
+ end
16
+ }
17
+ end
18
+
19
+ %w[ post put ].each do |method|
20
+ module_eval %{
21
+ def #{method}(path, json, env = {})
22
+ page.driver.#{method}(path, json, env)
23
+ end
24
+ }
25
+ end
26
+ end
27
+
28
+ module RackTestJson
29
+ autoload :Driver, 'capybara/rack_test_json/driver'
30
+ end
31
+ end
32
+
33
+ Capybara.register_driver :rack_test_json do |app|
34
+ Capybara::RackTestJson::Driver.new(app)
35
+ end
@@ -0,0 +1,24 @@
1
+ require 'multi_json'
2
+
3
+ class Capybara::RackTestJson::Driver < Capybara::RackTest::Driver
4
+ def body
5
+ MultiJson.decode(source) || {}
6
+ end
7
+
8
+
9
+ %w[ post put ].each do |method|
10
+ class_eval %{
11
+ def #{method}(path, json, env = {})
12
+ json = MultiJson.encode(json) unless json.is_a?(String)
13
+
14
+ request_env = {
15
+ 'CONTENT_LENGTH' => json.size,
16
+ 'CONTENT_TYPE' => "application/json; charset=\#{json.encoding.to_s.downcase}",
17
+ 'rack.input' => StringIO.new(json)
18
+ }.merge(env)
19
+
20
+ super(path, {}, request_env)
21
+ end
22
+ }
23
+ end
24
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe "to require 'capybara/json'" do
4
+ it 'should register driver' do
5
+ Capybara.drivers.should have_key(:rack_test_json)
6
+ end
7
+ end
8
+
9
+ describe Capybara::Json do
10
+ include described_class
11
+
12
+ before(:all) do
13
+ Capybara.app = JsonTestApp
14
+ Capybara.current_driver = :rack_test_json
15
+ end
16
+
17
+ after(:all) do
18
+ Capybara.app = nil
19
+ Capybara.current_driver = Capybara.default_driver
20
+ end
21
+
22
+ %w[ get delete ].each do |method|
23
+ it "register #{method}" do
24
+ __send__(method, '/')
25
+ body.should == { 'Hello world!' => 'Hello world!' }
26
+ end
27
+ end
28
+
29
+ %w[ post put ].each do |method|
30
+ it "register #{method}" do
31
+ __send__(method, '/', {})
32
+ body.should == { 'Hello world!' => 'Hello world!' }
33
+ end
34
+
35
+ it "#{method} send json" do
36
+ json = { "some" => "args" }
37
+ __send__(method, '/env', json)
38
+
39
+ body['content_type'].should =~ %r"application/json"
40
+ body['rack.input'].should == MultiJson.encode(json)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ # this code is written in capybara's spec/spec_helper
4
+ alias :running :lambda
5
+
6
+ klass = Capybara::RackTestJson::Driver
7
+
8
+ describe klass do
9
+ before { @driver = described_class.new(JsonTestApp) }
10
+
11
+ it_should_behave_like 'driver'
12
+ it_should_behave_like 'driver with header support'
13
+ it_should_behave_like 'driver with status code support'
14
+ it_should_behave_like 'driver with cookies support'
15
+ it_should_behave_like 'driver with infinite redirect detection'
16
+
17
+ it_should_behave_like 'driver to post json'
18
+ it_should_behave_like 'driver to put json'
19
+ end
data/spec/spec.watchr ADDED
@@ -0,0 +1,10 @@
1
+ require 'autowatchr'
2
+
3
+ Autowatchr.new(self) do |config|
4
+ config.ruby = 'clear && rspec'
5
+ config.test_dir = 'spec'
6
+ config.test_re = "^#{config.test_dir}/(.*)_spec\.rb$"
7
+ config.test_file = '%s_spec.rb'
8
+ end
9
+
10
+
@@ -0,0 +1,10 @@
1
+ ROOT = File.dirname(__FILE__)
2
+ $: << File.expand_path('../lib', ROOT)
3
+
4
+ require 'capybara/json'
5
+ Dir[File.expand_path("support/**/*.rb", ROOT)].each {|f| require f }
6
+
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ end
@@ -0,0 +1,64 @@
1
+ require 'capybara/spec/driver'
2
+
3
+ [ 'driver', 'driver with header support' ].each do |shared|
4
+ RSpec.world.shared_example_groups.delete(shared)
5
+ end
6
+
7
+ shared_examples_for 'driver' do
8
+ describe '#visit' do
9
+ it "should move to another page" do
10
+ @driver.visit('/')
11
+ @driver.body.should include('Hello world!')
12
+ @driver.visit('/foo')
13
+ @driver.body.should include('Another World')
14
+ end
15
+
16
+ it "should show the correct URL" do
17
+ @driver.visit('/foo')
18
+ @driver.current_url.should include('/foo')
19
+ end
20
+ end
21
+
22
+ describe '#body' do
23
+ it "should return json reponses" do
24
+ @driver.visit('/')
25
+ @driver.body.should include('Hello world!')
26
+ end
27
+ # pending encoding
28
+ end
29
+
30
+ # TODO: find by jsonpath?
31
+ end
32
+
33
+ shared_examples_for 'driver with header support' do
34
+ it "should make headers available through response_headers" do
35
+ @driver.visit('/')
36
+ @driver.response_headers['Content-Type'].should =~ /^application\/json/
37
+ end
38
+ end
39
+
40
+
41
+ %w[ post put ].each do |method|
42
+ shared_examples_for "driver to #{method} json" do
43
+ it 'should set content type as json to request' do
44
+ @driver.__send__(method, '/env', {})
45
+ @driver.body['content_type'].should =~ %r"^application/json"
46
+ end
47
+
48
+ it 'should set content length' do
49
+ json = { :some => :args }
50
+
51
+ @driver.__send__(method, '/env', json)
52
+ @driver.body['content_length'].should == MultiJson.encode(json).length
53
+ end
54
+
55
+ it 'should post body' do
56
+ json = { :some => :args }
57
+
58
+ @driver.__send__(method, '/env', json)
59
+ @driver.body['rack.input'].should == MultiJson.encode(json)
60
+ end
61
+ end
62
+ end
63
+
64
+
@@ -0,0 +1,40 @@
1
+ require 'capybara/spec/test_app'
2
+
3
+ class JsonTestApp < TestApp
4
+ def invoke
5
+ res = catch(:halt) { yield }
6
+
7
+ res = [ MultiJson.encode(res => res) ] if Fixnum === res or String === res
8
+ res = [ MultiJson.encode(res) ] if Hash === res
9
+ if Array === res and Fixnum === res.first
10
+ status(res.shift)
11
+ body(res.pop)
12
+ headers(*res)
13
+ elsif res.respond_to? :each
14
+ content_type :json
15
+ body res
16
+ end
17
+ end
18
+
19
+ %w[ get post put delete ].each do |method|
20
+ __send__(method, '/') do
21
+ 'Hello world!'
22
+ end
23
+
24
+ # rack specifications
25
+ # http://rack.rubyforge.org/doc/SPEC.html
26
+ __send__(method, '/env') do
27
+ envs = %w[ REQUEST_METHOD PATH_INFO QUERY_STRING CONTENT_TYPE CONTENT_LENGTH rack.url_scheme ]\
28
+ .inject({}) do |hash, key|
29
+ hash[key.downcase] = env[key]
30
+ hash
31
+ end
32
+ headers = env.select {|key, value| key.start_with?('HTTP_') }.inject({}) do |hash, (key, value)|
33
+ hash[key.sub('HTTP_', '')] = value
34
+ hash
35
+ end
36
+
37
+ envs.merge('params' => params, 'headers' => headers, 'rack.input' => env['rack.input'].string)
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: capybara-json
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - okitan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: capybara
16
+ requirement: &83236190 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *83236190
25
+ - !ruby/object:Gem::Dependency
26
+ name: multi_json
27
+ requirement: &83234440 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *83234440
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &83233800 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 2.8.0.rc
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *83233800
47
+ - !ruby/object:Gem::Dependency
48
+ name: sinatra
49
+ requirement: &83228020 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *83228020
58
+ - !ruby/object:Gem::Dependency
59
+ name: yajl-ruby
60
+ requirement: &83227250 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *83227250
69
+ - !ruby/object:Gem::Dependency
70
+ name: autowatchr
71
+ requirement: &83226910 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *83226910
80
+ description: for testing json-api with capybara
81
+ email:
82
+ - okitakunio@gmail.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - .gitignore
88
+ - .rspec
89
+ - .rvmrc
90
+ - .travis.yml
91
+ - Gemfile
92
+ - Gemfile.capybara.lt.1.0
93
+ - README.md
94
+ - Rakefile
95
+ - VERSION
96
+ - capybara-json.gemspec
97
+ - lib/capybara-json.rb
98
+ - lib/capybara/json.rb
99
+ - lib/capybara/rack_test_json/driver.rb
100
+ - spec/capybara/json_spec.rb
101
+ - spec/capybara/rack_test_json/driver_spec.rb
102
+ - spec/spec.watchr
103
+ - spec/spec_helper.rb
104
+ - spec/support/driver_examples.rb
105
+ - spec/support/json_test_app.rb
106
+ homepage: ''
107
+ licenses: []
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project: capybara-json
126
+ rubygems_version: 1.8.10
127
+ signing_key:
128
+ specification_version: 3
129
+ summary: for testing json-api with capybara
130
+ test_files:
131
+ - spec/capybara/json_spec.rb
132
+ - spec/capybara/rack_test_json/driver_spec.rb
133
+ - spec/spec.watchr
134
+ - spec/spec_helper.rb
135
+ - spec/support/driver_examples.rb
136
+ - spec/support/json_test_app.rb