app_test.rb 9.1 KB

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