1
0

app_test.rb 9.7 KB

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