1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/sinatra-contrib/spec/link_header_spec.rb

100 lines
2.3 KiB
Ruby
Raw Permalink Normal View History

2014-05-09 02:39:16 -04:00
require 'spec_helper'
2011-03-24 03:46:55 -04:00
RSpec.describe Sinatra::LinkHeader do
2011-03-24 03:46:55 -04:00
before do
mock_app do
helpers Sinatra::LinkHeader
before('/') { link 'something', :rel => 'from-filter', :foo => :bar }
get '/' do
link :something, 'booyah'
end
get '/style' do
stylesheet '/style.css'
end
get '/prefetch' do
prefetch '/foo'
end
get '/link_headers' do
response['Link'] = "<foo> ;bar=\"baz\""
stylesheet '/style.css'
prefetch '/foo'
link_headers
end
end
end
describe :link do
it "sets link headers" do
get '/'
2016-05-10 10:08:54 -04:00
expect(headers['Link'].lines).to include('<booyah>; rel="something"')
2011-03-24 03:46:55 -04:00
end
it "returns link html tags" do
get '/'
2016-05-10 10:08:54 -04:00
expect(body).to eq('<link href="booyah" rel="something" />')
2011-03-24 03:46:55 -04:00
end
it "takes an options hash" do
get '/'
elements = ["<something>", "foo=\"bar\"", "rel=\"from-filter\""]
expect(headers['Link'].split(",\n").first.strip.split('; ').sort).to eq(elements)
2011-03-24 03:46:55 -04:00
end
end
describe :stylesheet do
it 'sets link headers' do
get '/style'
2016-05-10 10:08:54 -04:00
expect(headers['Link']).to match(%r{^</style\.css>;})
2011-03-24 03:46:55 -04:00
end
it 'sets type to text/css' do
get '/style'
2016-05-10 10:08:54 -04:00
expect(headers['Link']).to include('type="text/css"')
2011-03-24 03:46:55 -04:00
end
it 'sets rel to stylesheet' do
get '/style'
2016-05-10 10:08:54 -04:00
expect(headers['Link']).to include('rel="stylesheet"')
2011-03-24 03:46:55 -04:00
end
it 'returns html tag' do
get '/style'
2016-05-10 10:08:54 -04:00
expect(body).to match(%r{^<link href="/style\.css"})
2011-03-24 03:46:55 -04:00
end
end
describe :prefetch do
it 'sets link headers' do
get '/prefetch'
2016-05-10 10:08:54 -04:00
expect(headers['Link']).to match(%r{^</foo>;})
2011-03-24 03:46:55 -04:00
end
it 'sets rel to prefetch' do
get '/prefetch'
2016-05-10 10:08:54 -04:00
expect(headers['Link']).to include('rel="prefetch"')
2011-03-24 03:46:55 -04:00
end
it 'returns html tag' do
get '/prefetch'
2016-05-10 10:08:54 -04:00
expect(body).to eq('<link href="/foo" rel="prefetch" />')
2011-03-24 03:46:55 -04:00
end
end
describe :link_headers do
it 'generates html for all link headers' do
get '/link_headers'
2016-05-10 10:08:54 -04:00
expect(body).to include('<link href="/foo" rel="prefetch" />')
expect(body).to include('<link href="/style.css" ')
2011-03-24 03:46:55 -04:00
end
it "respects Link headers not generated on its own" do
get '/link_headers'
2016-05-10 10:08:54 -04:00
expect(body).to include('<link href="foo" bar="baz" />')
2011-03-24 03:46:55 -04:00
end
end
end