rspec-api 0.1.1 → 0.1.2

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: a73666993aeb89c6f6d8256fbd334cf95042c055
4
- data.tar.gz: b2c4a007b05a4a00427b169dc1bde8440852243b
3
+ metadata.gz: c57c8cdc4f5827c53876cb303f2bea89fefae297
4
+ data.tar.gz: 4ae692036878221cd3d1cadc572f13d5c1b9a913
5
5
  SHA512:
6
- metadata.gz: 172cff78f4cf2cc300fa1e049975cb80604cccc8aa967156d9363b81cee93ee1321e82d1b68f720678f8ec3abd75c48ae95a87aac93d40049ca20675ae46bd7a
7
- data.tar.gz: f380e9efcb009d145b5faa83792be55a4e0a6c527026498318fc99702189af5c94612b32099bcd1546a471b6e39a9632206004a4c472f257fccdeb06f6508d6a
6
+ metadata.gz: 4f2bc0c289191ddb64a0c936e04c861afeed46516f2e4d00edf609888fcbe8a9cbed8ddad9a9da09f6e1cd9f42bc0f9bcfcd014d59adcc8afc75dd14196e63e6
7
+ data.tar.gz: 15437e3057b40d9121253bbe42257967943b5455d2655aae305a9674599de50c6a5a30fa735338791ed6fc75e9aba1ac893581e86b470065c782f4cc369d5f19
data/lib/rspec-api/dsl.rb CHANGED
@@ -10,10 +10,6 @@ def resource(name, args = {}, &block)
10
10
  describe name, args, &block
11
11
  end
12
12
 
13
- def rspec_api
14
- metadata[:rspec_api]
15
- end
16
-
17
13
  RSpec.configuration.include DSL::Resource, rspec_api_dsl: :resource
18
14
  RSpec.configuration.include DSL::Route, rspec_api_dsl: :route
19
15
  RSpec.configuration.include DSL::Request, rspec_api_dsl: :request
@@ -12,7 +12,7 @@ module DSL
12
12
  end
13
13
 
14
14
  def response_body
15
- JSON response.body
15
+ JSON response_body_without_callbacks
16
16
  rescue JSON::ParserError, JSON::GeneratorError
17
17
  nil
18
18
  end
@@ -58,5 +58,14 @@ module DSL
58
58
  has_entity_body?(status_code) && status_code < 400
59
59
  end
60
60
  end
61
+
62
+ private
63
+
64
+ def response_body_without_callbacks
65
+ body = response.body
66
+ # TODO: extract the 'a_callback' constant
67
+ callback_pattern = %r[a_callback\((.*?)\)]
68
+ body =~ callback_pattern ? body.match(callback_pattern)[1] : body
69
+ end
61
70
  end
62
71
  end
@@ -6,6 +6,7 @@ module DSL
6
6
 
7
7
  module ClassMethods
8
8
  def should_match_body_expectations(status_code, &block)
9
+ should_return_a_jsonp rspec_api[:callback] if rspec_api[:callback]
9
10
  should_return_a_json rspec_api[:array] if success? status_code
10
11
  should_include_attributes rspec_api.fetch(:attributes, {}) if success? status_code
11
12
  if rspec_api[:array]
@@ -18,6 +19,16 @@ module DSL
18
19
 
19
20
  private
20
21
 
22
+ def should_return_a_jsonp(callback_options)
23
+ it {
24
+ if callback_options[:value] == request_params[callback_options[:name].to_s]
25
+ expect(response_body).to be_a_jsonp(callback_options[:value])
26
+ else
27
+ expect(response_body).to be_a_jsonp(nil)
28
+ end
29
+ }
30
+ end
31
+
21
32
  def should_return_a_json(is_array)
22
33
  it { expect(response_body).to be_a_json(is_array ? Array : Hash) }
23
34
  end
@@ -7,7 +7,7 @@ module DSL
7
7
  module ClassMethods
8
8
  def should_match_headers_expectations(status_code)
9
9
  should_have_json_content_type if has_entity_body? status_code
10
- should_be_paginated(rspec_api[:page]) if rspec_api[:array] && rspec_api[:page]
10
+ should_be_paginated(rspec_api[:page][:name]) if rspec_api[:array] && rspec_api[:page]
11
11
  end
12
12
 
13
13
  private
@@ -3,6 +3,10 @@ module DSL
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  module ClassMethods
6
+ def rspec_api
7
+ metadata[:rspec_api]
8
+ end
9
+
6
10
  def self.define_action(verb)
7
11
  define_method verb do |route, args = {}, &block|
8
12
  rspec_api.merge! array: args.delete(:array), verb: verb, route: route
@@ -24,11 +28,11 @@ module DSL
24
28
  end
25
29
 
26
30
  def accepts_page(page_parameter)
27
- rspec_api[:page] = page_parameter
31
+ rspec_api[:page] = {name: page_parameter, value: 2}
28
32
  end
29
33
 
30
34
  def accepts_sort(sort_parameter, options={})
31
- rspec_api[:sort] = {parameter: sort_parameter, attribute: options[:on]}
35
+ rspec_api[:sort] = {name: sort_parameter, attribute: options[:on]}
32
36
  end
33
37
 
34
38
  # TODO: the second 'accepts_filter' should not override the first, but add
@@ -36,6 +40,10 @@ module DSL
36
40
  rspec_api[:filter] = options.merge(name: filter_parameter)
37
41
  end
38
42
 
43
+ def accepts_callback(callback_parameter)
44
+ rspec_api[:callback] = {name: callback_parameter, value: 'a_callback'}
45
+ end
46
+
39
47
  private
