1
0

app_test.rb 9.5 KB

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