Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.61 KB | None | 0 0
  1. ENV["TEST"] = "1"
  2. ENV["JETS_ENV"] ||= "test"
  3. # Ensures aws api never called. Fixture home folder does not contain ~/.aws/credentails
  4. ENV['HOME'] = "spec/fixtures/home"
  5.  
  6. require "byebug"
  7. require "fileutils"
  8. require "jets"
  9. require "uri"
  10.  
  11. abort("The Jets environment is running in production mode!") if Jets.env == "production"
  12. Jets.boot
  13.  
  14. require 'capybara/rspec'
  15. Capybara.app = Jets.application
  16. # Capybara.current_driver = :selenium
  17. # Capybara.app_host = 'http://localhost:8888'
  18.  
  19.  
  20. module Helpers
  21.   def payload(name)
  22.     JSON.load(IO.read("spec/fixtures/payloads/#{name}.json"))
  23.   end
  24.  
  25.   def post(path, **params)
  26.     http_call(method: :post, path: path, **params)
  27.   end
  28.  
  29.   def get(path, **params)
  30.     http_call(method: :get, path: path, **params)
  31.   end
  32.  
  33.   Response = Struct.new(:status, :body)
  34.  
  35.   def http_call(method:, path:, **params)
  36.     json = JSON.load(IO.read("spec/fixtures/payloads/posts-index.json"))
  37.  
  38.     id_params = path.scan(%r{:([^/]+)}).flatten
  39.     expanded_path = path.dup
  40.     path_parameters = {}
  41.  
  42.     id_params.each do |id_param|
  43.       raise "missing param: :#{id_param}" unless params.include? id_param.to_sym
  44.  
  45.       expanded_path.gsub!(":#{id_param}", params[id_param.to_sym])
  46.       path_parameters = { id_param => params[id_param.to_sym] }
  47.     end
  48.  
  49.     json['resource'] = path
  50.     json['path'] = expanded_path
  51.     json['httpMethod'] = method.to_s.upcase
  52.     json['pathParameters'] = path_parameters
  53.  
  54.     if method != :get
  55.       json['headers']['Content-Type'] = 'application/x-www-form-urlencoded'
  56.       json['body'] = params.to_a.map { |e| "#{URI.encode(e[0].to_s)}=#{URI.encode(e[1].to_s)}" }.join('&')
  57.     end
  58.  
  59.     route = find_route(path, method.to_sym)
  60.     controller = Object.const_get(route.controller_name).new(json, {}, route.action_name)
  61.  
  62.     response = controller.dispatch!
  63.  
  64.     unless response.is_a? Array
  65.       raise "Expected response to be an array. Are you rendering correctly?"
  66.     end
  67.  
  68.     if response.size != 3
  69.       raise "Expected response to be an array of size 3. Are you rendering correctly?"
  70.     end
  71.  
  72.     @_response = Response.new(response[0].to_i, response[2].read)
  73.   end
  74.  
  75.   def response
  76.     @_response
  77.   end
  78.  
  79.   def find_route(path, method)
  80.     path = path[0..-2] if path.end_with? '/'
  81.     path = path[1..-1] if path.start_with? '/'
  82.  
  83.     route = Jets::Router.routes.find { |r| r.path == path && r.method == method.to_s.upcase }
  84.     raise "Route not found: #{method.to_s.upcase} #{path}" if route.blank?
  85.  
  86.     route
  87.   end
  88.  
  89.   def json(str)
  90.     JSON.parse(str, symbolize_names: true)
  91.   end
  92. end
  93.  
  94. RSpec.configure do |c|
  95.   c.include Helpers
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement