halchemy 1.0.2

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 (53) hide show
  1. checksums.yaml +7 -0
  2. data/.idea/.gitignore +8 -0
  3. data/.idea/halchemy.iml +69 -0
  4. data/.idea/icon.png +0 -0
  5. data/.idea/inspectionProfiles/Project_Default.xml +36 -0
  6. data/.idea/modules.xml +8 -0
  7. data/.idea/vcs.xml +6 -0
  8. data/.rubocop.yml +8 -0
  9. data/CHANGELOG.md +5 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +43 -0
  12. data/Rakefile +12 -0
  13. data/__tests__/common.rb +97 -0
  14. data/__tests__/configurable/base_url.rb +46 -0
  15. data/__tests__/configurable/error_handling.rb +75 -0
  16. data/__tests__/configurable/headers.rb +92 -0
  17. data/__tests__/make_http_requests/follow_link_relations.rb +86 -0
  18. data/__tests__/make_http_requests/http_response_details.rb +60 -0
  19. data/__tests__/make_http_requests/optimistic_concurrency.rb +38 -0
  20. data/__tests__/make_http_requests/with_headers.rb +32 -0
  21. data/__tests__/make_http_requests/with_parameters.rb +32 -0
  22. data/__tests__/make_http_requests/with_templates.rb +45 -0
  23. data/__tests__/use_resources/iterate_collections.rb +89 -0
  24. data/bdd +1 -0
  25. data/lib/halchemy/api.rb +150 -0
  26. data/lib/halchemy/error_handling.rb +13 -0
  27. data/lib/halchemy/follower.rb +20 -0
  28. data/lib/halchemy/http_model.rb +41 -0
  29. data/lib/halchemy/metadata.rb +20 -0
  30. data/lib/halchemy/requester.rb +181 -0
  31. data/lib/halchemy/resource.rb +68 -0
  32. data/lib/halchemy/status_codes.rb +60 -0
  33. data/lib/halchemy/version.rb +5 -0
  34. data/lib/halchemy.rb +8 -0
  35. data/sig/__tests__/base_url.rbs +1 -0
  36. data/sig/__tests__/halchemy.rbs +4 -0
  37. data/sig/__tests__/headers.rbs +1 -0
  38. data/sig/__tests__/remove_headers.rbs +1 -0
  39. data/sig/halchemy/api.rbs +54 -0
  40. data/sig/halchemy/base_requester.rbs +35 -0
  41. data/sig/halchemy/error_handling.rbs +6 -0
  42. data/sig/halchemy/follower.rbs +9 -0
  43. data/sig/halchemy/hal_resource.rbs +13 -0
  44. data/sig/halchemy/http_model/request.rbs +14 -0
  45. data/sig/halchemy/http_model/response.rbs +15 -0
  46. data/sig/halchemy/metadata.rbs +9 -0
  47. data/sig/halchemy/read_only_requester.rbs +9 -0
  48. data/sig/halchemy/requester.rbs +18 -0
  49. data/sig/halchemy/resource.rbs +5 -0
  50. data/sig/list_style_handlers.rbs +1 -0
  51. data/sig/matchers.rbs +1 -0
  52. data/sig/patterns.rbs +1 -0
  53. metadata +93 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9624f3662fe7646de50ba70ca2606d3ff468e242c4ddfd6c9aaafff3bf249065