40
48
 
41
49
  def nested_attribute(name)
@@ -9,8 +9,8 @@ module DSL
9
9
  module ClassMethods
10
10
  def request(*args, &block)
11
11
  text, values = parse_request_arguments args
12
- extra_parameters.each do |params|
13
- request_with_extra_params text, values.merge(params), &block
12
+ sets_of_parameters.each do |params|
13
+ request_with_params text, values.merge(params), &block
14
14
  end
15
15
  end
16
16
 
@@ -24,7 +24,7 @@ module DSL
24
24
 
25
25
  private
26
26
 
27
- def request_with_extra_params(text, values = {}, &block)
27
+ def request_with_params(text, values = {}, &block)
28
28
  context request_description(text, values), rspec_api_dsl: :request do
29
29
  # NOTE: Having setup_fixtures inside the context sets up different
30
30
  # fixtures for each `request` inside the same `get`. This might be
@@ -38,24 +38,47 @@ module DSL
38
38
  end
39
39
  end
40
40
 
41
- def extra_parameters
42
- [].tap do |optional_params|
43
- optional_params << {} # default: no extra params
41
+ def sets_of_parameters
42
+ [].tap do |sets_of_params|
43
+ sets_of_params.push no_params
44
+ sets_of_params.push callback_params if rspec_api[:callback]
44
45
  if rspec_api[:array]
45
- if sort = rspec_api[:sort]
46
- optional_params << {sort: sort[:parameter]}
47
- optional_params << {sort: "-#{sort[:parameter]}"}
48
- end
49
- if rspec_api[:page]
50
- optional_params << {page: 2}
51
- end
52
- if filter = rspec_api[:filter]
53
- optional_params << {filter[:name] => existing(filter[:on])}
54
- end
46
+ sets_of_params.push sort_params(verse: :asc) if rspec_api[:sort]
47
+ sets_of_params.push sort_params(verse: :desc) if rspec_api[:sort]
48
+ sets_of_params.push page_params if rspec_api[:page]
49
+ sets_of_params.push filter_params if rspec_api[:filter]
55
50
  end
56
51
  end
57
52
  end
58
53
 
54
+ def no_params
55
+ {} # always send the original request without extra parameters
56
+ end
57
+
58
+ def sort_params(options = {})
59
+ ascending = options[:verse] == :asc
60
+ sort = rspec_api[:sort][:name]
61
+ {sort: ascending ? "#{sort}" : "-#{sort}"}
62
+ end
63
+
64
+ def page_params
65
+ {}.tap do |params|
66
+ params[rspec_api[:page][:name]] = rspec_api[:page][:value]
67
+ end
68
+ end
69
+
70
+ def filter_params
71
+ {}.tap do |params|
72
+ params[rspec_api[:filter][:name]] = existing rspec_api[:filter][:on]
73
+ end
74
+ end
75
+
76
+ def callback_params
77
+ {}.tap do |params|
78
+ params[rspec_api[:callback][:name]] = rspec_api[:callback][:value]
79
+ end
80
+ end
81
+
59
82
  def setup_request(verb, route, values)
60
83
  request = Proc.new {
61
84
  interpolated_route, body = route.dup, values.dup
@@ -6,4 +6,5 @@ require_relative 'matchers/attributes'
6
6
  require_relative 'matchers/fixtures'
7
7
  require_relative 'matchers/page'
8
8
  require_relative 'matchers/sort'
9
- require_relative 'matchers/filter'
9
+ require_relative 'matchers/filter'
10
+ require_relative 'matchers/jsonp'
@@ -0,0 +1,17 @@
1
+ RSpec::Matchers.define :be_a_jsonp do |callback_name|
2
+ match do |response_body|
3
+ if callback_name.nil?
4
+ true
5
+ else
6
+ response.body =~ %r[^#{callback_name}\((.*?)\)$]
7
+ end
8
+ end
9
+
10
+ description do
11
+ %Q(be a JSONP callback)
12
+ end
13
+
14
+ failure_message_for_should do |response_body|
15
+ %Q(should #{description}, but is #{response_body})
16
+ end
17
+ end
@@ -3,7 +3,7 @@ RSpec::Matchers.define :have_pagination_links do |page|
3
3
  if page.nil?
4
4
  true
5
5
  else
6
- links = response_headers['Link'] || '' # https://github.com/lostisland/faraday/pull/306
6
+ links = response_headers['Link'] || '' # see http://git.io/CUz3-Q
7
7
  rels = links.split(',').map{|link| link[/<.+?>; rel="(.*)"$/, 1]}
8
8
  rels.sort == ['first', 'prev']
9
9
  end
@@ -16,5 +16,4 @@ RSpec::Matchers.define :have_pagination_links do |page|
16
16
  failure_message_for_should do |response_headers|
17
17
  %Q(should #{description}, but are #{response_headers})
18
18
  end
19
- end
20
-
19
+ end
@@ -1,3 +1,3 @@
1
1
  module RspecApi
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - claudiob
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-11 00:00:00.000000000 Z
11
+ date: 2013-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -80,6 +80,7 @@ files:
80
80
  - lib/rspec-api/matchers/fields.rb
81
81
  - lib/rspec-api/matchers/filter.rb
82
82
  - lib/rspec-api/matchers/fixtures.rb
83
+ - lib/rspec-api/matchers/jsonp.rb
83
84
  - lib/rspec-api/matchers/page.rb
84
85
  - lib/rspec-api/matchers/sort.rb
85
86
  - lib/rspec-api/matchers/status.rb