app_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. require 'test_helper'
  2. require 'rack/test'
  3. require 'app'
  4. class AppTest < MiniTest::Spec
  5. include Rack::Test::Methods
  6. MODERN_BROWSER = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0'
  7. def app
  8. App
  9. end
  10. before do
  11. current_session.env('HTTPS', 'on')
  12. end
  13. it 'redirects to HTTPS' do
  14. get 'http://example.com/test?q=1', {}, 'HTTPS' => 'off'
  15. assert last_response.redirect?
  16. assert_equal 'https://example.com/test?q=1', last_response['Location']
  17. end
  18. it 'returns HSTS header' do
  19. get 'https://example.com/test'
  20. assert_equal 'max-age=31536000; includeSubDomains', last_response['Strict-Transport-Security']
  21. end
  22. describe "/" do
  23. it "works" do
  24. get '/'
  25. assert last_response.ok?
  26. end
  27. it "redirects to /#q= when there is a 'q' query param" do
  28. get '/search', q: 'foo'
  29. assert last_response.redirect?
  30. assert_equal 'https://example.org/#q=foo', last_response['Location']
  31. end
  32. it "redirects without the query string" do
  33. get '/', foo: 'bar'
  34. assert last_response.redirect?
  35. assert_equal 'https://example.org/', last_response['Location']
  36. end
  37. it "sets default size" do
  38. get '/'
  39. assert_includes last_response.body, 'data-size="20rem"'
  40. end
  41. it "sets size from cookie" do
  42. set_cookie('size=42')
  43. get '/'
  44. assert_includes last_response.body, 'data-size="42px"'
  45. end
  46. it "sets layout from cookie" do
  47. set_cookie('layout=foo')
  48. get '/'
  49. assert_includes last_response.body, '<body class="foo">'
  50. end
  51. it "sets the <html> theme from cookie" do
  52. get '/'
  53. assert_match %r{<html [^>]*class="[^\"]*_theme-default}, last_response.body
  54. refute_includes last_response.body, '_theme-dark'
  55. set_cookie('dark=1')
  56. get '/'
  57. assert_match %r{<html [^>]*class="[^\"]*_theme-dark}, last_response.body
  58. refute_includes last_response.body, '_theme-default'
  59. end
  60. end
  61. describe "/[static-page]" do
  62. it "redirects to /#/[static-page] by default" do
  63. %w(offline about news help).each do |page|
  64. get "/#{page}", {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  65. assert last_response.redirect?
  66. assert_equal "https://example.org/#/#{page}", last_response['Location']
  67. end
  68. end
  69. it "redirects via JS cookie when a cookie exists" do
  70. %w(offline about news help).each do |page|
  71. set_cookie('foo=bar')
  72. get "/#{page}", {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  73. assert last_response.redirect?
  74. assert_equal 'https://example.org/', last_response['Location']
  75. assert last_response['Set-Cookie'].start_with?("initial_path=%2F#{page}; path=/; expires=")
  76. end
  77. end
  78. end
  79. describe "/search" do
  80. it "redirects to /#q=" do
  81. get '/search'
  82. assert last_response.redirect?
  83. assert_equal 'https://example.org/#q=', last_response['Location']
  84. get '/search', q: 'foo'
  85. assert last_response.redirect?
  86. assert_equal 'https://example.org/#q=foo', last_response['Location']
  87. end
  88. end
  89. describe "/manifest.appcache" do
  90. it "works" do
  91. get '/manifest.appcache'
  92. assert last_response.ok?
  93. end
  94. it "works with cookie" do
  95. set_cookie('docs=css/html~5')
  96. get '/manifest.appcache'
  97. assert last_response.ok?
  98. assert_includes last_response.body, '/css/index.json?1420139788'
  99. assert_includes last_response.body, '/html~5/index.json?1420139791'
  100. end
  101. it "ignores invalid docs in the cookie" do
  102. set_cookie('docs=foo')
  103. get '/manifest.appcache'
  104. assert last_response.ok?
  105. refute_includes last_response.body, 'foo'
  106. end
  107. it "has the word 'default' when no 'dark' cookie is set" do
  108. get '/manifest.appcache'
  109. assert_includes last_response.body, '# default'
  110. refute_includes last_response.body, '# dark'
  111. end
  112. it "has the word 'dark' when the cookie is set" do
  113. set_cookie('dark=1')
  114. get '/manifest.appcache'
  115. assert_includes last_response.body, '# dark'
  116. refute_includes last_response.body, '# default'
  117. end
  118. it "sets default size" do
  119. get '/manifest.appcache'
  120. assert_includes last_response.body, '20rem'
  121. end
  122. it "sets size from cookie" do
  123. set_cookie('size=42')
  124. get '/manifest.appcache'
  125. assert_includes last_response.body, '42px'
  126. end
  127. it "sets layout from cookie" do
  128. set_cookie('layout=foo_layout')
  129. get '/manifest.appcache'
  130. assert_includes last_response.body, 'foo_layout'
  131. end
  132. end
  133. describe "/[doc]" do
  134. it "renders when the doc exists and isn't enabled" do
  135. set_cookie('docs=html~5')
  136. get '/html~4/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  137. assert last_response.ok?
  138. end
  139. it "renders when the doc exists, is a default doc, and all docs are enabled" do
  140. set_cookie('docs=')
  141. get '/css/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  142. assert last_response.ok?
  143. end
  144. it "redirects via JS cookie when the doc exists and is enabled" do
  145. set_cookie('docs=html~5')
  146. get '/html~5/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  147. assert last_response.redirect?
  148. assert_equal 'https://example.org/', last_response['Location']
  149. assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%7E5%2F; path=/; expires=")
  150. end
  151. it "renders when the doc exists, has no version in the path, and isn't enabled" do
  152. get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  153. assert last_response.ok?
  154. end
  155. it "redirects via JS cookie when the doc exists, has no version in the path, and a version is enabled" do
  156. set_cookie('docs=html~5')
  157. get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  158. assert last_response.redirect?
  159. assert_equal 'https://example.org/', last_response['Location']
  160. assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%2F; path=/; expires=")
  161. end
  162. it "renders when the doc exists and is enabled, and the request is from Googlebot" do
  163. set_cookie('docs=html')
  164. get '/html/', {}, 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +https://www.google.com/bot.html)'
  165. assert last_response.ok?
  166. end
  167. it "returns 404 when the doc doesn't exist" do
  168. get '/html~6/'
  169. assert last_response.not_found?
  170. end
  171. it "decodes '~' properly" do
  172. get '/html%7E5/'
  173. assert last_response.ok?
  174. get '/html%7E42/'
  175. assert last_response.not_found?
  176. end
  177. it "redirects with trailing slash" do
  178. get '/html'
  179. assert last_response.redirect?
  180. assert_equal 'https://example.org/html/', last_response['Location']
  181. get '/html', bar: 'baz'
  182. assert last_response.redirect?
  183. assert_equal 'https://example.org/html/?bar=baz', last_response['Location']
  184. end
  185. it "redirects old docs" do
  186. get '/iojs/'
  187. assert last_response.redirect?
  188. assert_equal 'https://example.org/node/', last_response['Location']
  189. end
  190. end
  191. describe "/[doc]-[type]" do
  192. it "works when the doc exists" do
  193. get '/html~4-foo-bar_42/'
  194. assert last_response.ok?
  195. assert_includes last_response.body, 'data-doc="{&quot;name&quot;:&quot;HTML&quot;,&quot;slug&quot;:&quot;html~4&quot;'
  196. end
  197. it "works when the doc has no version in the path and a version exists" do
  198. get '/html-foo-bar_42/'
  199. assert last_response.ok?
  200. assert_includes last_response.body, 'data-doc="{&quot;name&quot;:&quot;HTML&quot;,&quot;slug&quot;:&quot;html~5&quot;'
  201. end
  202. it "returns 404 when the type is blank" do
  203. get '/css-/'
  204. assert last_response.not_found?
  205. end
  206. it "returns 404 when the type is not alpha-numeric" do
  207. get '/css-foo:bar/'
  208. assert last_response.not_found?
  209. end
  210. it "returns 404 when the doc doesn't exist" do
  211. get '/html~6-bar/'
  212. assert last_response.not_found?
  213. end
  214. it "redirects with trailing slash" do
  215. get '/css-foo'
  216. assert last_response.redirect?
  217. assert_equal 'https://example.org/css-foo/', last_response['Location']
  218. get '/css-foo', bar: 'baz'
  219. assert last_response.redirect?
  220. assert_equal 'https://example.org/css-foo/?bar=baz', last_response['Location']
  221. end
  222. it "redirects old docs" do
  223. get '/yii1-foo/'
  224. assert last_response.redirect?
  225. assert_equal 'https://example.org/yii~1.1-foo/', last_response['Location']
  226. end
  227. end
  228. describe "/[doc+type]/[path]" do
  229. it "works when the doc exists" do
  230. get '/css/foo'
  231. assert last_response.ok?
  232. get '/css-bar/foo'
  233. assert last_response.ok?
  234. end
  235. it "returns 404 when the doc doesn't exist" do
  236. get '/foo/bar'
  237. assert last_response.not_found?
  238. end
  239. it "redirects without trailing slash" do
  240. get '/css/foo/'
  241. assert last_response.redirect?
  242. assert_equal 'https://example.org/css/foo', last_response['Location']
  243. get '/css/foo/', bar: 'baz'
  244. assert last_response.redirect?
  245. assert_equal 'https://example.org/css/foo?bar=baz', last_response['Location']
  246. end
  247. it "redirects old docs" do
  248. get '/python2/foo'
  249. assert last_response.redirect?
  250. assert_equal 'https://example.org/python~2.7/foo', last_response['Location']
  251. end
  252. end
  253. describe "/docs.json" do
  254. it "returns to the asset path" do
  255. get '/docs.json'
  256. assert last_response.redirect?
  257. assert_equal 'https://example.org/assets/docs.json', last_response['Location']
  258. end
  259. end
  260. describe "/application.js" do
  261. it "returns to the asset path" do
  262. get '/application.js'
  263. assert last_response.redirect?
  264. assert_equal 'https://example.org/assets/application.js', last_response['Location']
  265. end
  266. end
  267. describe "/application.css" do
  268. it "returns to the asset path" do
  269. get '/application.css'
  270. assert last_response.redirect?
  271. assert_equal 'https://example.org/assets/application.css', last_response['Location']
  272. end
  273. end
  274. describe "/feed" do
  275. it "returns an atom feed" do
  276. get '/feed'
  277. assert last_response.ok?
  278. assert_equal 'application/atom+xml', last_response['Content-Type']
  279. get '/feed.atom'
  280. assert last_response.ok?
  281. assert_equal 'application/atom+xml', last_response['Content-Type']
  282. end
  283. end
  284. describe "/s/[link]" do
  285. it "redirects" do
  286. %w(maxcdn shopify code-school jetbrains tw fb re).each do |link|
  287. get "/s/#{link}"
  288. assert last_response.redirect?
  289. end
  290. end
  291. end
  292. describe "/ping" do
  293. it "works" do
  294. get '/ping'
  295. assert last_response.ok?
  296. end
  297. end
  298. describe "404" do
  299. it "works" do
  300. get '/foo'
  301. assert last_response.not_found?
  302. end
  303. end
  304. end