app_test.rb 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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')
  70. get '/manifest.appcache'
  71. assert last_response.ok?
  72. assert_includes last_response.body, '/css/index.json'
  73. assert_includes last_response.body, '/html/index.json'
  74. end
  75. it "has the word 'default' when no 'dark' cookie is set" do
  76. get '/manifest.appcache'
  77. assert_includes last_response.body, '# default'
  78. refute_includes last_response.body, '# dark'
  79. end
  80. it "has the word 'dark' when the cookie is set" do
  81. set_cookie('dark=1')
  82. get '/manifest.appcache'
  83. assert_includes last_response.body, '# dark'
  84. refute_includes last_response.body, '# default'
  85. end
  86. it "sets default size" do
  87. get '/manifest.appcache'
  88. assert_includes last_response.body, '18rem'
  89. end
  90. it "sets size from cookie" do
  91. set_cookie('size=42')
  92. get '/manifest.appcache'
  93. assert_includes last_response.body, '42px'
  94. end
  95. it "sets layout from cookie" do
  96. set_cookie('layout=foo_layout')
  97. get '/manifest.appcache'
  98. assert_includes last_response.body, 'foo_layout'
  99. end
  100. end
  101. describe "/[doc]" do
  102. it "renders when the doc exists and isn't enabled" do
  103. set_cookie('docs=css')
  104. get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  105. assert last_response.ok?
  106. end
  107. it "redirects via JS cookie when the doc exists and is enabled" do
  108. set_cookie('docs=html')
  109. get '/html/', {}, 'HTTP_USER_AGENT' => MODERN_BROWSER
  110. assert last_response.redirect?
  111. assert_equal 'http://example.org/', last_response['Location']
  112. assert last_response['Set-Cookie'].start_with?("initial_path=%2Fhtml%2F; path=/; expires=")
  113. end
  114. it "renders when the doc exists and is enabled, and the request is from Googlebot" do
  115. set_cookie('docs=html')
  116. get '/html/', {}, 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
  117. assert last_response.ok?
  118. end
  119. it "returns 404 when the doc doesn't exist" do
  120. get '/foo/'
  121. assert last_response.not_found?
  122. end
  123. it "redirects with trailing slash" do
  124. get '/html'
  125. assert last_response.redirect?
  126. assert_equal 'http://example.org/html/', last_response['Location']
  127. get '/html', bar: 'baz'
  128. assert last_response.redirect?
  129. assert_equal 'http://example.org/html/?bar=baz', last_response['Location']
  130. end
  131. end
  132. describe "/[doc]-[type]" do
  133. it "works when the doc exists" do
  134. get '/html-foo-bar_42/'
  135. assert last_response.ok?
  136. end
  137. it "returns 404 when the type is blank" do
  138. get '/html-/'
  139. assert last_response.not_found?
  140. end
  141. it "returns 404 when the type is not alpha-numeric" do
  142. get '/html-foo:bar/'
  143. assert last_response.not_found?
  144. end
  145. it "returns 404 when the doc doesn't exist" do
  146. get '/foo-bar/'
  147. assert last_response.not_found?
  148. end
  149. it "redirects with trailing slash" do
  150. get '/html-foo'
  151. assert last_response.redirect?
  152. assert_equal 'http://example.org/html-foo/', last_response['Location']
  153. get '/html-foo', bar: 'baz'
  154. assert last_response.redirect?
  155. assert_equal 'http://example.org/html-foo/?bar=baz', last_response['Location']
  156. end
  157. end
  158. describe "/[doc+type]/[path]" do
  159. it "works when the doc exists" do
  160. get '/html/foo'
  161. assert last_response.ok?
  162. get '/html-bar/foo'
  163. assert last_response.ok?
  164. end
  165. it "returns 404 when the doc doesn't exist" do
  166. get '/foo/bar'
  167. assert last_response.not_found?
  168. end
  169. it "redirects without trailing slash" do
  170. get '/html/foo/'
  171. assert last_response.redirect?
  172. assert_equal 'http://example.org/html/foo', last_response['Location']
  173. get '/html/foo/', bar: 'baz'
  174. assert last_response.redirect?
  175. assert_equal 'http://example.org/html/foo?bar=baz', last_response['Location']
  176. end
  177. end
  178. describe "/docs.json" do
  179. it "returns to the asset path" do
  180. get '/docs.json'
  181. assert last_response.redirect?
  182. assert_equal 'http://example.org/assets/docs.json', last_response['Location']
  183. end
  184. end
  185. describe "/application.js" do
  186. it "returns to the asset path" do
  187. get '/application.js'
  188. assert last_response.redirect?
  189. assert_equal 'http://example.org/assets/application.js', last_response['Location']
  190. end
  191. end
  192. describe "/application.css" do
  193. it "returns to the asset path" do
  194. get '/application.css'
  195. assert last_response.redirect?
  196. assert_equal 'http://example.org/assets/application.css', last_response['Location']
  197. end
  198. end
  199. describe "/feed" do
  200. it "returns an atom feed" do
  201. get '/feed'
  202. assert last_response.ok?
  203. assert_equal 'application/atom+xml', last_response['Content-Type']
  204. get '/feed.atom'
  205. assert last_response.ok?
  206. assert_equal 'application/atom+xml', last_response['Content-Type']
  207. end
  208. end
  209. describe "/s/[link]" do
  210. it "redirects" do
  211. %w(maxcdn shopify code-school jetbrains tw fb re).each do |link|
  212. get "/s/#{link}"
  213. assert last_response.redirect?
  214. end
  215. end
  216. end
  217. describe "/ping" do
  218. it "works" do
  219. get '/ping'
  220. assert last_response.ok?
  221. end
  222. end
  223. describe "404" do
  224. it "works" do
  225. get '/foo'
  226. assert last_response.not_found?
  227. end
  228. end
  229. end