api_matchers 0.6.2 → 1.0.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 (116) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/ci.yml +30 -0
  3. data/.gitignore +1 -0
  4. data/Gemfile +1 -1
  5. data/History.markdown +62 -50
  6. data/README.markdown +1070 -114
  7. data/TODO.markdown +39 -3
  8. data/api_matchers.gemspec +13 -6
  9. data/lib/api_matchers/collection/base.rb +65 -0
  10. data/lib/api_matchers/collection/be_sorted_by.rb +97 -0
  11. data/lib/api_matchers/collection/have_json_size.rb +54 -0
  12. data/lib/api_matchers/core/exceptions.rb +5 -0
  13. data/lib/api_matchers/core/find_in_json.rb +57 -28
  14. data/lib/api_matchers/core/http_status_codes.rb +118 -0
  15. data/lib/api_matchers/core/json_path_finder.rb +87 -0
  16. data/lib/api_matchers/core/parser.rb +4 -2
  17. data/lib/api_matchers/core/rspec_matchers.rb +130 -37
  18. data/lib/api_matchers/core/setup.rb +49 -23
  19. data/lib/api_matchers/core/value_normalizer.rb +26 -0
  20. data/lib/api_matchers/error_response/base.rb +77 -0
  21. data/lib/api_matchers/error_response/have_error.rb +69 -0
  22. data/lib/api_matchers/error_response/have_error_on.rb +173 -0
  23. data/lib/api_matchers/hateoas/base.rb +77 -0
  24. data/lib/api_matchers/hateoas/have_link.rb +102 -0
  25. data/lib/api_matchers/headers/base.rb +7 -7
  26. data/lib/api_matchers/headers/be_json.rb +2 -4
  27. data/lib/api_matchers/headers/be_xml.rb +2 -4
  28. data/lib/api_matchers/headers/have_cache_control.rb +90 -0
  29. data/lib/api_matchers/headers/have_cors_headers.rb +102 -0
  30. data/lib/api_matchers/headers/have_header.rb +102 -0
  31. data/lib/api_matchers/http_status/base.rb +48 -0
  32. data/lib/api_matchers/http_status/be_client_error.rb +17 -0
  33. data/lib/api_matchers/http_status/be_forbidden.rb +17 -0
  34. data/lib/api_matchers/http_status/be_no_content.rb +17 -0
  35. data/lib/api_matchers/http_status/be_not_found.rb +17 -0
  36. data/lib/api_matchers/http_status/be_redirect.rb +17 -0
  37. data/lib/api_matchers/http_status/be_server_error.rb +17 -0
  38. data/lib/api_matchers/http_status/be_successful.rb +17 -0
  39. data/lib/api_matchers/http_status/be_unauthorized.rb +17 -0
  40. data/lib/api_matchers/http_status/be_unprocessable.rb +17 -0
  41. data/lib/api_matchers/http_status/have_http_status.rb +39 -0
  42. data/lib/api_matchers/json_api/base.rb +83 -0
  43. data/lib/api_matchers/json_api/be_json_api_compliant.rb +158 -0
  44. data/lib/api_matchers/json_api/have_json_api_attributes.rb +62 -0
  45. data/lib/api_matchers/json_api/have_json_api_data.rb +110 -0
  46. data/lib/api_matchers/json_api/have_json_api_relationships.rb +62 -0
  47. data/lib/api_matchers/json_structure/base.rb +65 -0
  48. data/lib/api_matchers/json_structure/have_json_keys.rb +55 -0
  49. data/lib/api_matchers/json_structure/have_json_type.rb +72 -0
  50. data/lib/api_matchers/pagination/base.rb +73 -0
  51. data/lib/api_matchers/pagination/be_paginated.rb +73 -0
  52. data/lib/api_matchers/pagination/have_pagination_links.rb +74 -0
  53. data/lib/api_matchers/pagination/have_total_count.rb +77 -0
  54. data/lib/api_matchers/response_body/base.rb +10 -9
  55. data/lib/api_matchers/response_body/have_json.rb +2 -4
  56. data/lib/api_matchers/response_body/have_json_node.rb +45 -1
  57. data/lib/api_matchers/response_body/have_node.rb +2 -0
  58. data/lib/api_matchers/response_body/have_xml_node.rb +13 -14
  59. data/lib/api_matchers/response_body/match_json_schema.rb +89 -0
  60. data/lib/api_matchers/version.rb +3 -1
  61. data/lib/api_matchers.rb +75 -14
  62. data/spec/api_matchers/collection/be_sorted_by_spec.rb +110 -0
  63. data/spec/api_matchers/collection/have_json_size_spec.rb +101 -0
  64. data/spec/api_matchers/error_response/have_error_on_spec.rb +123 -0
  65. data/spec/api_matchers/error_response/have_error_spec.rb +108 -0
  66. data/spec/api_matchers/hateoas/have_link_spec.rb +105 -0
  67. data/spec/api_matchers/headers/base_spec.rb +8 -3
  68. data/spec/api_matchers/headers/be_json_spec.rb +1 -1
  69. data/spec/api_matchers/headers/be_xml_spec.rb +1 -1
  70. data/spec/api_matchers/headers/have_cache_control_spec.rb +102 -0
  71. data/spec/api_matchers/headers/have_cors_headers_spec.rb +74 -0
  72. data/spec/api_matchers/headers/have_header_spec.rb +88 -0
  73. data/spec/api_matchers/http_status/be_client_error_spec.rb +53 -0
  74. data/spec/api_matchers/http_status/be_forbidden_spec.rb +33 -0
  75. data/spec/api_matchers/http_status/be_no_content_spec.rb +33 -0
  76. data/spec/api_matchers/http_status/be_not_found_spec.rb +39 -0
  77. data/spec/api_matchers/http_status/be_redirect_spec.rb +55 -0
  78. data/spec/api_matchers/http_status/be_server_error_spec.rb +49 -0
  79. data/spec/api_matchers/http_status/be_successful_spec.rb +78 -0
  80. data/spec/api_matchers/http_status/be_unauthorized_spec.rb +33 -0
  81. data/spec/api_matchers/http_status/be_unprocessable_spec.rb +39 -0
  82. data/spec/api_matchers/http_status/have_http_status_spec.rb +81 -0
  83. data/spec/api_matchers/json_api/be_json_api_compliant_spec.rb +109 -0
  84. data/spec/api_matchers/json_api/have_json_api_attributes_spec.rb +61 -0
  85. data/spec/api_matchers/json_api/have_json_api_data_spec.rb +95 -0
  86. data/spec/api_matchers/json_api/have_json_api_relationships_spec.rb +61 -0
  87. data/spec/api_matchers/json_structure/have_json_keys_spec.rb +81 -0
  88. data/spec/api_matchers/json_structure/have_json_type_spec.rb +134 -0
  89. data/spec/api_matchers/pagination/be_paginated_spec.rb +95 -0
  90. data/spec/api_matchers/pagination/have_pagination_links_spec.rb +80 -0
  91. data/spec/api_matchers/pagination/have_total_count_spec.rb +85 -0
  92. data/spec/api_matchers/response_body/base_spec.rb +15 -7
  93. data/spec/api_matchers/response_body/have_json_node_spec.rb +57 -0
  94. data/spec/api_matchers/response_body/match_json_schema_spec.rb +86 -0
  95. metadata +152 -47
  96. data/.rvmrc.example +0 -1
  97. data/.travis.yml +0 -12
  98. data/Gemfile.lock +0 -46
  99. data/lib/api_matchers/http_status_code/base.rb +0 -32
  100. data/lib/api_matchers/http_status_code/be_bad_request.rb +0 -25
  101. data/lib/api_matchers/http_status_code/be_forbidden.rb +0 -21
  102. data/lib/api_matchers/http_status_code/be_internal_server_error.rb +0 -25
  103. data/lib/api_matchers/http_status_code/be_not_found.rb +0 -25
  104. data/lib/api_matchers/http_status_code/be_ok.rb +0 -25
  105. data/lib/api_matchers/http_status_code/be_unauthorized.rb +0 -25
  106. data/lib/api_matchers/http_status_code/be_unprocessable_entity.rb +0 -25
  107. data/lib/api_matchers/http_status_code/create_resource.rb +0 -25
  108. data/spec/api_matchers/http_status_code/base_spec.rb +0 -12
  109. data/spec/api_matchers/http_status_code/be_bad_request_spec.rb +0 -49
  110. data/spec/api_matchers/http_status_code/be_forbidden_spec.rb +0 -49
  111. data/spec/api_matchers/http_status_code/be_internal_server_error_spec.rb +0 -49
  112. data/spec/api_matchers/http_status_code/be_not_found_spec.rb +0 -49
  113. data/spec/api_matchers/http_status_code/be_ok_spec.rb +0 -49
  114. data/spec/api_matchers/http_status_code/be_unauthorized_spec.rb +0 -49
  115. data/spec/api_matchers/http_status_code/be_unprocessable_entity_spec.rb +0 -27
  116. data/spec/api_matchers/http_status_code/create_resource_spec.rb +0 -49
