Module: JSONAPIonify::Api::TestHelper

Extended by:
ActiveSupport::Concern
Includes:
Rack::Test::Methods
Defined in:
lib/jsonapionify/api/test_helper.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#accept(*values) ⇒ Object



84
85
86
# File 'lib/jsonapionify/api/test_helper.rb', line 84

def accept(*values)
  header('accept', values.join(','))
end

#authorization(type, value) ⇒ Object



93
94
95
# File 'lib/jsonapionify/api/test_helper.rb', line 93

def authorization(type, value)
  header 'Authorization', [type, value].join(' ')
end

#content_type(value) ⇒ Object



80
81
82
# File 'lib/jsonapionify/api/test_helper.rb', line 80

def content_type(value)
  header('content-type', value)
end

#delete(*args, &block) ⇒ Object



88
89
90
91
# File 'lib/jsonapionify/api/test_helper.rb', line 88

def delete(*args, &block)
  header('content-type', set_headers['content-type'].to_s)
  super
end

#header(name, value) ⇒ Object



75
76
77
78
# File 'lib/jsonapionify/api/test_helper.rb', line 75

def header(name, value)
  set_headers[name] = value
  super
end

#json(hash) ⇒ Object



67
68
69
# File 'lib/jsonapionify/api/test_helper.rb', line 67

def json(hash)
  Oj.dump hash.deep_stringify_keys
end

#last_response_error_messagesObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jsonapionify/api/test_helper.rb', line 34

def last_response_error_messages
  last_response_errors&.map do |error|
    {
      id:        error['id'],
      code:      error['code'],
      status:    error['status'],
      title:     error['title'],
      detail:    error['detail'],
      links:     error['links'],
      source:    error['source']&.each_with_object([]) { |(k, v), a|
        a << "\n  #{k}: #{v}" if v
      }&.join,
      meta:      error['meta']&.except('backtrace')&.each_with_object([]) { |(k, v), a|
        a << "\n  #{k}: #{v}" if v
      }&.join,
      ': ':      ':',
      backtrace: ["\n", *(error.dig('meta', 'backtrace') || [])].join("\n")
    }.each_with_object([]) { |(k, v), a|
      a << "#{k}: #{v}" if v&.strip
    }.join("\n")
  end
end

#last_response_errorsObject



57
58
59
60
61
# File 'lib/jsonapionify/api/test_helper.rb', line 57

def last_response_errors
  last_response_json['errors']
rescue
  []
end

#last_response_jsonObject



71
72
73
# File 'lib/jsonapionify/api/test_helper.rb', line 71

def last_response_json
  Oj.load last_response.body
end

#set_headersObject



63
64
65
# File 'lib/jsonapionify/api/test_helper.rb', line 63

def set_headers
  @set_headers ||= Rack::Utils::HeaderHash.new
end