zoanthid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 930c8d9fb4cb927d63b4a29d758992959234d184
4
+ data.tar.gz: 5ed5369376a391e3df10264fc37ae626478edac1
5
+ SHA512:
6
+ metadata.gz: 29d1c07769c54b6f6a12f86ba4c28139062b69e0ba04e58854eab98b8a13e76ffbf3b61dbf77e0fafbc222cbdac1942bb3ee2f313ffd55494bd1573643ce92ff
7
+ data.tar.gz: 1044ba511f4498f9acff5ca41f43ff41671857348baf7331ef1cde235c5e0f44bfc8002593b78be70725a8eb84fcf1765cd7ca4ec546d179958034ed7a7751ab
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
@@ -0,0 +1,5 @@
1
+ # Changes
2
+
3
+ ## 0.1.0
4
+
5
+ * Initial run at an API.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in zoanthid.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ben Hamill
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # Zoanthid [![Build Status](https://travis-ci.org/benhamill/zoanthid.png)](https://travis-ci.org/benhamill/zoanthid) [![Code Climate](https://codeclimate.com/github/benhamill/zoanthid.png)](https://codeclimate.com/github/benhamill/zoanthid)
2
+
3
+ Zoanthids are [a kind of coral](http://en.wikipedia.org/wiki/Zoantharia) that is
4
+ very robust.
5
+
6
+ Zoanthid is a library that interfaces with Rails and RSpec to help you write
7
+ expressive tests for your [HAL](http://stateless.co/hal_specification.html) API.
8
+ It uses [Cetacean](https://github.com/benhamill/cetacean#readme) and
9
+ [Faraday](https://github.com/lostisland/faraday#readme) to make the requests and
10
+ deal with the responses. It's not configurable at this juncture.
11
+
12
+ ## Usage
13
+
14
+ This will most likely change, but here's the idea so far:
15
+
16
+ Require Zoanthid in your spec_helper.rb, then if you put spec files in
17
+ `spec/api/`, or label them with `type: :api` and you'll get some helpers.
18
+
19
+ ``` ruby
20
+ describe "listing posts" do
21
+ before do
22
+ get :root
23
+ get expand_uri(:posts)
24
+ end
25
+
26
+ it "shows the total" do
27
+ expect(document['total']).to eq(25)
28
+ end
29
+
30
+ it "has a next link" do
31
+ expect(expand_uri(:next)).to eq('/posts?page=2')
32
+ end
33
+
34
+ it "can jump to any page" do
35
+ expect(expand_uri(:jump_to_page, page: 99)).to eq('/posts?page=99')
36
+ end
37
+
38
+ it "embeds the posts" do
39
+ expect(document.embedded(:posts).count).to eq(25)
40
+ end
41
+ end
42
+ ```
43
+
44
+ ## The Future?
45
+
46
+ Possibly in the future, there might be nice matchers. I plan to add features
47
+ slowly, though, only in the face of an actual need.
48
+
49
+ ## Contributing
50
+
51
+ Help is gladly welcomed. If you have a feature you'd like to add, it's much more
52
+ likely to get in (or get in faster) the closer you stick to these steps:
53
+
54
+ 1. Open an Issue to talk about it. We can discuss whether it's the right
55
+ direction or maybe help track down a bug, etc.
56
+ 1. Fork the project, and make a branch to work on your feature/fix. Master is
57
+ where you'll want to start from.
58
+ 1. Turn the Issue into a Pull Request. There are several ways to do this, but
59
+ [hub](https://github.com/defunkt/hub) is probably the easiest.
60
+ 1. Make sure your Pull Request includes tests.
61
+ 1. Bonus points if your Pull Request updates `CHANGES.md` to include a summary
62
+ of your changes and your name like the other entries. If the last entry is
63
+ the last release, add a new `## Unreleased` heading at the top.
64
+
65
+ If you don't know how to fix something, even just a Pull Request that includes a
66
+ failing test can be helpful. If in doubt, make an Issue to discuss.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,52 @@
1
+ module HALSpec
2
+ module DSL
3
+ def app
4
+ ::Rails.application
5
+ end
6
+
7
+ def client
8
+ @client ||= Faraday.new do |client|
9
+ client.adapter :rack, app
10
+ client.headers['Accept'] = 'application/hal+json'
11
+ end
12
+ end
13
+
14
+ %w(get post put delete head patch).each do |verb|
15
+ define_method verb do |*args, &block|
16
+ Cetacean.new(client.send verb, *args, &block)
17
+ end
18
+ end
19
+ end
20
+
21
+ module ExampleGroup
22
+ extend ActiveSupport::Concern
23
+
24
+ DEFAULT_HOST = "www.example.com"
25
+
26
+ included do
27
+ metadata[:type] = :hal
28
+
29
+ app = ::Rails.application
30
+ if app.respond_to?(:routes)
31
+ include app.routes.url_helpers if app.routes.respond_to?(:url_helpers)
32
+ include app.routes.mounted_helpers if app.routes.respond_to?(:mounted_helpers)
33
+
34
+ if respond_to?(:default_url_options)
35
+ default_url_options[:host] ||= ::HALSpec::ExampleGroup::DEFAULT_HOST
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ RSpec.configure do |config|
43
+ config.include(
44
+ HALSpec::ExampleGroup,
45
+ type: :hal,
46
+ example_group: ->(example_group, metadata) do
47
+ metadata[:type].nil? && config.escaped_path(%w[spec hal]) =~ example_group[:file_path]
48
+ end
49
+ )
50
+
51
+ config.include HALSpec::DSL, type: :hal
52
+ end
@@ -0,0 +1,14 @@
1
+ require "zoanthid/version"
2
+ require "zoanthid/browser"
3
+ require "zoanthid/dsl"
4
+ require "zoanthid/example_group"
5
+
6
+ module Zoanthid
7
+ def self.app
8
+ @app
9
+ end
10
+
11
+ def self.app=(app)
12
+ @app = app
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ class Zoanthid::Browser
2
+ REQUEST_METHODS = [:get, :post, :put, :delete, :head, :patch]
3
+
4
+ def document
5
+ history.last
6
+ end
7
+
8
+ def clear_history!
9
+ @history = []
10
+ end
11
+
12
+ REQUEST_METHODS.each do |verb|
13
+ define_method verb do |*args, &block|
14
+ history << Cetacean.new(client.send verb, *args, &block)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def app
21
+ Zoanthid.app
22
+ end
23
+
24
+ def client
25
+ @client ||= Faraday.new do |client|
26
+ client.adapter :rack, app
27
+ client.headers['Accept'] = 'application/hal+json'
28
+ end
29
+ end
30
+
31
+ def history
32
+ @history ||= []
33
+ end
34
+ end
@@ -0,0 +1,28 @@
1
+ require 'cetacean'
2
+ require 'faraday'
3
+
4
+ module Zoanthid::DSL
5
+ def document
6
+ browser.document
7
+ end
8
+
9
+ def browser
10
+ @browser ||= Zoanthid::Browser.new
11
+ end
12
+
13
+ def expand_link(rel, expansions={})
14
+ document.get_uri(rel).expand(expansions)
15
+ end
16
+
17
+ def clear_history!
18
+ browser.clear_history!
19
+ end
20
+
21
+ Zoanthid::Browser::REQUEST_METHODS.each do |verb|
22
+ define_method verb do |*args, &block|
23
+ args[0] = '/' if args.first == :root
24
+
25
+ browser.send verb, *args, &block
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,13 @@
1
+ module Zoanthid
2
+ module ExampleGroup
3
+ DEFAULT_HOST = "www.example.com"
4
+
5
+ def included(base)
6
+ metadata[:type] = :api
7
+
8
+ if respond_to?(:default_url_options)
9
+ default_url_options[:host] ||= Zoanthid::ExampleGroup::DEFAULT_HOST
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ require 'zoanthid'
2
+ require 'rspec/core'
3
+
4
+ RSpec.configure do |config|
5
+ config.include(
6
+ Zoanthid::ExampleGroup,
7
+ type: :api,
8
+ example_group: ->(example_group, metadata) do
9
+ metadata[:type].nil? && config.escaped_path(%w[spec api]) =~ example_group[:file_path]
10
+ end
11
+ )
12
+
13
+ config.include Zoanthid::DSL, type: :api
14
+
15
+ config.after do
16
+ if self.class.include?(Zoanthid::DSL)
17
+ clear_history!
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Zoanthid
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
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
+
8
+ require_relative 'support/test_app'
9
+ require 'zoanthid/rspec'
10
+
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+
16
+ # Run specs in random order to surface order dependencies. If you find an
17
+ # order dependency and want to debug it, you can fix the order by providing
18
+ # the seed, which is printed after each run.
19
+ # --seed 1234
20
+ # config.order = 'random'
21
+
22
+ config.expect_with :rspec do |expectations|
23
+ expectations.syntax = :expect
24
+ end
25
+
26
+ config.mock_with :rspec do |mocks|
27
+ mocks.syntax = :expect
28
+ end
29
+ end
30
+
31
+ require 'pry'
@@ -0,0 +1,35 @@
1
+ require 'sinatra/base'
2
+ require 'json'
3
+
4
+ class TestApp < Sinatra::Base
5
+ configure do
6
+ mime_type :hal, 'application/hal+json'
7
+ end
8
+
9
+ before do
10
+ content_type :hal
11
+ end
12
+
13
+ after do
14
+ if content_type == 'application/hal+json'
15
+ body JSON.dump(body)
16
+ end
17
+ end
18
+
19
+ get '/' do
20
+ {
21
+ _links: {
22
+ self: { href: '/' },
23
+ posts: { href: '/posts{?page}', templated: true }
24
+ },
25
+ get_root: 'get root'
26
+ }
27
+ end
28
+
29
+ get '/posts' do
30
+ {
31
+ _links: { self: { href: '/posts' } },
32
+ posts: 'posts'
33
+ }
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+ require 'zoanthid'
3
+
4
+ describe Zoanthid, type: 'api' do
5
+ before do
6
+ Zoanthid.app = TestApp
7
+
8
+ get :root
9
+ end
10
+
11
+ it "handles GETs" do
12
+ expect(document['get_root']).to eq('get root')
13
+ end
14
+
15
+ it "can extract URIs" do
16
+ expect(expand_link(:posts)).to eq('/posts')
17
+ end
18
+
19
+ it "can expand URIs" do
20
+ expect(expand_link(:posts, page: 2)).to eq('/posts?page=2')
21
+ end
22
+
23
+ context "following a rel" do
24
+ before do
25
+ get expand_link(:posts)
26
+ end
27
+
28
+ it "ends up on the right document" do
29
+ expect(document['posts']).to eq('posts')
30
+ end
31
+ end
32
+
33
+ context "in a different context" do
34
+ it "forgets the document from another context" do
35
+ expect(document['get_root']).to eq('get root')
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zoanthid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zoanthid"
8
+ spec.version = Zoanthid::VERSION
9
+ spec.authors = ["Ben Hamill"]
10
+ spec.email = ["git-commits@benhamill.com"]
11
+ spec.description = %q{Zoanthid is a library that interfaces with Rails and RSpec to help you write expressive tests for your HAL API.}
12
+ spec.summary = %q{Kinda like Capybara for your HAL APIs.}
13
+ spec.homepage = "https://github.com/benhamill/zoanthid#readme"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "sinatra"
26
+
27
+ spec.add_dependency "cetacean", "~> 1.0"
28
+ spec.add_dependency "rack-test", "~> 0.6.2"
29
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zoanthid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ben Hamill
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sinatra
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: cetacean
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.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.6.2
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: 0.6.2
111
+ description: Zoanthid is a library that interfaces with Rails and RSpec to help you
112
+ write expressive tests for your HAL API.
113
+ email:
114
+ - git-commits@benhamill.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - .gitignore
120
+ - .rspec
121
+ - .travis.yml
122
+ - CHANGES.md
123
+ - Gemfile
124
+ - LICENSE.txt
125
+ - README.md
126
+ - Rakefile
127
+ - lib/api_spec.rb
128
+ - lib/zoanthid.rb
129
+ - lib/zoanthid/browser.rb
130
+ - lib/zoanthid/dsl.rb
131
+ - lib/zoanthid/example_group.rb
132
+ - lib/zoanthid/rspec.rb
133
+ - lib/zoanthid/version.rb
134
+ - spec/spec_helper.rb
135
+ - spec/support/test_app.rb
136
+ - spec/zoanthid_spec.rb
137
+ - zoanthid.gemspec
138
+ homepage: https://github.com/benhamill/zoanthid#readme
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - '>='
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.0.3
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Kinda like Capybara for your HAL APIs.
162
+ test_files:
163
+ - spec/spec_helper.rb
164
+ - spec/support/test_app.rb
165
+ - spec/zoanthid_spec.rb