app_test.rb 8.3 KB

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