rspec-api-matchers 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +24 -19
  3. data/lib/rspec-api/matchers.rb +38 -21
  4. data/lib/rspec-api/matchers/attributes/have_attributes.rb +23 -0
  5. data/lib/rspec-api/matchers/attributes/matcher.rb +101 -0
  6. data/lib/rspec-api/matchers/collection/be_a_collection.rb +20 -0
  7. data/lib/rspec-api/matchers/collection/matcher.rb +19 -0
  8. data/lib/rspec-api/matchers/content_type/have_content_type.rb +24 -0
  9. data/lib/rspec-api/matchers/content_type/matcher.rb +23 -0
  10. data/lib/rspec-api/matchers/filter/be_filtered.rb +20 -0
  11. data/lib/rspec-api/matchers/filter/matcher.rb +57 -0
  12. data/lib/rspec-api/matchers/headers/have_headers.rb +20 -0
  13. data/lib/rspec-api/matchers/headers/matcher.rb +28 -0
  14. data/lib/rspec-api/matchers/json/be_valid_json.rb +20 -0
  15. data/lib/rspec-api/matchers/json/matcher.rb +41 -0
  16. data/lib/rspec-api/matchers/jsonp/be_wrapped_in_callback.rb +24 -0
  17. data/lib/rspec-api/matchers/jsonp/matcher.rb +23 -0
  18. data/lib/rspec-api/matchers/page_links/have_page_links.rb +25 -0
  19. data/lib/rspec-api/matchers/page_links/matcher.rb +18 -0
  20. data/lib/rspec-api/matchers/response/be_a_valid_response.rb +20 -0
  21. data/lib/rspec-api/matchers/response/matcher.rb +52 -0
  22. data/lib/rspec-api/matchers/sort/be_sorted.rb +20 -0
  23. data/lib/rspec-api/matchers/sort/matcher.rb +56 -0
  24. data/lib/rspec-api/matchers/status/have_status.rb +25 -0
  25. data/lib/rspec-api/matchers/status/matcher.rb +46 -0
  26. data/lib/rspec-api/matchers/version.rb +1 -1
  27. metadata +24 -18
  28. data/lib/rspec-api/dsl/be_a_collection.rb +0 -24
  29. data/lib/rspec-api/dsl/be_a_jsonp.rb +0 -24
  30. data/lib/rspec-api/dsl/be_filtered.rb +0 -24
  31. data/lib/rspec-api/dsl/be_sorted.rb +0 -24
  32. data/lib/rspec-api/dsl/have_attributes.rb +0 -37
  33. data/lib/rspec-api/dsl/have_prev_page_link.rb +0 -20
  34. data/lib/rspec-api/dsl/have_status.rb +0 -20
  35. data/lib/rspec-api/dsl/include_content_type.rb +0 -24
  36. data/lib/rspec-api/matchers/attributes.rb +0 -171
  37. data/lib/rspec-api/matchers/collection.rb +0 -42
  38. data/lib/rspec-api/matchers/content_type.rb +0 -26
  39. data/lib/rspec-api/matchers/filter.rb +0 -58
  40. data/lib/rspec-api/matchers/jsonp.rb +0 -27
  41. data/lib/rspec-api/matchers/prev_page_link.rb +0 -23
  42. data/lib/rspec-api/matchers/sort.rb +0 -65
  43. data/lib/rspec-api/matchers/status.rb +0 -60