@@ -56,7 +56,7 @@ RSpec.describe APIMatchers::Headers::BeJSON do
56
56
  response = OpenStruct.new(:response_header => { "foo-bar" => "application/xml; charset=utf-8"})
57
57
  expect {
58
58
  expect(response).to be_json
59
- }.to fail_with(%Q{expected a JSON response with 'application/json; charset=utf-8'. Got: '{"foo-bar"=>"application/xml; charset=utf-8"}'.})
59
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected a JSON response with 'application\/json; charset=utf-8'. Got: '\{"foo-bar".*"application\/xml; charset=utf-8"\}'\./)
60
60
  end
61
61
  end
62
62
  end
@@ -56,7 +56,7 @@ RSpec.describe APIMatchers::Headers::BeXML do
56
56
  response = OpenStruct.new(:response_header => { "foo-baz" => "application/json; charset=utf-8"})
57
57
  expect {
58
58
  expect(response).to be_xml
59
- }.to fail_with(%Q{expected a XML response with 'application/xml; charset=utf-8'. Got: '{"foo-baz"=>"application/json; charset=utf-8"}'.})
59
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected a XML response with 'application\/xml; charset=utf-8'. Got: '\{"foo-baz".*"application\/json; charset=utf-8"\}'\./)
60
60
  end
