1
0

app_test.rb 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "decodes '~' properly" do
  141. get '/html%7E5/'
  142. assert last_response.ok?
  143. get '/html%7E42/'
  144. assert last_response.not_found?
  145. end
  146. it "redirects with trailing slash" do
  147. get '/html'
  148. assert last_response.redirect?
  149. assert_equal 'http://example.org/html/', last_response['Location']
  150. get '/html', bar: 'baz'
  151. assert last_response.redirect?
  152. assert_equal 'http://example.org/html/?bar=baz', last_response['Location']
  153. end
  154. it "redirects old docs" do
  155. get '/iojs/'
  156. assert last_response.redirect?
  157. assert_equal 'http://example.org/node/', last_response['Location']
  158. end
  159. end
  160. describe "/[doc]-[type]" do
  161. it "works when the doc exists" do
  162. get '/html~4-foo-bar_42/'
  163. assert last_response.ok?
  164. assert_includes last_response.body, 'data-doc="{&quot;name&quot;:&quot;HTML&quot;,&quot;slug&quot;:&quot;html~4&quot;'
  165. end
  166. it "works when the doc has no version in the path and a version exists" do
  167. get '/html-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~5&quot;'
  170. end
  171. it "returns 404 when the type is blank" do
  172. get '/css-/'
  173. assert last_response.not_found?
  174. end
  175. it "returns 404 when the type is not alpha-numeric" do
  176. get '/css-foo:bar/'
  177. assert last_response.not_found?
  178. end
  179. it "returns 404 when the doc doesn't exist" do
  180. get '/html~6-bar/'
  181. assert last_response.not_found?
  182. end
  183. it "redirects with trailing slash" do
  184. get '/css-foo'
  185. assert last_response.redirect?
  186. assert_equal 'http://example.org/css-foo/', last_response['Location']
  187. get '/css-foo', bar: 'baz'
  188. assert last_response.redirect?
  189. assert_equal 'http://example.org/css-foo/?bar=baz', last_response['Location']
  190. end
  191. it "redirects old docs" do
  192. get '/yii1-foo/'
  193. assert last_response.redirect?
  194. assert_equal 'http://example.org/yii~1.1-foo/', last_response['Location']
  195. end
  196. end
  197. describe "/[doc+type]/[path]" do
  198. it "works when the doc exists" do
  199. get '/css/foo'
  200. assert last_response.ok?
  201. get '/css-bar/foo'
  202. assert last_response.ok?
  203. end
  204. it "returns 404 when the doc doesn't exist" do
  205. get '/foo/bar'
  206. assert last_response.not_found?
  207. end
  208. it "redirects without trailing slash" do
  209. get '/css/foo/'
  210. assert last_response.redirect?
  211. assert_equal 'http://example.org/css/foo', last_response['Location']
  212. get '/css/foo/', bar: 'baz'
  213. assert last_response.redirect?
  214. assert_equal 'http://example.org/css/foo?bar=baz', last_response['Location']
  215. end
  216. it "redirects old docs" do
  217. get '/python2/foo'
  218. assert last_response.redirect?
  219. assert_equal 'http://example.org/python~2.7/foo', last_response['Location']
  220. end
  221. end
  222. describe "/docs.json" do
  223. it "returns to the asset path" do
  224. get '/docs.json'
  225. assert last_response.redirect?
  226. assert_equal 'http://example.org/assets/docs.json', last_response['Location']
  227. end
  228. end
  229. describe "/application.js" do
  230. it "returns to the asset path" do
  231. get '/application.js'
  232. assert last_response.redirect?
  233. assert_equal 'http://example.org/assets/application.js', last_response['Location']
  234. end
  235. end
  236. describe "/application.css" do
  237. it "returns to the asset path" do
  238. get '/application.css'
  239. assert last_response.redirect?
  240. assert_equal 'http://example.org/assets/application.css', last_response['Location']
  241. end
  242. end
  243. describe "/feed" do
  244. it "returns an atom feed" do
  245. get '/feed'
  246. assert last_response.ok?
  247. assert_equal 'application/atom+xml', last_response['Content-Type']
  248. get '/feed.atom'
  249. assert last_response.ok?
  250. assert_equal 'application/atom+xml', last_response['Content-Type']
  251. end
  252. end
  253. describe "/s/[link]" do
  254. it "redirects" do
  255. %w(maxcdn shopify code-school jetbrains tw fb re).each do |link|
  256. get "/s/#{link}"
  257. assert last_response.redirect?
  258. end
  259. end
  260. end
  261. describe "/ping" do
  262. it "works" do
  263. get '/ping'
  264. assert last_response.ok?
  265. end
  266. end
  267. describe "404" do
  268. it "works" do
  269. get '/foo'
  270. assert last_response.not_found?
  271. end
  272. end
  273. end