@@ -1,58 +0,0 @@
1
- require 'json'
2
-
3
- module RSpecApi
4
- module Matchers
5
- class Filter
6
- def initialize(value, options = {})
7
- @value = value
8
- @field = options[:by]
9
- @comparing_with = options[:comparing_with]
10
- end
11
-
12
- def matches?(response)
13
- @desc = " by #{@field}#{@comparing_with || '='}#{@value}"
14
- @body = response.body
15
- array = extract_json_from @body
16
- array.all? do |item| # TODO: Don't always use string
17
- if @comparing_with
18
- @comparing_with.call @value, item[@field.to_s].to_s
19
- else
20
- @value.to_s == item[@field.to_s].to_s
21
- end
22
- end
23
- end
24
- alias == matches?
25
-
26
- def failure_message_for_should
27
- "expected body to #{description}, but got #{@body}"
28
- end
29
-
30
- def failure_message_for_should_not
31
- "expected body not to #{description}, but it was"
32
- end
33
-
34
- def description
35
- %Q(be filtered#{@desc})
36
- end
37
-
38
- private
39
-
40
- # These go elsewhere
41
-
42
- def extract_json_from(something)
43
- array = JSON without_callbacks(something)
44
- if array.is_a?(Array) and array.empty?
45
- raise RSpec::Core::Pending::PendingDeclaredInExample.new "You are testing if an array is filtered, but the array is empty. Try with more fixtures"
46
- end
47
- array
48
- rescue JSON::ParserError, JSON::GeneratorError
49
- raise RSpec::Core::Pending::PendingDeclaredInExample.new "You are testing if an array is filtered, but the array is empty. Try with more fixtures"
50
- end
51
-
52
- def without_callbacks(something)
53
- callback_pattern = %r[^.+?\((.*?)\)$]
54
- something =~ callback_pattern ? something.match(callback_pattern)[1] : something
55
- end
56
- end
57
- end
58
- end
@@ -1,27 +0,0 @@
1
- module RSpecApi
2
- module Matchers
3
- class Jsonp
4
- def initialize(callback)
5
- @callback = callback
6
- end
7
-
8
- def matches?(response)
9
- @body = response.body
10
- @body =~ %r{^#{@callback || '.+?'}\((.*?)\)$}
11
- end
12
- alias == matches?
13
-
14
- def failure_message_for_should
15
- "expected body to #{description}, but got #{@body}"
16
- end
17
-
18
- def failure_message_for_should_not
19
- "expected body not to #{description}, but it was"
20
- end
21
-
22
- def description
23
- %Q(be wrapped in a JSONP callback #{@callback}).rstrip
24
- end
25
- end
26
- end
27
- end
@@ -1,23 +0,0 @@
1
- module RSpecApi
2
- module Matchers
3
- class PrevPageLink
4
- def matches?(response)
5
- @headers = response.headers
6
- links = @headers['Link'] || '' # not fetch, see http://git.io/CUz3-Q
7
- links =~ %r{<.+?>. rel\="prev"}
8
- end
9
-
10
- def failure_message_for_should
11
- "expected headers to #{description}, but got #{@headers}"
12
- end
13
-
14
- def failure_message_for_should_not
15
- "expected headers not to #{description}, but got #{@headers}"
16
- end
17
-
18
- def description
19
- %Q{include a 'Link' to the previous page}
20
- end
21
- end
22
- end
23
- end
@@ -1,65 +0,0 @@
1
- require 'json'
2
-
3
- module RSpecApi
4
- module Matchers
5
- class Sort
6
- def initialize(options = {})
7
- @field = options[:by]
8
- @reverse = options[:verse].to_s == 'desc' || options[:verse].to_s == 'descending' || options[:reverse] == true || options[:ascending] == true || options[:asc] == true || options[:descending] == false || options[:desc] == false
9
- end
10
-
11
- def matches?(response)
12
- # If we don't get which field the body should be sorted by, then we
13
- # say that it's sorted. For instance sort by random... no expectations
14
- # We might still want to do some check about being a JSON array, though
15
- if @field.nil?
16
- true
17
- else
18
- @desc = " by #{'descending ' if @reverse}#{@field}" # TODO: use arrows
19
- @body = response.body
20
- array = extract_array of: @field, from: @body # what if this fails?
21
- is_sorted? array, @reverse
22
- end
23
- end
24
- alias == matches?
25
-
26
- def failure_message_for_should
27
- # NOTE: might just print the (unsorted) fields, not the whole @body
28
- "expected body to #{description}, but got #{@body}"
29
- end
30
-
31
- def failure_message_for_should_not
32
- "expected body not to #{description}, but it was"
33
- end
34
-
35
- def description
36
- %Q(be sorted#{@desc})
37
- end
38
-
39
- private
40
-
41
- def is_sorted?(array, reverse)
42
- return false unless array.is_a?(Array)
43
- array.reverse! if reverse
44
- array == array.sort
45
- end
46
-
47
- # These go elsewhere
48
-
49
- def extract_array(options = {})
50
- array = JSON without_callbacks(options[:from])
51
- if array.is_a?(Array) and array.empty?
52
- raise RSpec::Core::Pending::PendingDeclaredInExample.new "You are testing if an array is sorted, but the array is empty. Try with more fixtures"
53
- end
54
- array.map{|item| item[options[:of].to_s]} # what if it's not an array of hashes?
55
- rescue JSON::ParserError, JSON::GeneratorError
56
- raise RSpec::Core::Pending::PendingDeclaredInExample.new "You are testing if an array is sorted, but the array is empty. Try with more fixtures"
57
- end
58
-
59
- def without_callbacks(something)
60
- callback_pattern = %r[^.+?\((.*?)\)$]
61
- something =~ callback_pattern ? something.match(callback_pattern)[1] : something
62
- end
63
- end
64
- end
65
- end
@@ -1,60 +0,0 @@
1
- require 'rack/utils'
2
-
3
- module RSpecApi
4
- module Matchers
5
- class Status
6
- def initialize(status)
7
- @expected_status = status
8
- end
9
-
10
- def matches?(response)
11
- @status = response.status
12
- @status == expected_code
13
- end
14
- alias == matches?
15
-
16
- def failure_message_for_should
17
- "expected HTTP status code #{expected_code}, got #{@status}"
18
- end
19
-
20
- def failure_message_for_should_not
21
- "expected HTTP status code not to be #{expected_code}, but it was"
22
- end
23
-
24
- def description
25
- "be HTTP status code #{expected_code}"
26
- end
27
-
28
- private
29
-
30
- def expected_code
31
- status_to_numeric_code @expected_status
32
- end
33
-
34
- # Translates an HTTP status symbol or code into the corresponding code
35
- #
36
- # @example
37
- # status_to_code(:ok) # => 200
38
- # status_to_code(200) # => 200
39
- # status_to_code(987) # => raise ArgumentError
40
- def status_to_numeric_code(status)
41
- code = status.is_a?(Symbol) ? Rack::Utils.status_code(status) : status
42
- validate_status_code! code
43
- code
44
- end
45
-
46
- # Raises an exception if +numeric_code+ is not a valid HTTP status code
47
- #
48
- # @example
49
- # validate_status_code!(200) # => (no error)
50
- # validate_status_code!(999) # => raise ArgumentError
51
- def validate_status_code!(code)
52
- valid_codes = Rack::Utils::SYMBOL_TO_STATUS_CODE.values
53
- message = "#{code} must be a valid HTTP status code: #{valid_codes}"
54
- unless valid_codes.include? code
55
- raise ArgumentError, message
56
- end
57
- end
58
- end
59
- end
60
- end