Module: DSL::Request

Extended by:
ActiveSupport::Concern
Defined in:
lib/rspec-api/dsl/request/body.rb,
lib/rspec-api/dsl/request/status.rb,
lib/rspec-api/dsl/request/headers.rb,
lib/rspec-api/dsl/request/request.rb,
lib/rspec-api/dsl/request/response.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#get_request_param_for_list(params_sets) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/rspec-api/dsl/request/body.rb', line 70

def get_request_param_for_list(params_sets)
  (params_sets || []).find do |params|
    conditions = []
    conditions << (request_params[params[:name].to_s] == params[:value])
    params.fetch(:extra_fields, {}).each do |name, value|
      conditions << (request_params[name.to_s] == value)
    end
    conditions.all?
  end
end

#request_paramsObject



10
11
12
# File 'lib/rspec-api/dsl/request/request.rb', line 10

def request_params
  {} # To be overriden by more specific modules
end

#responseObject



58
59
60
61
# File 'lib/rspec-api/dsl/request/response.rb', line 58

def response
  # To be overriden by more specific modules
  OpenStruct.new # body: nil, status: nil, headers: {}
end

#response_bodyObject



63
64
65
66
67
# File 'lib/rspec-api/dsl/request/response.rb', line 63

def response_body
  JSON response_body_without_callbacks
rescue JSON::ParserError, JSON::GeneratorError
  nil
end

#response_headersObject



69
70
71
# File 'lib/rspec-api/dsl/request/response.rb', line 69

def response_headers
  response.headers || {}
end

#response_is_successful?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/rspec-api/dsl/request/body.rb', line 81

def response_is_successful?
  response.status < 400 && !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(response.status)
end

#response_statusObject



73
74
75
# File 'lib/rspec-api/dsl/request/response.rb', line 73

def response_status
  response.status
end

#should_be_filtered_by(filter_params_sets) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rspec-api/dsl/request/body.rb', line 53

def should_be_filtered_by(filter_params_sets)
  # TODO: The following is just so the condition does not match if it's nil
  #       but this should be fixed in get_request_param_for_list
  if filter_params_sets
    filter_params_sets = filter_params_sets.dup
    filter_params_sets.each{|x| x[:value] = request_params.fetch(x[:name].to_s, :something_nil)}
  end
  filter_params = response_is_successful? && get_request_param_for_list(filter_params_sets)
  value = filter_params[:value] if filter_params
  options = filter_params ? filter_params.slice(:by, :comparing_with) : {}
  expect(response).to be_filtered_if filter_params, value, options
end

#should_be_sorted_by(sort_params_sets) ⇒ Object



47
48
49
50
51
# File 'lib/rspec-api/dsl/request/body.rb', line 47

def should_be_sorted_by(sort_params_sets)
  sort_params = response_is_successful? && get_request_param_for_list(sort_params_sets)
  options = sort_params ? sort_params.slice(:by, :verse) : {}
  expect(response).to be_sorted_if sort_params, options
end

#should_be_valid_json(type) ⇒ Object



29
30
31
# File 'lib/rspec-api/dsl/request/body.rb', line 29

def should_be_valid_json(type)
  expect(response).to be_valid_json_if response_is_successful?, type
end

#should_be_wrapped_by(callback_params_sets) ⇒ Object

If the request had a ‘callback’ query parameter, then the body should be JSONP, otherwise it should not. For instance if the request was ‘GET /?method=alert` and the request `accepts_callback :method`, then the body must be a JSON wrapped in the alert(…) callback The callback param says how the request might have been made, e.g. name: ’method’, value: ‘alert’… however to make sure that it was really made, we need to check that request_params is present and that is actually ‘alert’



41
42
43
44
45
# File 'lib/rspec-api/dsl/request/body.rb', line 41

def should_be_wrapped_by(callback_params_sets)
  callback_params = response_is_successful? && get_request_param_for_list(callback_params_sets)
  value = callback_params[:value] if callback_params
  expect(response).to be_a_jsonp_if callback_params, value
end

#should_have_attributes(attributes) ⇒ Object



66
67
68
# File 'lib/rspec-api/dsl/request/body.rb', line 66

def should_have_attributes(attributes)
  expect(response).to have_attributes_if response_is_successful?, attributes
end