4
+ data.tar.gz: 76e2f1871a5ca28140b659561c6eae2ae8f54d29a13ae0d71a6ff06c10c0c069
5
+ SHA512:
6
+ metadata.gz: ef7bf7ea4b23440e81dab6487ba8e39405b45d971fdf83f7493bc575d66ff16280f31315febcbb9257303f8645d3d0761bebb1c2e21f392d80193c7d0ba3011a
7
+ data.tar.gz: 734e615c1f71ac9aa3e46061b2c8703b787f2c812004b41ffc3a0e10df14e74ac50bae48ec575ce1f5351b49f080813a56bb30a02323bed5557206d8462d8ad1
data/.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
@@ -0,0 +1,69 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="RUBY_MODULE" version="4">
3
+ <component name="ModuleRunConfigurationManager">
4
+ <shared />
5
+ </component>
6
+ <component name="NewModuleRootManager">
7
+ <content url="file://$MODULE_DIR$/../../features">
8
+ <sourceFolder url="file://$MODULE_DIR$/../../features" isTestSource="true" />
9
+ </content>
10
+ <content url="file://$MODULE_DIR$">
11
+ <sourceFolder url="file://$MODULE_DIR$/features" isTestSource="true" />
12
+ <sourceFolder url="file://$MODULE_DIR$/spec" isTestSource="true" />
13
+ <sourceFolder url="file://$MODULE_DIR$/__tests__" isTestSource="true" />
14
+ </content>
15
+ <orderEntry type="jdk" jdkName="RVM: ruby-3.4.1" jdkType="RUBY_SDK" />
16
+ <orderEntry type="sourceFolder" forTests="false" />
17
+ <orderEntry type="library" scope="PROVIDED" name="addressable (v2.8.7, RVM: ruby-3.4.1) [gem]" level="application" />
18
+ <orderEntry type="library" scope="PROVIDED" name="ast (v2.4.2, RVM: ruby-3.4.1) [gem]" level="application" />
19
+ <orderEntry type="library" scope="PROVIDED" name="bigdecimal (v3.1.9, RVM: ruby-3.4.1) [gem]" level="application" />
20
+ <orderEntry type="library" scope="PROVIDED" name="builder (v3.3.0, RVM: ruby-3.4.1) [gem]" level="application" />
21
+ <orderEntry type="library" scope="PROVIDED" name="bundler (v2.6.2, RVM: ruby-3.4.1) [gem]" level="application" />
22
+ <orderEntry type="library" scope="PROVIDED" name="cicphash (v2.0.0, RVM: ruby-3.4.1) [gem]" level="application" />
23
+ <orderEntry type="library" scope="PROVIDED" name="crack (v1.0.0, RVM: ruby-3.4.1) [gem]" level="application" />
24
+ <orderEntry type="library" scope="PROVIDED" name="cucumber (v9.2.1, RVM: ruby-3.4.1) [gem]" level="application" />
25
+ <orderEntry type="library" scope="PROVIDED" name="cucumber-ci-environment (v10.0.1, RVM: ruby-3.4.1) [gem]" level="application" />
26
+ <orderEntry type="library" scope="PROVIDED" name="cucumber-core (v13.0.3, RVM: ruby-3.4.1) [gem]" level="application" />
27
+ <orderEntry type="library" scope="PROVIDED" name="cucumber-cucumber-expressions (v17.1.0, RVM: ruby-3.4.1) [gem]" level="application" />
28
+ <orderEntry type="library" scope="PROVIDED" name="cucumber-gherkin (v27.0.0, RVM: ruby-3.4.1) [gem]" level="application" />
29
+ <orderEntry type="library" scope="PROVIDED" name="cucumber-html-formatter (v21.7.0, RVM: ruby-3.4.1) [gem]" level="application" />
30
+ <orderEntry type="library" scope="PROVIDED" name="cucumber-messages (v22.0.0, RVM: ruby-3.4.1) [gem]" level="application" />
31
+ <orderEntry type="library" scope="PROVIDED" name="cucumber-tag-expressions (v6.1.1, RVM: ruby-3.4.1) [gem]" level="application" />
32
+ <orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.5.1, RVM: ruby-3.4.1) [gem]" level="application" />
33
+ <orderEntry type="library" scope="PROVIDED" name="ffi (v1.17.1, RVM: ruby-3.4.1) [gem]" level="application" />
34
+ <orderEntry type="library" scope="PROVIDED" name="hashdiff (v1.1.2, RVM: ruby-3.4.1) [gem]" level="application" />
35
+ <orderEntry type="library" scope="PROVIDED" name="http-2 (v1.0.2, RVM: ruby-3.4.1) [gem]" level="application" />
36
+ <orderEntry type="library" scope="PROVIDED" name="http_status_codes (v0.1.0, RVM: ruby-3.4.1) [gem]" level="application" />
37
+ <orderEntry type="library" scope="PROVIDED" name="httpx (v1.4.0, RVM: ruby-3.4.1) [gem]" level="application" />
38
+ <orderEntry type="library" scope="PROVIDED" name="json (v2.9.1, RVM: ruby-3.4.1) [gem]" level="application" />
39
+ <orderEntry type="library" scope="PROVIDED" name="language_server-protocol (v3.17.0.3, RVM: ruby-3.4.1) [gem]" level="application" />
40
+ <orderEntry type="library" scope="PROVIDED" name="logger (v1.6.5, RVM: ruby-3.4.1) [gem]" level="application" />
41
+ <orderEntry type="library" scope="PROVIDED" name="mini_mime (v1.1.5, RVM: ruby-3.4.1) [gem]" level="application" />
42
+ <orderEntry type="library" scope="PROVIDED" name="multi_test (v1.1.0, RVM: ruby-3.4.1) [gem]" level="application" />
43
+ <orderEntry type="library" scope="PROVIDED" name="parallel (v1.26.3, RVM: ruby-3.4.1) [gem]" level="application" />
44
+ <orderEntry type="library" scope="PROVIDED" name="parser (v3.3.7.0, RVM: ruby-3.4.1) [gem]" level="application" />
45
+ <orderEntry type="library" scope="PROVIDED" name="public_suffix (v6.0.1, RVM: ruby-3.4.1) [gem]" level="application" />
46
+ <orderEntry type="library" scope="PROVIDED" name="racc (v1.8.1, RVM: ruby-3.4.1) [gem]" level="application" />
47
+ <orderEntry type="library" scope="PROVIDED" name="rainbow (v3.1.1, RVM: ruby-3.4.1) [gem]" level="application" />
48
+ <orderEntry type="library" scope="PROVIDED" name="rake (v13.2.1, RVM: ruby-3.4.1) [gem]" level="application" />
49
+ <orderEntry type="library" scope="PROVIDED" name="regexp_parser (v2.10.0, RVM: ruby-3.4.1) [gem]" level="application" />
50
+ <orderEntry type="library" scope="PROVIDED" name="rexml (v3.4.0, RVM: ruby-3.4.1) [gem]" level="application" />
51
+ <orderEntry type="library" scope="PROVIDED" name="rspec (v3.13.0, RVM: ruby-3.4.1) [gem]" level="application" />
52
+ <orderEntry type="library" scope="PROVIDED" name="rspec-core (v3.13.2, RVM: ruby-3.4.1) [gem]" level="application" />
53
+ <orderEntry type="library" scope="PROVIDED" name="rspec-expectations (v3.13.3, RVM: ruby-3.4.1) [gem]" level="application" />
54
+ <orderEntry type="library" scope="PROVIDED" name="rspec-mocks (v3.13.2, RVM: ruby-3.4.1) [gem]" level="application" />
55
+ <orderEntry type="library" scope="PROVIDED" name="rspec-support (v3.13.2, RVM: ruby-3.4.1) [gem]" level="application" />
56
+ <orderEntry type="library" scope="PROVIDED" name="rubocop (v1.70.0, RVM: ruby-3.4.1) [gem]" level="application" />
57
+ <orderEntry type="library" scope="PROVIDED" name="rubocop-ast (v1.37.0, RVM: ruby-3.4.1) [gem]" level="application" />
58
+ <orderEntry type="library" scope="PROVIDED" name="ruby-progressbar (v1.13.0, RVM: ruby-3.4.1) [gem]" level="application" />
59
+ <orderEntry type="library" scope="PROVIDED" name="sorbet-runtime (v0.5.11780, RVM: ruby-3.4.1) [gem]" level="application" />
60
+ <orderEntry type="library" scope="PROVIDED" name="sys-uname (v1.3.1, RVM: ruby-3.4.1) [gem]" level="application" />
61
+ <orderEntry type="library" scope="PROVIDED" name="unicode-display_width (v3.1.4, RVM: ruby-3.4.1) [gem]" level="application" />
62
+ <orderEntry type="library" scope="PROVIDED" name="unicode-emoji (v4.0.4, RVM: ruby-3.4.1) [gem]" level="application" />
63
+ <orderEntry type="library" scope="PROVIDED" name="uri_template (v0.7.0, RVM: ruby-3.4.1) [gem]" level="application" />
64
+ <orderEntry type="library" scope="PROVIDED" name="webmock (v3.24.0, RVM: ruby-3.4.1) [gem]" level="application" />
65
+ </component>
66
+ <component name="RakeTasksCache-v2" failedReloadsCounter="2">
67
+ <option name="failedReloadsCounter" value="2" />
68
+ </component>
69
+ </module>
data/.idea/icon.png ADDED
Binary file
@@ -0,0 +1,36 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="HttpUrlsUsage" enabled="true" level="WEAK WARNING" enabled_by_default="true">
5
+ <option name="ignoredUrls">
6
+ <list>
7
+ <option value="http://0.0.0.0" />
8
+ <option value="http://127.0.0.1" />
9
+ <option value="http://activemq.apache.org/schema/" />
10
+ <option value="http://cxf.apache.org/schemas/" />
11
+ <option value="http://example.org" />
12
+ <option value="http://java.sun.com/" />
13
+ <option value="http://javafx.com/fxml" />
14
+ <option value="http://javafx.com/javafx/" />
15
+ <option value="http://json-schema.org/draft" />
16
+ <option value="http://localhost" />
17
+ <option value="http://maven.apache.org/POM/" />
18
+ <option value="http://maven.apache.org/xsd/" />
19
+ <option value="http://primefaces.org/ui" />
20
+ <option value="http://schema.cloudfoundry.org/spring/" />
21
+ <option value="http://schemas.xmlsoap.org/" />
22
+ <option value="http://tiles.apache.org/" />
23
+ <option value="http://www.ibm.com/webservices/xsd" />
24
+ <option value="http://www.jboss.com/xml/ns/" />
25
+ <option value="http://www.jboss.org/j2ee/schema/" />
26
+ <option value="http://www.springframework.org/schema/" />
27
+ <option value="http://www.springframework.org/security/tags" />
28
+ <option value="http://www.springframework.org/tags" />
29
+ <option value="http://www.thymeleaf.org" />
30
+ <option value="http://www.w3.org/" />
31
+ <option value="http://xmlns.jcp.org/" />
32
+ </list>
33
+ </option>
34
+ </inspection_tool>
35
+ </profile>
36
+ </component>
data/.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/halchemy.iml" filepath="$PROJECT_DIR$/.idea/halchemy.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
data/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$/../../.." vcs="Git" />
5
+ </component>
6
+ </project>
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.1
3
+
4
+ Style/StringLiterals:
5
+ EnforcedStyle: double_quotes
6
+
7
+ Style/StringLiteralsInInterpolation:
8
+ EnforcedStyle: double_quotes
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-01-19
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Michael Ottoson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Halchemy
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/halchemy`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/halchemy. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/halchemy/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Halchemy project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/halchemy/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "minitest/test_task"
5
+
6
+ Minitest::TestTask.create
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[test rubocop]
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ BASE_URL = "http://example.org"
4
+
5
+ READ_METHODS = %i[get head options].freeze
6
+ MODIFY_METHODS = %i[put patch delete].freeze
7
+ PAYLOAD_METHODS = %i[post put patch].freeze
8
+ ALL_METHODS = READ_METHODS + MODIFY_METHODS + %i[post]
9
+
10
+
11
+ Before do
12
+ WebMock::HttpLibAdapters::HttpxAdapter.enable!
13
+ WebMock::Config.instance.query_values_notation = :flat_array
14
+ WebMock.disable_net_connect!
15
+ stub_request(:any, /.*/)
16
+ end
17
+
18
+ After do
19
+ WebMock.reset!
20
+ end
21
+
22
+
23
+ ROOT_JSON = {
24
+ _links: {
25
+ self: { href: "/" },
26
+ resource1: { href: "/path/to/resource1" },
27
+ resource2: { href: "/path/to/resource2" },
28
+ }
29
+ }.to_json
30
+
31
+ RESOURCE_JSON = {
32
+ data: "some resource",
33
+ _links: {
34
+ self: { href: "/path/to/resource1" }
35
+ },
36
+ _etag: "from field"
37
+ }.to_json
38
+
39
+
40
+
41
+ Given(/^a HAL resource$/) do
42
+ stub_for_hal_resource_scenarios
43
+ @api = Halchemy::Api.new BASE_URL
44
+ @root_resource = @api.root.get
45
+ end
46
+
47
+ # @return [void]
48
+ def stub_for_hal_resource_scenarios
49
+ headers = {
50
+ "Content-Type" => %w[application/json charset=UTF-8],
51
+ "Etag" => "from header"
52
+ }
53
+
54
+ stub_request(:get, BASE_URL).to_return(body: ROOT_JSON, headers: headers)
55
+ stub_request(:get, %r{\A#{BASE_URL}/path(/.*)?\z}).to_return(status: 200, body: RESOURCE_JSON, headers: headers)
56
+ end
57
+
58
+ def last_request
59
+ executed_requests = WebMock::RequestRegistry.instance.requested_signatures.hash
60
+
61
+ raise "No requests have been made" if executed_requests.empty?
62
+
63
+ executed_requests.keys.last
64
+ end
65
+
66
+ def clear_request_registry
67
+ WebMock::RequestRegistry.instance.reset!
68
+ end
69
+
70
+ def normalize_path(url)
71
+ uri = URI.parse(url)
72
+
73
+ # Sort query parameters
74
+ if uri.query
75
+ query_params = URI.decode_www_form(uri.query).sort
76
+ uri.query = URI.encode_www_form(query_params)
77
+ end
78
+
79
+ # Return normalized path with query
80
+ uri.path + (uri.query ? "?#{uri.query}" : "")
81
+ end
82
+
83
+ def make_requests(methods, requester, payload = nil, content_type = nil)
84
+ requests = {}
85
+ methods.each do |method|
86
+ clear_request_registry
87
+
88
+ if payload.nil?
89
+ requester.public_send(method)
90
+ else
91
+ requester.public_send(method, payload, content_type)
92
+ end
93
+
94
+ requests[method] = last_request
95
+ end
96
+ requests
97
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "webmock"
4
+ require "webmock/cucumber"
5
+ require "httpx/adapters/webmock"
6
+ require "rspec/expectations"
7
+ require "halchemy"
8
+
9
+ Given(/^the Api is created with a (.*)$/) do |base_url|
10
+ @api = Halchemy::Api.new(base_url)
11
+ end
12
+
13
+ And(/^is later changed to a (.*)$/) do |new_base_url|
14
+ @api.base_url = new_base_url
15
+ end
16
+
17
+ When(/^I GET the root resource$/) do
18
+ @methods_used = [:get]
19
+ @api.root.get
20
+ end
21
+
22
+ When(/^a request is given a (.*)$/) do |relative_url|
23
+ @methods_used = ALL_METHODS
24
+ @methods_used.each do |method|
25
+ @api.using_endpoint(relative_url).public_send(method)
26
+ end
27
+ end
28
+
29
+ Then(/^the request is made to the (.*)$/) do |expected_url|
30
+ @methods_used.each do |method|
31
+ expect(a_request(method, expected_url)).to have_been_made.at_least_once
32
+ end
33
+ end
34
+
35
+ When(/^the request is given an (.*)$/) do |absolute_url|
36
+ @methods_used = ALL_METHODS
37
+ @methods_used.each do |method|
38
+ @api.using_endpoint(absolute_url).public_send(method)
39
+ end
40
+ end
41
+
42
+ Then(/^the request is made to that (.*)$/) do |absolute_url|
43
+ ALL_METHODS.each do |method|
44
+ expect(a_request(method, absolute_url)).to have_been_made.at_least_once
45
+ end
46
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ Given(/^an Api with default error handling configuration$/) do
4
+ @api = Halchemy::Api.new BASE_URL
5
+ end
6
+
7
+ Given(/^an Api configured to not throw on network error$/) do
8
+ @api = Halchemy::Api.new BASE_URL
9
+ @api.error_handling.raise_for_network_errors = false
10
+ end
11
+
12
+ Given(/^an Api configured to throw on status codes >399$/) do
13
+ @api = Halchemy::Api.new BASE_URL
14
+ @api.error_handling.raise_for_status_codes = ">399"
15
+ end
16
+
17
+ Given(/^an Api configured to throw on status codes >399, except 404$/) do
18
+ @api = Halchemy::Api.new BASE_URL
19
+ @api.error_handling.raise_for_status_codes = "400-403 >404"
20
+ end
21
+
22
+ When(/^a request has this result (.*)$/) do |result|
23
+ server = stub_request(:any, BASE_URL)
24
+ if result.start_with?("status_code:")
25
+ server.to_return(status: result.sub("status_code:", "").to_i, body: { error: "error" }.to_json)
26
+ else
27
+ server.to_raise(result)
28
+ end
29
+
30
+ @exception_thrown = {}
31
+ ALL_METHODS.each do |method|
32
+ @exception_thrown[method] = false
33
+ @api.using_endpoint("/").public_send(method)
34
+ rescue Halchemy::HttpError
35
+ @exception_thrown[method] = true
36
+ end
37
+ end
38
+
39
+ Then(/^an exception (.*) thrown$/) do |is_or_is_not|
40
+ ALL_METHODS.each do |method|
41
+ expect(@exception_thrown[method]).to be(is_or_is_not == "is")
42
+ end
43
+ end
44
+
45
+ When(/^a request results in a status code of 401$/) do
46
+ stub_request(:any, BASE_URL).to_return(status: 401)
47
+ @resource = {}
48
+ ALL_METHODS.each do |method|
49
+ @resource[method] = @api.using_endpoint("/").public_send(method)
50
+ end
51
+ end
52
+
53
+ And(/^the code asks to throw an exception for non-successful status codes$/) do
54
+ @raise_method = {}
55
+ ALL_METHODS.each do |method|
56
+ @raise_method[method] = @resource[method]._halchemy.method(:raise_for_status_codes)
57
+ end
58
+ end
59
+
60
+ Then(/^based on the override settings (.*) an exception (.*) thrown$/) do |settings, is_or_is_not|
61
+ ALL_METHODS.each do |method|
62
+ exception_thrown = false
63
+ begin
64
+ if settings == "empty"
65
+ @raise_method[method].call
66
+ else
67
+ @raise_method[method].call(settings)
68
+ end
69
+ rescue Halchemy::HttpError
70
+ exception_thrown = true
71
+ end
72
+
73
+ expect(exception_thrown).to be(is_or_is_not == "is")
74
+ end
75
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ HEADERS = {
4
+ "Cache-control" => "no-cache",
5
+ "Connection" => "close",
6
+ "User-agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 " \
7
+ "(KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
8
+ }.freeze
9
+
10
+ REMOVE_HEADERS = %w[Cache-control Authorization].freeze
11
+
12
+ Given(/^the Api is created with no headers$/) do
13
+ @api = Halchemy::Api.new BASE_URL
14
+ end
15
+
16
+ And(/^later is given new a new (.*) with its (.*)$/) do |header, value|
17
+ headers = { header => value }
18
+ @api.add_headers(headers)
19
+ end
20
+
21
+ When(/^a request is sent$/) do
22
+ @headers = {}
23
+ ALL_METHODS.each do |method|
24
+ @api.using_endpoint("/").public_send(method)
25
+ @headers[method] = last_request.headers
26
+ end
27
+ end
28
+
29
+ Then(/^the request contains each (.*) with its (.*) for all sensible ones for the (.*)$/) do |header, value, type|
30
+ # TODO: handle different method types having different headers - for now all headers are in
31
+ ALL_METHODS.each do |method|
32
+ p "In the future, this will be checked against method type #{type}"
33
+ expect(a_request(method, BASE_URL).with(headers: { header => value })).to have_been_made.at_least_once
34
+ end
35
+ end
36
+
37
+ Given(/^the Api is created with headers$/) do
38
+ @api = Halchemy::Api.new(BASE_URL, headers: HEADERS)
39
+ end
40
+
41
+ And(/^later some headers are removed$/) do
42
+ @api.remove_headers(REMOVE_HEADERS)
43
+ end
44
+
45
+ Then(/^the request does not contain the removed headers$/) do
46
+ ALL_METHODS.each do |method|
47
+ remaining_headers = REMOVE_HEADERS.select { |header| @headers[method].key?(header) }
48
+ expect(remaining_headers).to match_array([])
49
+ end
50
+ end
51
+
52
+ Then(/^the request contains the headers and their values$/) do
53
+ expect(a_request(:get, BASE_URL).with(headers: HEADERS)).to have_been_made.at_least_once
54
+ end
55
+
56
+ Given(/^an Api is created with a different (.*) for an out-of-the-box (.*)$/) do |value, header|
57
+ headers = { header => value }
58
+ @api = Halchemy::Api.new(BASE_URL, headers: headers)
59
+ end
60
+
61
+ Then(/^the request contains the (.*) with the new (.*)$/) do |header, value|
62
+ expect(a_request(:get, BASE_URL).with(headers: { header => value })).to have_been_made.at_least_once
63
+ end
64
+
65
+ Then(/^the request contains each new (.*) with its (.*)$/) do |header, value|
66
+ expect(a_request(:get, BASE_URL).with(headers: { header => value })).to have_been_made.at_least_once
67
+ end
68
+
69
+ When(/^I set a new value to a previously added header but with a different case$/) do
70
+ upper = HEADERS.each_with_object(Hash.new(0)) do |item, result|
71
+ key, value = item
72
+ result[key.upcase] = value
73
+ end
74
+ @api.add_headers(upper)
75
+
76
+ lower = HEADERS.each_with_object(Hash.new(0)) do |item, result|
77
+ key, value = item
78
+ result[key.downcase] = value
79
+ end
80
+ @api.add_headers(lower)
81
+ @api.using_endpoint("/path").get
82
+ end
83
+
84
+ Then(/^the header is changed not added$/) do
85
+ headers = last_request.headers
86
+
87
+ api_has_duplicate_headers = @api.headers.keys.map(&:downcase).uniq.size != @api.headers.keys.size
88
+ request_has_duplicate_headers = headers.keys.map(&:downcase).uniq.size != headers.keys.size
89
+
90
+ expect(request_has_duplicate_headers).to be(false), "request has duplicate headers"
91
+ expect(api_has_duplicate_headers).to be(false), "api has duplicate headers"
92
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ When(/^I make a request using its link relations$/) do
4
+ @requests = make_requests(ALL_METHODS, @api.follow(@root_resource).to("resource1"))
5
+ end
6
+
7
+ Then(/^the href of the link is used for the request$/) do
8
+ ALL_METHODS.each do |method|
9
+ expect(@requests[method].uri.to_s).to include("resource1")
10
+ end
11
+ end
12
+
13
+ When(/^I make a request to a link relation the resource does not have$/) do
14
+ @error = {}
15
+ ALL_METHODS.each do |method|
16
+ @error[method] = nil
17
+ @api.follow(@root_resource).to("non-existent").public_send(method)
18
+ rescue KeyError => e
19
+ @error[method] = e
20
+ end
21
+ end
22
+
23
+ Then(/^the request fails, informing me of the issue$/) do
24
+ ALL_METHODS.each do |method|
25
+ expect(@error[method]).to be_a(KeyError)
26
+ expect(@error[method].message).to include("does not have a link relation named non-existent")
27
+ end
28
+ end
29
+
30
+ When(/^I ask for the links it has$/) do
31
+ @links = @root_resource.links
32
+ end
33
+
34
+ Then(/^I get a list of its relations$/) do
35
+ expect(@links).to be_a(Array)
36
+ expect(@links.length).to be 3
37
+ expect(@links).to include("self", "resource1", "resource2")
38
+ end
39
+
40
+ When(/^I ask if it has a link relation$/) do
41
+ @true_if_exists = @root_resource.rel?("self")
42
+ @false_if_not_exists = @root_resource.rel?("not-a-rel")
43
+ end
44
+
45
+ Then(/^it tells me whether it does or not$/) do
46
+ expect(@true_if_exists).to be true
47
+ expect(@false_if_not_exists).to be false
48
+ end
49
+
50
+ When(/^I use an object my language uses to represent JSON as the payload of a request$/) do
51
+ data = {
52
+ "key1" => "value1",
53
+ "key2" => 2,
54
+ "key3" => true,
55
+ "key4" => nil,
56
+ "key5" => [1, 2, 3],
57
+ "key6" => { "subkey1" => "sub value", "subkey2" => 2 }
58
+ }
59
+
60
+ @payload = data.to_json
61
+ @requests = make_requests(PAYLOAD_METHODS, @api.follow(@root_resource).to("resource1"), @payload)
62
+ end
63
+
64
+ Then(/^the request body is properly formatted JSON$/) do
65
+ PAYLOAD_METHODS.each do |method|
66
+ expect(@requests[method].headers["Content-Type"]).to eq("application/json")
67
+ expect(@requests[method].body).to eq(@payload)
68
+ end
69
+ end
70
+
71
+ When(/^I use data type that is not an object but is valid as JSON, e\.g\. (.*)$/) do |data|
72
+ @payload = data
73
+ @requests = make_requests(PAYLOAD_METHODS, @api.follow(@root_resource).to("resource1"), @payload)
74
+ end
75
+
76
+ When(/^the payload of a request is has (.*) of a different (.*)$/) do |data, content_type|
77
+ @payload = data
78
+ @requests = make_requests(PAYLOAD_METHODS, @api.follow(@root_resource).to("resource1"), data, content_type)
79
+ end
80
+
81
+ Then(/^the request is made with the correct (.*) and (.*) header$/) do |data, content_type|
82
+ PAYLOAD_METHODS.each do |method|
83
+ expect(@requests[method].headers["Content-Type"]).to eq(content_type)
84
+ expect(@requests[method].body).to eq(data)
85
+ end
86
+ end