api-spec 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 13025c2c8d737b6ef9f3fcd35f0b91ea56497bfa
4
+ data.tar.gz: 01e12492405ea6e734df32921e6596ad442fa8a7
5
+ SHA512:
6
+ metadata.gz: 1d154d6490b635275889107a5a8cbad347c01e64cc1097500af754bb6b9b3ad0f5e98c05586eba0983c13cafeb5a086774772585537ac6644e8f161c759406a0
7
+ data.tar.gz: 4722389b44a3a729758f0e31a47b16e2c3b48d6619f5e20c3fe913072c3fba8630e8f1f5814683eb5c958716348664c1d4cc6f900ed96077f2ada2f6b4a1eb70
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in api-spec.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Bruz Marzolf
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.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # API Spec
2
+
3
+ Cucumber steps to make it easier to build API tests.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'api-spec'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install api-spec
20
+
21
+ ## Usage
22
+
23
+ TODO
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/CXInc/api-spec/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/api-spec.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'api_spec/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "api-spec"
8
+ spec.version = ApiSpec::VERSION
9
+ spec.authors = ["Bruz Marzolf"]
10
+ spec.email = ["bruz@bruzilla.com"]
11
+ spec.summary = %q{Helpers for writing cucumber API tests.}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "rest-client", "~> 1.7"
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
data/lib/api-spec.rb ADDED
@@ -0,0 +1 @@
1
+ require "api_spec"
@@ -0,0 +1,65 @@
1
+ require "json_spec/cucumber"
2
+
3
+ require_relative "../api_spec"
4
+
5
+ World(ApiSpec::Helpers)
6
+
7
+ Before do |scenario|
8
+ $test_name = scenario.name.downcase.gsub(" ", "_")
9
+ ApiSpec::State.reset
10
+ end
11
+
12
+ # For json_spec
13
+ def last_json
14
+ @response.body
15
+ end
16
+
17
+ When(/^I GET "(.*?)"$/) do |path|
18
+ parameters = ApiSpec::Parameters.new(:get, path)
19
+ @response = RestClient.get parameters.url, test_headers
20
+ puts "@response = #{@response}"
21
+ end
22
+
23
+ When(/^I GET "(.*?)" with:$/) do |path, table|
24
+ make_request(:get, path, table)
25
+ end
26
+
27
+ When(/^I DELETE "(.*?)" with:$/) do |path, table|
28
+ make_request(:delete, path, table)
29
+ end
30
+
31
+ Then(/^the status code is (\d+)$/) do |code|
32
+ @response.code.to_s.should == code
33
+ end
34
+
35
+ Then(/^the Content\-Type is (.*)$/) do |content_type|
36
+ unless @response.respond_to?(:headers)
37
+ fail "Headers are not available on 4XX and 5XX responses"
38
+ end
39
+
40
+ @response.headers[:content_type].should match(content_type)
41
+ end
42
+
43
+ Then(/^the response contains an authentication cookie$/) do
44
+ @response.headers[:set_cookie].first.should match(/x-cx-auth/)
45
+ end
46
+
47
+ When(/^I POST to "(.*?)" with:$/) do |path, table|
48
+ make_request(:post, path, table)
49
+ end
50
+
51
+ When(/^I PUT to "(.*?)" with:$/) do |path, table|
52
+ make_request(:put, path, table)
53
+ end
54
+
55
+ Then(/^the JSON response at "(.*?)" should be variable "(.*?)"$/) do |path, key|
56
+ step "the JSON response at \"#{path}\" should be \"#{ ApiSpec::State.get(key) }\""
57
+ end
58
+
59
+ Then(/^the JSON response at "(.*?)" should include keys:$/) do |base_path, table|
60
+ table.hashes.each do |hash|
61
+ path = "#{ base_path }/#{ hash[:key] }"
62
+ value = hash[:value]
63
+ step "the JSON response at \"#{ path }\" should be #{ value }"
64
+ end
65
+ end
@@ -0,0 +1,45 @@
1
+ require "rest-client"
2
+
3
+ module ApiSpec
4
+ class ErrorResponse < Struct.new(:code, :body)
5
+ end
6
+
7
+ module Helpers
8
+ extend self
9
+
10
+ def test_headers
11
+ {"X-Test-Name" => $test_name}
12
+ end
13
+
14
+ def make_request(method, path, table)
15
+ parameters = ApiSpec::Parameters.new(method, path, table)
16
+ headers = parameters.headers.merge(test_headers)
17
+
18
+ begin
19
+ case method
20
+ when :get
21
+ @response = RestClient.get parameters.url,
22
+ headers.merge(params: parameters.query)
23
+ when :post
24
+ @response = RestClient.post parameters.url, parameters.body,
25
+ headers.merge(content_type: :json)
26
+ when :put
27
+ @response = RestClient.put parameters.url, parameters.body,
28
+ headers.merge(content_type: :json)
29
+ when :delete
30
+ @response = RestClient.delete parameters.url,
31
+ headers
32
+ else
33
+ fail "Unsupported method: #{method}"
34
+ end
35
+
36
+ if ENV["API_SPEC_DEBUG"]
37
+ puts "@response = #{@response}"
38
+ end
39
+ rescue RestClient::Exception => e
40
+ @response = ErrorResponse.new(e.http_code, e.http_body)
41
+ puts "Error response body: #{ @response.body }"
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,115 @@
1
+ module ApiSpec
2
+ class Parameters
3
+ attr_reader :query
4
+
5
+ def initialize(method, path, table = nil)
6
+ @method = method
7
+ @path = path
8
+
9
+ @path_params = {}
10
+ @query = {}
11
+ @form = {}
12
+ @json = {}
13
+ @cookie = {}
14
+ @header = {}
15
+
16
+ load_table(table) if table
17
+ end
18
+
19
+ def url
20
+ interpolated_path = @path.scan(/\{(.*?)\}/).reduce(@path) do |path, match|
21
+ key = match.first
22
+ value = @path_params[key]
23
+
24
+ raise StandardError.new("Missing #{key}") unless value
25
+
26
+ path.gsub /\{#{key}\}/, URI.encode(value)
27
+ end
28
+
29
+ "http://localhost:#{$port}#{interpolated_path}"
30
+ end
31
+
32
+ def method_missing(name)
33
+ State.get(name)
34
+ end
35
+
36
+ def body
37
+ if @json.any?
38
+ @json.to_json
39
+ elsif @form.any?
40
+ @form
41
+ else
42
+ ""
43
+ end
44
+ end
45
+
46
+ def headers
47
+ @header.merge(cookie_header)
48
+ end
49
+
50
+ def cookie_header
51
+ string = @cookie
52
+ .map { |key, value| "#{key}=#{value}" }
53
+ .join("; ")
54
+
55
+ { "Cookie" => string }
56
+ end
57
+
58
+ private
59
+
60
+ def load_table(table)
61
+ table.hashes.each do |hash|
62
+ matches = hash[:value].match(/\{(.*)\}/)
63
+ key = hash["parameter name"]
64
+
65
+ value = if matches
66
+ method = matches[1]
67
+ hash[:value].gsub(/\{#{method}\}/, send(method).to_s)
68
+ else
69
+ hash[:value]
70
+ end
71
+
72
+ set_param(hash["parameter type"], key, value)
73
+ end
74
+ end
75
+
76
+ def set_param(type, key, value)
77
+ case type
78
+ when "path"
79
+ @path_params[key] = value
80
+ when "query"
81
+ @query[key] = value
82
+ when "json"
83
+ parts = key.split("/")
84
+
85
+ hash = parts.reverse.reduce(nil) do |acc, part|
86
+ if part.match(/\d+/)
87
+ [acc || json_value(value)]
88
+ else
89
+ if acc
90
+ {part => acc}
91
+ else
92
+ {part => json_value(value)}
93
+ end
94
+ end
95
+ end
96
+
97
+ @json.merge!(hash)
98
+ when "form"
99
+ @form[key] = value
100
+ when "cookie"
101
+ @cookie[key] = value
102
+ when "header"
103
+ @header[key] = value
104
+ else
105
+ fail "Unrecognized parameter type: #{type}"
106
+ end
107
+ end
108
+
109
+ def json_value(value_string)
110
+ JSON.parse("{\"value\":#{value_string}}")["value"]
111
+ rescue JSON::ParserError
112
+ value_string
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,23 @@
1
+ module ApiSpec
2
+ class State
3
+ class << self
4
+ def set(key, value)
5
+ state[key.to_sym] = value
6
+ end
7
+
8
+ def get(key)
9
+ state[key.to_sym]
10
+ end
11
+
12
+ def reset
13
+ @state = {}
14
+ end
15
+
16
+ private
17
+
18
+ def state
19
+ @state ||= {}
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module ApiSpec
2
+ VERSION = "0.1.0"
3
+ end
data/lib/api_spec.rb ADDED
@@ -0,0 +1,8 @@
1
+ module ApiSpec
2
+ end
3
+
4
+ require_relative "api_spec/helpers"
5
+ require_relative "api_spec/parameters"
6
+ require_relative "api_spec/state"
7
+ require_relative "api_spec/version"
8
+
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: api-spec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bruz Marzolf
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description:
56
+ email:
57
+ - bruz@bruzilla.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - api-spec.gemspec
68
+ - lib/api-spec.rb
69
+ - lib/api_spec.rb
70
+ - lib/api_spec/cucumber.rb
71
+ - lib/api_spec/helpers.rb
72
+ - lib/api_spec/parameters.rb
73
+ - lib/api_spec/state.rb
74
+ - lib/api_spec/version.rb
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.4.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Helpers for writing cucumber API tests.
99
+ test_files: []