Class: Middleware::ContentTypeValidator

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/chef-licensing/restful_client/middleware/content_type_validator.rb

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/chef-licensing/restful_client/middleware/content_type_validator.rb', line 6

def call(env)
  @app.call(env).on_complete do |response_env|
    content_type = response_env[:response_headers]["content-type"]
    body = response_env[:body]
    # trim the body to 1000 characters to avoid printing a huge string in the error message
    body = body[0..1000] if body.is_a?(String)
    raise ChefLicensing::UnsupportedContentType, error_message(content_type, body) unless content_type == "application/json"
  end
end

#error_message(content_type, body = nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/chef-licensing/restful_client/middleware/content_type_validator.rb', line 16

def error_message(content_type, body = nil)
  <<~EOM
    Expected 'application/json' content-type, but received '#{content_type}' from the licensing server.
    Snippet of body: `#{body}`
    Possible causes: Check for firewall restrictions, ensure proper server response, or seek support assistance.
  EOM
end