Module: Pollett::Testing::RequestHelper::InstanceMethods
- Defined in:
- lib/pollett/testing/request_helper.rb
Instance Method Summary collapse
- #a_delete(path, context, params = nil) ⇒ Object
- #a_get(path, context, params = nil) ⇒ Object
- #a_head(path, context, params = nil) ⇒ Object
- #a_patch(path, context, params = nil) ⇒ Object
- #a_post(path, context, params = nil) ⇒ Object
- #a_put(path, context, params = nil) ⇒ Object
- #auth_header_for(context) ⇒ Object
- #authenticated_request(method, path, context, params) ⇒ Object
- #data ⇒ Object
- #errors ⇒ Object
- #expect_keys(hash, *keys) ⇒ Object
- #expect_status(status) ⇒ Object
- #included ⇒ Object
- #json ⇒ Object
- #json_request(method, path, params = nil, headers = {}) ⇒ Object
- #jsonapi ⇒ Object
- #links ⇒ Object
- #meta ⇒ Object
- #params_for(method, params) ⇒ Object
Instance Method Details
#a_delete(path, context, params = nil) ⇒ Object
73 74 75 |
# File 'lib/pollett/testing/request_helper.rb', line 73 def a_delete(path, context, params = nil) authenticated_request(:delete, path, context, params) end |
#a_get(path, context, params = nil) ⇒ Object
57 58 59 |
# File 'lib/pollett/testing/request_helper.rb', line 57 def a_get(path, context, params = nil) authenticated_request(:get, path, context, params) end |
#a_head(path, context, params = nil) ⇒ Object
53 54 55 |
# File 'lib/pollett/testing/request_helper.rb', line 53 def a_head(path, context, params = nil) authenticated_request(:head, path, context, params) end |
#a_patch(path, context, params = nil) ⇒ Object
65 66 67 |
# File 'lib/pollett/testing/request_helper.rb', line 65 def a_patch(path, context, params = nil) authenticated_request(:patch, path, context, params) end |
#a_post(path, context, params = nil) ⇒ Object
61 62 63 |
# File 'lib/pollett/testing/request_helper.rb', line 61 def a_post(path, context, params = nil) authenticated_request(:post, path, context, params) end |
#a_put(path, context, params = nil) ⇒ Object
69 70 71 |
# File 'lib/pollett/testing/request_helper.rb', line 69 def a_put(path, context, params = nil) authenticated_request(:put, path, context, params) end |
#auth_header_for(context) ⇒ Object
95 96 97 |
# File 'lib/pollett/testing/request_helper.rb', line 95 def auth_header_for(context) { "HTTP_AUTHORIZATION" => ActionController::HttpAuthentication::Token.encode_credentials(context.id) } end |
#authenticated_request(method, path, context, params) ⇒ Object
77 78 79 |
# File 'lib/pollett/testing/request_helper.rb', line 77 def authenticated_request(method, path, context, params) json_request(method, path, params, auth_header_for(context)) end |
#data ⇒ Object
21 22 23 |
# File 'lib/pollett/testing/request_helper.rb', line 21 def data @data ||= json[:data] end |
#errors ⇒ Object
25 26 27 |
# File 'lib/pollett/testing/request_helper.rb', line 25 def errors @errors ||= json[:errors] end |
#expect_keys(hash, *keys) ⇒ Object
49 50 51 |
# File 'lib/pollett/testing/request_helper.rb', line 49 def expect_keys(hash, *keys) keys.each { |k| expect(hash).to have_key(k) } end |
#expect_status(status) ⇒ Object
45 46 47 |
# File 'lib/pollett/testing/request_helper.rb', line 45 def expect_status(status) expect(response.status).to eq(status) end |
#included ⇒ Object
41 42 43 |
# File 'lib/pollett/testing/request_helper.rb', line 41 def included @included ||= json[:included] end |
#json ⇒ Object
17 18 19 |
# File 'lib/pollett/testing/request_helper.rb', line 17 def json @json ||= JSON.parse(response.body, symbolize_names: true) end |
#json_request(method, path, params = nil, headers = {}) ⇒ Object
81 82 83 84 85 |
# File 'lib/pollett/testing/request_helper.rb', line 81 def json_request(method, path, params = nil, headers = {}) send(method, path, params_for(method, params), headers.merge({ "CONTENT_TYPE" => "application/json" })) end |
#jsonapi ⇒ Object
33 34 35 |
# File 'lib/pollett/testing/request_helper.rb', line 33 def jsonapi @jsonapi ||= json[:jsonapi] end |
#links ⇒ Object
37 38 39 |
# File 'lib/pollett/testing/request_helper.rb', line 37 def links @links ||= json[:links] end |
#meta ⇒ Object
29 30 31 |
# File 'lib/pollett/testing/request_helper.rb', line 29 def ||= json[:meta] end |
#params_for(method, params) ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/pollett/testing/request_helper.rb', line 87 def params_for(method, params) if [:post, :patch, :put].include?(method) && params JSON.generate(params) else params end end |