2015-01-03 16:38:22 +01:00
|
|
|
require 'test_helper'
|
|
|
|
require 'rack/test'
|
|
|
|
require 'app'
|
|
|
|
|
|
|
|
class AppTest < MiniTest::Spec
|
|
|
|
include Rack::Test::Methods
|
|
|
|
|
2015-08-09 17:38:34 +02:00
|
|
|
MODERN_BROWSER = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0'
|
|
|
|
|
2015-01-03 16:38:22 +01:00
|
|
|
def app
|
|
|
|
App
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "/" do
|
|
|
|
it "works" do
|
|
|
|
get '/'
|
|
|
|
assert last_response.ok?
|
|
|
|
end
|
|
|
|
|
2017-02-19 15:34:59 +01:00
|
|
|
it "redirects to /#q= when there is a 'q' query param" do
|
|
|
|
get '/search', q: 'foo'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/#q=foo', last_response['Location']
|
|
|
|
end
|
|
|
|
|
2015-01-03 16:38:22 +01:00
|
|
|
it "redirects without the query string" do
|
|
|
|
get '/', foo: 'bar'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/', last_response['Location']
|
|
|
|
end
|
2015-02-17 04:29:06 +01:00
|
|
|
|
|
|
|
it "sets default size" do
|
|
|
|
get '/'
|
2017-02-20 21:50:15 +01:00
|
|
|
assert_includes last_response.body, 'data-size="20rem"'
|
2015-02-17 04:29:06 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets size from cookie" do
|
|
|
|
set_cookie('size=42')
|
|
|
|
get '/'
|
|
|
|
assert_includes last_response.body, 'data-size="42px"'
|
|
|
|
end
|
2015-08-03 23:08:36 +02:00
|
|
|
|
|
|
|
it "sets layout from cookie" do
|
|
|
|
set_cookie('layout=foo')
|
|
|
|
get '/'
|
2017-09-10 00:14:23 +02:00
|
|
|
assert_includes last_response.body, '<body class="foo">'
|
2015-08-03 23:08:36 +02:00
|
|
|
end
|
2015-01-03 16:38:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "/[static-page]" do
|
2015-08-09 17:38:34 +02:00
|
|
|
it "redirects to /#/[static-page] by default" do
|
2015-01-03 16:38:22 +01:00
|
|
|
%w(offline about news help).each do |page|
|
2015-08-09 17:38:34 +02:00
|
|
|
get "/#{page}", {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal "http://example.org/#/#{page}", last_response['Location']
|
|
|
|
end
|
|
|
|
end
|
2015-08-09 17:38:34 +02:00
|
|
|
|
|
|
|
it "redirects via JS cookie when a cookie exists" do
|
|
|
|
%w(offline about news help).each do |page|
|
|
|
|
set_cookie('foo=bar')
|
|
|
|
get "/#{page}", {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/', last_response['Location']
|
|
|
|
assert last_response['Set-Cookie'].start_with?("initial_path=%2F#{page}; path=/; expires=")
|
|
|
|
end
|
|
|
|
end
|
2015-01-03 16:38:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "/search" do
|
|
|
|
it "redirects to /#q=" do
|
|
|
|
get '/search'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/#q=', last_response['Location']
|
|
|
|
|
|
|
|
get '/search', q: 'foo'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/#q=foo', last_response['Location']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "/manifest.appcache" do
|
|
|
|
it "works" do
|
|
|
|
get '/manifest.appcache'
|
|
|
|
assert last_response.ok?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "works with cookie" do
|
2016-01-24 18:24:05 +01:00
|
|
|
set_cookie('docs=css/html~5')
|
2015-01-03 16:38:22 +01:00
|
|
|
get '/manifest.appcache'
|
|
|
|
assert last_response.ok?
|
2016-01-17 19:30:46 +01:00
|
|
|
assert_includes last_response.body, '/css/index.json?1420139788'
|
2016-01-24 18:24:05 +01:00
|
|
|
assert_includes last_response.body, '/html~5/index.json?1420139791'
|
2016-01-17 19:30:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores invalid docs in the cookie" do
|
|
|
|
set_cookie('docs=foo')
|
|
|
|
get '/manifest.appcache'
|
|
|
|
assert last_response.ok?
|
|
|
|
refute_includes last_response.body, 'foo'
|
2015-01-03 16:38:22 +01:00
|
|
|
end
|
2015-02-09 04:19:07 +01:00
|
|
|
|
2015-08-03 23:06:19 +02:00
|
|
|
it "has the word 'default' when no 'dark' cookie is set" do
|
2015-02-09 04:19:07 +01:00
|
|
|
get '/manifest.appcache'
|
2015-08-03 23:06:19 +02:00
|
|
|
assert_includes last_response.body, '# default'
|
2015-02-09 04:19:07 +01:00
|
|
|
refute_includes last_response.body, '# dark'
|
|
|
|
end
|
|
|
|
|
2015-08-03 23:06:19 +02:00
|
|
|
it "has the word 'dark' when the cookie is set" do
|
2015-02-09 04:19:07 +01:00
|
|
|
set_cookie('dark=1')
|
|
|
|
get '/manifest.appcache'
|
|
|
|
assert_includes last_response.body, '# dark'
|
2015-08-03 23:06:19 +02:00
|
|
|
refute_includes last_response.body, '# default'
|
2015-02-09 04:19:07 +01:00
|
|
|
end
|
2015-02-17 04:42:58 +01:00
|
|
|
|
|
|
|
it "sets default size" do
|
|
|
|
get '/manifest.appcache'
|
2017-02-20 21:50:15 +01:00
|
|
|
assert_includes last_response.body, '20rem'
|
2015-02-17 04:42:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets size from cookie" do
|
|
|
|
set_cookie('size=42')
|
|
|
|
get '/manifest.appcache'
|
|
|
|
assert_includes last_response.body, '42px'
|
|
|
|
end
|
2015-08-03 23:08:36 +02:00
|
|
|
|
|
|
|
it "sets layout from cookie" do
|
|
|
|
set_cookie('layout=foo_layout')
|
|
|
|
get '/manifest.appcache'
|
|
|
|
assert_includes last_response.body, 'foo_layout'
|
|
|
|
end
|
2015-01-03 16:38:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "/[doc]" do
|
2015-06-07 22:54:16 +02:00
|
|
|
it "renders when the doc exists and isn't enabled" do
|
2016-01-24 18:24:05 +01:00
|
|
|
set_cookie('docs=html~5')
|
|
|
|
get '/html~4/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
|
2015-06-07 22:54:16 +02:00
|
|
|
assert last_response.ok?
|
|
|
|
end
|
|
|
|
|
2016-06-20 00:21:24 +02:00
|
|
|
it "renders when the doc exists, is a default doc, and all docs are enabled" do
|
|
|
|
set_cookie('docs=')
|
|
|
|
get '/css/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
|
|
|
|
assert last_response.ok?
|
|
|
|
end
|
|
|
|
|
2015-08-09 17:38:34 +02:00
|
|
|
it "redirects via JS cookie when the doc exists and is enabled" do
|
2016-01-24 18:24:05 +01:00
|
|
|
set_cookie('docs=html~5')
|
|
|
|
get '/html~5/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
|
2016-01-17 19:30:46 +01:00
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/', last_response['Location']
|
2016-01-24 18:24:05 +01:00
|
|
|
assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%7E5%2F; path=/; expires=")
|
2016-01-17 19:30:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "renders when the doc exists, has no version in the path, and isn't enabled" do
|
|
|
|
get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
|
|
|
|
assert last_response.ok?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects via JS cookie when the doc exists, has no version in the path, and a version is enabled" do
|
2016-01-24 18:24:05 +01:00
|
|
|
set_cookie('docs=html~5')
|
2015-08-09 17:38:34 +02:00
|
|
|
get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
|
2015-06-07 22:54:16 +02:00
|
|
|
assert last_response.redirect?
|
2015-08-09 17:38:34 +02:00
|
|
|
assert_equal 'http://example.org/', last_response['Location']
|
|
|
|
assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%2F; path=/; expires=")
|
2015-06-07 22:54:16 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "renders when the doc exists and is enabled, and the request is from Googlebot" do
|
|
|
|
set_cookie('docs=html')
|
|
|
|
get '/html/', {}, 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.ok?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when the doc doesn't exist" do
|
2016-01-24 18:24:05 +01:00
|
|
|
get '/html~6/'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.not_found?
|
|
|
|
end
|
|
|
|
|
2016-06-12 23:58:59 +02:00
|
|
|
it "decodes '~' properly" do
|
|
|
|
get '/html%7E5/'
|
|
|
|
assert last_response.ok?
|
|
|
|
|
|
|
|
get '/html%7E42/'
|
|
|
|
assert last_response.not_found?
|
|
|
|
end
|
|
|
|
|
2015-01-03 16:38:22 +01:00
|
|
|
it "redirects with trailing slash" do
|
|
|
|
get '/html'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/html/', last_response['Location']
|
|
|
|
|
|
|
|
get '/html', bar: 'baz'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/html/?bar=baz', last_response['Location']
|
|
|
|
end
|
2016-03-05 18:49:43 +01:00
|
|
|
|
|
|
|
it "redirects old docs" do
|
|
|
|
get '/iojs/'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/node/', last_response['Location']
|
|
|
|
end
|
2015-01-03 16:38:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "/[doc]-[type]" do
|
|
|
|
it "works when the doc exists" do
|
2016-01-24 18:24:05 +01:00
|
|
|
get '/html~4-foo-bar_42/'
|
2016-01-17 19:30:46 +01:00
|
|
|
assert last_response.ok?
|
2016-06-04 16:09:01 +02:00
|
|
|
assert_includes last_response.body, 'data-doc="{"name":"HTML","slug":"html~4"'
|
2016-01-17 19:30:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works when the doc has no version in the path and a version exists" do
|
2015-01-03 16:38:22 +01:00
|
|
|
get '/html-foo-bar_42/'
|
|
|
|
assert last_response.ok?
|
2016-06-04 16:09:01 +02:00
|
|
|
assert_includes last_response.body, 'data-doc="{"name":"HTML","slug":"html~5"'
|
2015-01-03 16:38:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when the type is blank" do
|
2016-01-17 19:30:46 +01:00
|
|
|
get '/css-/'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.not_found?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when the type is not alpha-numeric" do
|
2016-01-17 19:30:46 +01:00
|
|
|
get '/css-foo:bar/'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.not_found?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when the doc doesn't exist" do
|
2016-01-24 18:24:05 +01:00
|
|
|
get '/html~6-bar/'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.not_found?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects with trailing slash" do
|
2016-01-17 19:30:46 +01:00
|
|
|
get '/css-foo'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.redirect?
|
2016-01-17 19:30:46 +01:00
|
|
|
assert_equal 'http://example.org/css-foo/', last_response['Location']
|
2015-01-03 16:38:22 +01:00
|
|
|
|
2016-01-17 19:30:46 +01:00
|
|
|
get '/css-foo', bar: 'baz'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.redirect?
|
2016-01-17 19:30:46 +01:00
|
|
|
assert_equal 'http://example.org/css-foo/?bar=baz', last_response['Location']
|
2015-01-03 16:38:22 +01:00
|
|
|
end
|
2016-03-05 18:49:43 +01:00
|
|
|
|
|
|
|
it "redirects old docs" do
|
|
|
|
get '/yii1-foo/'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/yii~1.1-foo/', last_response['Location']
|
|
|
|
end
|
2015-01-03 16:38:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "/[doc+type]/[path]" do
|
|
|
|
it "works when the doc exists" do
|
2016-01-17 19:30:46 +01:00
|
|
|
get '/css/foo'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.ok?
|
|
|
|
|
2016-01-17 19:30:46 +01:00
|
|
|
get '/css-bar/foo'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.ok?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when the doc doesn't exist" do
|
|
|
|
get '/foo/bar'
|
|
|
|
assert last_response.not_found?
|
|
|
|
end
|
|
|
|
|
|
|
|
it "redirects without trailing slash" do
|
2016-01-17 19:30:46 +01:00
|
|
|
get '/css/foo/'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.redirect?
|
2016-01-17 19:30:46 +01:00
|
|
|
assert_equal 'http://example.org/css/foo', last_response['Location']
|
2015-01-03 16:38:22 +01:00
|
|
|
|
2016-01-17 19:30:46 +01:00
|
|
|
get '/css/foo/', bar: 'baz'
|
2015-01-03 16:38:22 +01:00
|
|
|
assert last_response.redirect?
|
2016-01-17 19:30:46 +01:00
|
|
|
assert_equal 'http://example.org/css/foo?bar=baz', last_response['Location']
|
2015-01-03 16:38:22 +01:00
|
|
|
end
|
2016-03-05 18:49:43 +01:00
|
|
|
|
|
|
|
it "redirects old docs" do
|
|
|
|
get '/python2/foo'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/python~2.7/foo', last_response['Location']
|
|
|
|
end
|
2015-01-03 16:38:22 +01:00
|
|
|
end
|
|
|
|
|
2015-03-02 00:20:09 +01:00
|
|
|
describe "/docs.json" do
|
|
|
|
it "returns to the asset path" do
|
|
|
|
get '/docs.json'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/assets/docs.json', last_response['Location']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-07 21:58:00 +02:00
|
|
|
describe "/application.js" do
|
|
|
|
it "returns to the asset path" do
|
|
|
|
get '/application.js'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/assets/application.js', last_response['Location']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "/application.css" do
|
|
|
|
it "returns to the asset path" do
|
|
|
|
get '/application.css'
|
|
|
|
assert last_response.redirect?
|
|
|
|
assert_equal 'http://example.org/assets/application.css', last_response['Location']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-03 16:38:22 +01:00
|
|
|
describe "/feed" do
|
|
|
|
it "returns an atom feed" do
|
|
|
|
get '/feed'
|
|
|
|
assert last_response.ok?
|
|
|
|
assert_equal 'application/atom+xml', last_response['Content-Type']
|
|
|
|
|
|
|
|
get '/feed.atom'
|
|
|
|
assert last_response.ok?
|
|
|
|
assert_equal 'application/atom+xml', last_response['Content-Type']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "/s/[link]" do
|
|
|
|
it "redirects" do
|
2015-07-13 04:53:47 +02:00
|
|
|
%w(maxcdn shopify code-school jetbrains tw fb re).each do |link|
|
2015-01-03 16:38:22 +01:00
|
|
|
get "/s/#{link}"
|
|
|
|
assert last_response.redirect?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "/ping" do
|
|
|
|
it "works" do
|
|
|
|
get '/ping'
|
|
|
|
assert last_response.ok?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "404" do
|
|
|
|
it "works" do
|
|
|
|
get '/foo'
|
|
|
|
assert last_response.not_found?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|