app_test.rb 9.9 KB

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