61
61
  end
62
62
  end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::Headers::HaveCacheControl do
4
+ describe "actual.to have_cache_control" do
5
+ context "with single directive" do
6
+ it "passes when directive is present" do
7
+ headers = { 'Cache-Control' => 'no-cache' }
8
+ expect(headers).to have_cache_control(:no_cache)
9
+ end
10
+
11
+ it "fails when directive is not present" do
12
+ headers = { 'Cache-Control' => 'public' }
13
+ expect {
14
+ expect(headers).to have_cache_control(:no_cache)
15
+ }.to fail_with(/expected Cache-Control to include \["no-cache"\]/)
16
+ end
17
+ end
18
+
19
+ context "with multiple directives" do
20
+ it "passes when all directives are present" do
21
+ headers = { 'Cache-Control' => 'private, no-store, max-age=0' }
22
+ expect(headers).to have_cache_control(:private, :no_store)
23
+ end
24
+
25
+ it "fails when some directives are missing" do
26
+ headers = { 'Cache-Control' => 'private' }
27
+ expect {
28
+ expect(headers).to have_cache_control(:private, :no_store)
29
+ }.to fail_with(/expected Cache-Control to include \["private", "no-store"\]/)
30
+ end
31
+ end
32
+
33
+ context "with directives that have values" do
34
+ it "passes when max-age is present" do
35
+ headers = { 'Cache-Control' => 'max-age=3600' }
36
+ expect(headers).to have_cache_control('max-age')
37
+ end
38
+
39
+ it "passes when s-maxage is present" do
40
+ headers = { 'Cache-Control' => 's-maxage=7200, public' }
41
+ expect(headers).to have_cache_control('s-maxage', :public)
42
+ end
43
+ end
44
+
45
+ context "when Cache-Control header is missing" do
46
+ it "fails with descriptive message" do
47
+ headers = { 'Content-Type' => 'application/json' }
48
+ expect {
49
+ expect(headers).to have_cache_control(:no_cache)
50
+ }.to fail_with(/but Cache-Control header was not present/)
51
+ end
52
+ end
53
+
54
+ context "underscore to dash conversion" do
55
+ it "converts underscores to dashes" do
56
+ headers = { 'Cache-Control' => 'no-store, must-revalidate' }
57
+ expect(headers).to have_cache_control(:no_store, :must_revalidate)
58
+ end
59
+ end
60
+
61
+ context "case insensitivity" do
62
+ it "matches directives case-insensitively" do
63
+ headers = { 'Cache-Control' => 'NO-CACHE, PRIVATE' }
64
+ expect(headers).to have_cache_control(:no_cache, :private)
65
+ end
66
+
67
+ it "matches header name case-insensitively" do
68
+ headers = { 'cache-control' => 'no-cache' }
69
+ expect(headers).to have_cache_control(:no_cache)
70
+ end
71
+ end
72
+ end
73
+
74
+ describe "actual.not_to have_cache_control" do
75
+ it "passes when directives are not present" do
76
+ headers = { 'Cache-Control' => 'public' }
77
+ expect(headers).not_to have_cache_control(:private)
78
+ end
79
+
80
+ it "fails when directives are present" do
81
+ headers = { 'Cache-Control' => 'private, no-cache' }
82
+ expect {
83
+ expect(headers).not_to have_cache_control(:private)
84
+ }.to fail_with(/expected Cache-Control NOT to include \["private"\]/)
85
+ end
86
+ end
87
+
88
+ describe "with configuration" do
89
+ before do
90
+ APIMatchers.setup { |config| config.header_method = :response_headers }
91
+ end
92
+
93
+ after do
94
+ APIMatchers.setup { |config| config.header_method = nil }
95
+ end
96
+
97
+ it "extracts headers from response object" do
98
+ response = OpenStruct.new(response_headers: { 'Cache-Control' => 'no-cache' })
99
+ expect(response).to have_cache_control(:no_cache)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::Headers::HaveCorsHeaders do
4
+ describe "actual.to have_cors_headers" do
5
+ context "with required CORS headers" do
6
+ it "passes when Access-Control-Allow-Origin is present" do
7
+ headers = { 'Access-Control-Allow-Origin' => '*' }
8
+ expect(headers).to have_cors_headers
9
+ end
10
+
11
+ it "fails when Access-Control-Allow-Origin is missing" do
12
+ headers = { 'Content-Type' => 'application/json' }
13
+ expect {
14
+ expect(headers).to have_cors_headers
15
+ }.to fail_with(/expected response to have CORS headers. Missing: \["Access-Control-Allow-Origin"\]/)
16
+ end
17
+ end
18
+
19
+ context "with for_origin" do
20
+ it "passes when origin matches" do
21
+ headers = { 'Access-Control-Allow-Origin' => 'https://example.com' }
22
+ expect(headers).to have_cors_headers.for_origin('https://example.com')
23
+ end
24
+
25
+ it "passes when origin is wildcard" do
26
+ headers = { 'Access-Control-Allow-Origin' => '*' }
27
+ expect(headers).to have_cors_headers.for_origin('https://example.com')
28
+ end
29
+
30
+ it "fails when origin does not match" do
31
+ headers = { 'Access-Control-Allow-Origin' => 'https://other.com' }
32
+ expect {
33
+ expect(headers).to have_cors_headers.for_origin('https://example.com')
34
+ }.to fail_with(/expected Access-Control-Allow-Origin to be 'https:\/\/example.com' or '\*'/)
35
+ end
36
+ end
37
+
38
+ context "case insensitivity" do
39
+ it "matches headers case-insensitively" do
40
+ headers = { 'access-control-allow-origin' => '*' }
41
+ expect(headers).to have_cors_headers
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "actual.not_to have_cors_headers" do
47
+ it "passes when CORS headers are not present" do
48
+ headers = { 'Content-Type' => 'application/json' }
49
+ expect(headers).not_to have_cors_headers
50
+ end
51
+
52
+ it "fails when CORS headers are present" do
53
+ headers = { 'Access-Control-Allow-Origin' => '*' }
54
+ expect {
55
+ expect(headers).not_to have_cors_headers
56
+ }.to fail_with(/expected response NOT to have CORS headers, but they were present/)
57
+ end
58
+ end
59
+
60
+ describe "with configuration" do
61
+ before do
62
+ APIMatchers.setup { |config| config.header_method = :response_headers }
63
+ end
64
+
65
+ after do
66
+ APIMatchers.setup { |config| config.header_method = nil }
67
+ end
68
+
69
+ it "extracts headers from response object" do
70
+ response = OpenStruct.new(response_headers: { 'Access-Control-Allow-Origin' => '*' })
71
+ expect(response).to have_cors_headers
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::Headers::HaveHeader do
4
+ describe "actual.to have_header" do
5
+ context "with hash headers" do
6
+ it "passes when header is present" do
7
+ headers = { 'Content-Type' => 'application/json' }
8
+ expect(headers).to have_header('Content-Type')
9
+ end
10
+
11
+ it "fails when header is not present" do
12
+ headers = { 'Content-Type' => 'application/json' }
13
+ expect {
14
+ expect(headers).to have_header('X-Custom-Header')
15
+ }.to fail_with(/expected response to have header 'X-Custom-Header', but it was not present/)
16
+ end
17
+
18
+ it "is case-insensitive" do
19
+ headers = { 'content-type' => 'application/json' }
20
+ expect(headers).to have_header('Content-Type')
21
+ end
22
+ end
23
+
24
+ context "with with_value" do
25
+ it "passes when header has expected value" do
26
+ headers = { 'X-Request-Id' => '12345' }
27
+ expect(headers).to have_header('X-Request-Id').with_value('12345')
28
+ end
29
+
30
+ it "fails when header has different value" do
31
+ headers = { 'X-Request-Id' => '12345' }
32
+ expect {
33
+ expect(headers).to have_header('X-Request-Id').with_value('67890')
34
+ }.to fail_with(/expected header 'X-Request-Id' to have value '67890'. Got: '12345'/)
35
+ end
36
+ end
37
+
38
+ context "with matching (regex)" do
39
+ it "passes when header matches pattern" do
40
+ headers = { 'X-Request-Id' => 'req-12345-abc' }
41
+ expect(headers).to have_header('X-Request-Id').matching(/req-\d+-\w+/)
42
+ end
43
+
44
+ it "fails when header does not match pattern" do
45
+ headers = { 'X-Request-Id' => 'invalid' }
46
+ expect {
47
+ expect(headers).to have_header('X-Request-Id').matching(/req-\d+-\w+/)
48
+ }.to fail_with(/expected header 'X-Request-Id' to match/)
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "actual.not_to have_header" do
54
+ it "passes when header is not present" do
55
+ headers = { 'Content-Type' => 'application/json' }
56
+ expect(headers).not_to have_header('X-Custom-Header')
57
+ end
58
+
59
+ it "fails when header is present" do
60
+ headers = { 'X-Custom-Header' => 'value' }
61
+ expect {
62
+ expect(headers).not_to have_header('X-Custom-Header')
63
+ }.to fail_with(/expected response NOT to have header 'X-Custom-Header', but it was present/)
64
+ end
65
+ end
66
+
67
+ describe "with configuration" do
68
+ before do
69
+ APIMatchers.setup { |config| config.header_method = :response_headers }
70
+ end
71
+
72
+ after do
73
+ APIMatchers.setup { |config| config.header_method = nil }
74
+ end
75
+
76
+ it "extracts headers from response object" do
77
+ response = OpenStruct.new(response_headers: { 'X-Custom' => 'value' })
78
+ expect(response).to have_header('X-Custom')
79
+ end
80
+ end
81
+
82
+ describe "with response object having headers method" do
83
+ it "automatically uses headers method" do
84
+ response = OpenStruct.new(headers: { 'X-Custom' => 'value' })
85
+ expect(response).to have_header('X-Custom')
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::HTTPStatus::BeClientError do
4
+ describe "actual.to be_client_error" do
5
+ it "passes for status 400" do
6
+ expect(400).to be_client_error
7
+ end
8
+
9
+ it "passes for status 401" do
10
+ expect(401).to be_client_error
11
+ end
12
+
13
+ it "passes for status 403" do
14
+ expect(403).to be_client_error
15
+ end
16
+
17
+ it "passes for status 404" do
18
+ expect(404).to be_client_error
19
+ end
20
+
21
+ it "passes for status 422" do
22
+ expect(422).to be_client_error
23
+ end
24
+
25
+ it "passes for status 499" do
26
+ expect(499).to be_client_error
27
+ end
28
+
29
+ it "fails for status 200" do
30
+ expect {
31
+ expect(200).to be_client_error
32
+ }.to fail_with("expected response to be client error (4xx). Got: 200")
33
+ end
34
+
35
+ it "fails for status 500" do
36
+ expect {
37
+ expect(500).to be_client_error
38
+ }.to fail_with("expected response to be client error (4xx). Got: 500")
39
+ end
40
+ end
41
+
42
+ describe "actual.not_to be_client_error" do
43
+ it "passes for non-4xx status" do
44
+ expect(200).not_to be_client_error
45
+ end
46
+
47
+ it "fails for 4xx status" do
48
+ expect {
49
+ expect(404).not_to be_client_error
50
+ }.to fail_with("expected response NOT to be client error (4xx). Got: 404")
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::HTTPStatus::BeForbidden do
4
+ describe "actual.to be_forbidden" do
5
+ it "passes for status 403" do
6
+ expect(403).to be_forbidden
7
+ end
8
+
9
+ it "fails for status 200" do
10
+ expect {
11
+ expect(200).to be_forbidden
12
+ }.to fail_with("expected response to be forbidden (403). Got: 200")
13
+ end
14
+
15
+ it "fails for status 401" do
16
+ expect {
17
+ expect(401).to be_forbidden
18
+ }.to fail_with("expected response to be forbidden (403). Got: 401")
19
+ end
20
+ end
21
+
22
+ describe "actual.not_to be_forbidden" do
23
+ it "passes for non-403 status" do
24
+ expect(200).not_to be_forbidden
25
+ end
26
+
27
+ it "fails for 403 status" do
28
+ expect {
29
+ expect(403).not_to be_forbidden
30
+ }.to fail_with("expected response NOT to be forbidden (403). Got: 403")
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::HTTPStatus::BeNoContent do
4
+ describe "actual.to be_no_content" do
5
+ it "passes for status 204" do
6
+ expect(204).to be_no_content
7
+ end
8
+
9
+ it "fails for status 200" do
10
+ expect {
11
+ expect(200).to be_no_content
12
+ }.to fail_with("expected response to be no content (204). Got: 200")
13
+ end
14
+
15
+ it "fails for status 201" do
16
+ expect {
17
+ expect(201).to be_no_content
18
+ }.to fail_with("expected response to be no content (204). Got: 201")
19
+ end
20
+ end
21
+
22
+ describe "actual.not_to be_no_content" do
23
+ it "passes for non-204 status" do
24
+ expect(200).not_to be_no_content
25
+ end
26
+
27
+ it "fails for 204 status" do
28
+ expect {
29
+ expect(204).not_to be_no_content
30
+ }.to fail_with("expected response NOT to be no content (204). Got: 204")
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::HTTPStatus::BeNotFound do
4
+ describe "actual.to be_not_found" do
5
+ it "passes for status 404" do
6
+ expect(404).to be_not_found
7
+ end
8
+
9
+ it "fails for status 200" do
10
+ expect {
11
+ expect(200).to be_not_found
12
+ }.to fail_with("expected response to be not found (404). Got: 200")
13
+ end
14
+
15
+ it "fails for status 500" do
16
+ expect {
17
+ expect(500).to be_not_found
18
+ }.to fail_with("expected response to be not found (404). Got: 500")
19
+ end
20
+
21
+ it "fails for other 4xx status" do
22
+ expect {
23
+ expect(401).to be_not_found
24
+ }.to fail_with("expected response to be not found (404). Got: 401")
25
+ end
26
+ end
27
+
28
+ describe "actual.not_to be_not_found" do
29
+ it "passes for non-404 status" do
30
+ expect(200).not_to be_not_found
31
+ end
32
+
33
+ it "fails for 404 status" do
34
+ expect {
35
+ expect(404).not_to be_not_found
36
+ }.to fail_with("expected response NOT to be not found (404). Got: 404")
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::HTTPStatus::BeRedirect do
4
+ describe "actual.to be_redirect" do
5
+ it "passes for status 300" do
6
+ expect(300).to be_redirect
7
+ end
8
+
9
+ it "passes for status 301" do
10
+ expect(301).to be_redirect
11
+ end
12
+
13
+ it "passes for status 302" do
14
+ expect(302).to be_redirect
15
+ end
16
+
17
+ it "passes for status 307" do
18
+ expect(307).to be_redirect
19
+ end
20
+
21
+ it "passes for status 308" do
22
+ expect(308).to be_redirect
23
+ end
24
+
25
+ it "fails for status 200" do
26
+ expect {
27
+ expect(200).to be_redirect
28
+ }.to fail_with("expected response to be redirect (3xx). Got: 200")
29
+ end
30
+
31
+ it "fails for status 400" do
32
+ expect {
33
+ expect(400).to be_redirect
34
+ }.to fail_with("expected response to be redirect (3xx). Got: 400")
35
+ end
36
+ end
37
+
38
+ describe "actual.not_to be_redirect" do
39
+ it "passes for non-3xx status" do
40
+ expect(200).not_to be_redirect
41
+ end
42
+
43
+ it "fails for 3xx status" do
44
+ expect {
45
+ expect(302).not_to be_redirect
46
+ }.to fail_with("expected response NOT to be redirect (3xx). Got: 302")
47
+ end
48
+ end
49
+
50
+ describe "alias be_redirection" do
51
+ it "works as alias for be_redirect" do
52
+ expect(301).to be_redirection
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::HTTPStatus::BeServerError do
4
+ describe "actual.to be_server_error" do
5
+ it "passes for status 500" do
6
+ expect(500).to be_server_error
7
+ end
8
+
9
+ it "passes for status 501" do
10
+ expect(501).to be_server_error
11
+ end
12
+
13
+ it "passes for status 502" do
14
+ expect(502).to be_server_error
15
+ end
16
+
17
+ it "passes for status 503" do
18
+ expect(503).to be_server_error
19
+ end
20
+
21
+ it "passes for status 599" do
22
+ expect(599).to be_server_error
23
+ end
24
+
25
+ it "fails for status 200" do
26
+ expect {
27
+ expect(200).to be_server_error
28
+ }.to fail_with("expected response to be server error (5xx). Got: 200")
29
+ end
30
+
31
+ it "fails for status 404" do
32
+ expect {
33
+ expect(404).to be_server_error
34
+ }.to fail_with("expected response to be server error (5xx). Got: 404")
35
+ end
36
+ end
37
+
38
+ describe "actual.not_to be_server_error" do
39
+ it "passes for non-5xx status" do
40
+ expect(200).not_to be_server_error
41
+ end
42
+
43
+ it "fails for 5xx status" do
44
+ expect {
45
+ expect(500).not_to be_server_error
46
+ }.to fail_with("expected response NOT to be server error (5xx). Got: 500")
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,78 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::HTTPStatus::BeSuccessful do
4
+ describe "actual.to be_successful" do
5
+ it "passes for status 200" do
6
+ expect(200).to be_successful
7
+ end
8
+
9
+ it "passes for status 201" do
10
+ expect(201).to be_successful
11
+ end
12
+
13
+ it "passes for status 204" do
14
+ expect(204).to be_successful
15
+ end
16
+
17
+ it "passes for status 299" do
18
+ expect(299).to be_successful
19
+ end
20
+
21
+ it "fails for status 199" do
22
+ expect {
23
+ expect(199).to be_successful
24
+ }.to fail_with("expected response to be successful (2xx). Got: 199")
25
+ end
26
+
27
+ it "fails for status 300" do
28
+ expect {
29
+ expect(300).to be_successful
30
+ }.to fail_with("expected response to be successful (2xx). Got: 300")
31
+ end
32
+
33
+ it "fails for status 404" do
34
+ expect {
35
+ expect(404).to be_successful
36
+ }.to fail_with("expected response to be successful (2xx). Got: 404")
37
+ end
38
+
39
+ it "fails for status 500" do
40
+ expect {
41
+ expect(500).to be_successful
42
+ }.to fail_with("expected response to be successful (2xx). Got: 500")
43
+ end
44
+ end
45
+
46
+ describe "actual.not_to be_successful" do
47
+ it "passes for non-2xx status" do
48
+ expect(404).not_to be_successful
49
+ end
50
+
51
+ it "fails for 2xx status" do
52
+ expect {
53
+ expect(200).not_to be_successful
54
+ }.to fail_with("expected response NOT to be successful (2xx). Got: 200")
55
+ end
56
+ end
57
+
58
+ describe "alias be_success" do
59
+ it "works as alias for be_successful" do
60
+ expect(200).to be_success
61
+ end
62
+ end
63
+
64
+ describe "with configuration" do
65
+ before do
66
+ APIMatchers.setup { |config| config.http_status_method = :code }
67
+ end
68
+
69
+ after do
70
+ APIMatchers.setup { |config| config.http_status_method = nil }
71
+ end
72
+
73
+ it "extracts status from response object" do
74
+ response = OpenStruct.new(code: 200)
75
+ expect(response).to be_successful
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe APIMatchers::HTTPStatus::BeUnauthorized do
4
+ describe "actual.to be_unauthorized" do
5
+ it "passes for status 401" do
6
+ expect(401).to be_unauthorized
7
+ end
8
+
9
+ it "fails for status 200" do
10
+ expect {
11
+ expect(200).to be_unauthorized
12
+ }.to fail_with("expected response to be unauthorized (401). Got: 200")
13
+ end
14
+
15
+ it "fails for status 403" do
16
+ expect {
17
+ expect(403).to be_unauthorized
18
+ }.to fail_with("expected response to be unauthorized (401). Got: 403")
19
+ end
20
+ end
21
+
22
+ describe "actual.not_to be_unauthorized" do
23
+ it "passes for non-401 status" do
24
+ expect(200).not_to be_unauthorized
25
+ end
26
+
27
+ it "fails for 401 status" do
28
+ expect {
29
+ expect(401).not_to be_unauthorized
30
+ }.to fail_with("expected response NOT to be unauthorized (401). Got: 401")
31
+ end
32
+ end
33
+ end