app_test.rb 10.0 KB

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