app_test.rb 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 "has the word 'default' when no 'dark' cookie is set" do
  104. get '/manifest.appcache'
  105. assert_includes last_response.body, '# default'
  106. refute_includes last_response.body, '# dark'
  107. end
  108. it "has the word 'dark' when the cookie is set" do
  109. set_cookie('dark=1')
  110. get '/manifest.appcache'
  111. assert_includes last_response.body, '# dark'
  112. refute_includes last_response.body, '# default'
  113. end
  114. it "sets default size" do
  115. get '/manifest.appcache'
  116. assert_includes last_response.body, '20rem'
  117. end
  118. it "sets size from cookie" do
  119. set_cookie('size=42')
  120. get '/manifest.appcache'
  121. assert_includes last_response.body, '42px'
  122. end
  123. it "sets layout from cookie" do
  124. set_cookie('layout=foo_layout')
  125. get '/manifest.appcache'
  126. assert_includes last_response.body, 'foo_layout'
  127. end
  128. end
  129. describe "/[doc]" do
  130. it "renders when the doc exists and isn't enabled" do
  131. set_cookie('docs=html~5')
  132. get '/html~4/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  133. assert last_response.ok?
  134. end
  135. it "renders when the doc exists, is a default doc, and all docs are enabled" do
  136. set_cookie('docs=')
  137. get '/css/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  138. assert last_response.ok?
  139. end
  140. it "redirects via JS cookie when the doc exists and is enabled" do
  141. set_cookie('docs=html~5')
  142. get '/html~5/', {}, '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%7E5%2F; path=/; expires=")
  146. end
  147. it "renders when the doc exists, has no version in the path, and isn't enabled" do
  148. get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  149. assert last_response.ok?
  150. end
  151. it "redirects via JS cookie when the doc exists, has no version in the path, and a version is enabled" do
  152. set_cookie('docs=html~5')
  153. get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  154. assert last_response.redirect?
  155. assert_equal 'https://example.org/', last_response['Location']
  156. assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%2F; path=/; expires=")
  157. end
  158. it "renders when the doc exists and is enabled, and the request is from Googlebot" do
  159. set_cookie('docs=html')
  160. get '/html/', {}, 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +https://www.google.com/bot.html)'
  161. assert last_response.ok?
  162. end
  163. it "returns 404 when the doc doesn't exist" do
  164. get '/html~6/'
  165. assert last_response.not_found?
  166. end
  167. it "decodes '~' properly" do
  168. get '/html%7E5/'
  169. assert last_response.ok?
  170. get '/html%7E42/'
  171. assert last_response.not_found?
  172. end
  173. it "redirects with trailing slash" do
  174. get '/html'
  175. assert last_response.redirect?
  176. assert_equal 'https://example.org/html/', last_response['Location']
  177. get '/html', bar: 'baz'
  178. assert last_response.redirect?
  179. assert_equal 'https://example.org/html/?bar=baz', last_response['Location']
  180. end
  181. it "redirects old docs" do
  182. get '/iojs/'
  183. assert last_response.redirect?
  184. assert_equal 'https://example.org/node/', last_response['Location']
  185. end
  186. end
  187. describe "/[doc]-[type]" do
  188. it "works when the doc exists" do
  189. get '/html~4-foo-bar_42/'
  190. assert last_response.ok?
  191. assert_includes last_response.body, 'data-doc="{&quot;name&quot;:&quot;HTML&quot;,&quot;slug&quot;:&quot;html~4&quot;'
  192. end
  193. it "works when the doc has no version in the path and a version exists" do
  194. get '/html-foo-bar_42/'
  195. assert last_response.ok?
  196. assert_includes last_response.body, 'data-doc="{&quot;name&quot;:&quot;HTML&quot;,&quot;slug&quot;:&quot;html~5&quot;'
  197. end
  198. it "returns 404 when the type is blank" do
  199. get '/css-/'
  200. assert last_response.not_found?
  201. end
  202. it "returns 404 when the type is not alpha-numeric" do
  203. get '/css-foo:bar/'
  204. assert last_response.not_found?
  205. end
  206. it "returns 404 when the doc doesn't exist" do
  207. get '/html~6-bar/'
  208. assert last_response.not_found?
  209. end
  210. it "redirects with trailing slash" do
  211. get '/css-foo'
  212. assert last_response.redirect?
  213. assert_equal 'https://example.org/css-foo/', last_response['Location']
  214. get '/css-foo', bar: 'baz'
  215. assert last_response.redirect?
  216. assert_equal 'https://example.org/css-foo/?bar=baz', last_response['Location']
  217. end
  218. it "redirects old docs" do
  219. get '/yii1-foo/'
  220. assert last_response.redirect?
  221. assert_equal 'https://example.org/yii~1.1-foo/', last_response['Location']
  222. end
  223. end
  224. describe "/[doc+type]/[path]" do
  225. it "works when the doc exists" do
  226. get '/css/foo'
  227. assert last_response.ok?
  228. get '/css-bar/foo'
  229. assert last_response.ok?
  230. end
  231. it "returns 404 when the doc doesn't exist" do
  232. get '/foo/bar'
  233. assert last_response.not_found?
  234. end
  235. it "redirects without trailing slash" do
  236. get '/css/foo/'
  237. assert last_response.redirect?
  238. assert_equal 'https://example.org/css/foo', last_response['Location']
  239. get '/css/foo/', bar: 'baz'
  240. assert last_response.redirect?
  241. assert_equal 'https://example.org/css/foo?bar=baz', last_response['Location']
  242. end
  243. it "redirects old docs" do
  244. get '/python2/foo'
  245. assert last_response.redirect?
  246. assert_equal 'https://example.org/python~2.7/foo', last_response['Location']
  247. end
  248. end
  249. describe "/docs.json" do
  250. it "returns to the asset path" do
  251. get '/docs.json'
  252. assert last_response.redirect?
  253. assert_equal 'https://example.org/assets/docs.json', last_response['Location']
  254. end
  255. end
  256. describe "/application.js" do
  257. it "returns to the asset path" do
  258. get '/application.js'
  259. assert last_response.redirect?
  260. assert_equal 'https://example.org/assets/application.js', last_response['Location']
  261. end
  262. end
  263. describe "/application.css" do
  264. it "returns to the asset path" do
  265. get '/application.css'
  266. assert last_response.redirect?
  267. assert_equal 'https://example.org/assets/application.css', last_response['Location']
  268. end
  269. end
  270. describe "/feed" do
  271. it "returns an atom feed" do
  272. get '/feed'
  273. assert last_response.ok?
  274. assert_equal 'application/atom+xml', last_response['Content-Type']
  275. get '/feed.atom'
  276. assert last_response.ok?
  277. assert_equal 'application/atom+xml', last_response['Content-Type']
  278. end
  279. end
  280. describe "/s/[link]" do
  281. it "redirects" do
  282. %w(maxcdn shopify code-school jetbrains tw fb re).each do |link|
  283. get "/s/#{link}"
  284. assert last_response.redirect?
  285. end
  286. end
  287. end
  288. describe "/ping" do
  289. it "works" do
  290. get '/ping'
  291. assert last_response.ok?
  292. end
  293. end
  294. describe "404" do
  295. it "works" do
  296. get '/foo'
  297. assert last_response.not_found?
  298. end
  299. end
  300. end