frankfurter/spec/api_spec.rb

68 lines
1.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2012-11-21 01:10:18 +01:00
require_relative 'helper'
require 'rack/test'
2014-03-14 13:08:48 +01:00
require 'api'
2012-11-21 01:10:18 +01:00
2014-03-14 13:08:48 +01:00
describe 'the API' do
2012-11-21 01:10:18 +01:00
include Rack::Test::Methods
let(:app) { Sinatra::Application }
let(:json) { Oj.load(last_response.body) }
2014-10-07 13:08:28 +02:00
let(:headers) { last_response.headers }
2012-11-21 01:10:18 +01:00
2014-06-19 13:31:35 +02:00
it 'describes itself' do
get '/'
last_response.must_be :ok?
end
2015-08-30 12:54:13 +02:00
it 'returns latest quotes' do
2012-11-21 01:10:18 +01:00
get '/latest'
2012-11-21 17:11:47 +01:00
last_response.must_be :ok?
2012-11-21 01:10:18 +01:00
end
it 'sets base currency' do
get '/latest?base=USD'
json['base'].must_equal 'USD'
end
2012-11-21 15:46:15 +01:00
it 'filters symbols' do
get '/latest?symbols=USD'
json['rates'].keys.must_equal %w(USD)
end
2015-08-30 12:54:13 +02:00
it 'returns historical quotes' do
get '/2012-11-20'
json['rates'].wont_be :empty?
2014-03-17 11:15:05 +01:00
json['date'].must_equal '2012-11-20'
end
it 'works around holidays' do
get '/2010-01-01'
json['rates'].wont_be :empty?
end
2014-10-07 13:08:28 +02:00
it 'returns a last modified header' do
%w(/ /latest /2012-11-20).each do |path|
get path
headers['Last-Modified'].wont_be_nil
end
2015-09-15 13:01:16 +02:00
end
2014-10-07 13:08:28 +02:00
it 'allows cross-origin requests' do
%w(/ /latest /2012-11-20).each do |path|
header 'Origin', '*'
get path
refute_empty headers['Access-Control-Allow-Methods']
end
2014-10-07 13:08:28 +02:00
end
it 'responds to preflight requests' do
%w(/ /latest /2012-11-20).each do |path|
header 'Origin', '*'
options path
refute_empty headers['Access-Control-Allow-Methods']
end
end
2012-11-21 01:10:18 +01:00
end