app_test.rb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. require 'test_helper'
  2. require 'rack/test'
  3. require 'app'
  4. class AppTest < MiniTest::Spec
  5. include Rack::Test::Methods
  6. def app
  7. App
  8. end
  9. describe "/" do
  10. it "works" do
  11. get '/'
  12. assert last_response.ok?
  13. end
  14. it "redirects without the query string" do
  15. get '/', foo: 'bar'
  16. assert last_response.redirect?
  17. assert_equal 'http://example.org/', last_response['Location']
  18. end
  19. it "sets default size" do
  20. get '/'
  21. assert_includes last_response.body, 'data-size="18rem"'
  22. end
  23. it "sets size from cookie" do
  24. set_cookie('size=42')
  25. get '/'
  26. assert_includes last_response.body, 'data-size="42px"'
  27. end
  28. end
  29. describe "/[static-page]" do
  30. it "redirects to /#/[static-page]" do
  31. %w(offline about news help).each do |page|
  32. get "/#{page}"
  33. assert last_response.redirect?
  34. assert_equal "http://example.org/#/#{page}", last_response['Location']
  35. end
  36. end
  37. end
  38. describe "/search" do
  39. it "redirects to /#q=" do
  40. get '/search'
  41. assert last_response.redirect?
  42. assert_equal 'http://example.org/#q=', last_response['Location']
  43. get '/search', q: 'foo'
  44. assert last_response.redirect?
  45. assert_equal 'http://example.org/#q=foo', last_response['Location']
  46. end
  47. end
  48. describe "/manifest.appcache" do
  49. it "works" do
  50. get '/manifest.appcache'
  51. assert last_response.ok?
  52. end
  53. it "works with cookie" do
  54. set_cookie('docs=css/html')
  55. get '/manifest.appcache'
  56. assert last_response.ok?
  57. assert_includes last_response.body, '/css/index.json'
  58. assert_includes last_response.body, '/html/index.json'
  59. end
  60. it "has the word 'default' when no 'dark' cookie is set" do
  61. get '/manifest.appcache'
  62. assert_includes last_response.body, '# default'
  63. refute_includes last_response.body, '# dark'
  64. end
  65. it "has the word 'dark' when the cookie is set" do
  66. set_cookie('dark=1')
  67. get '/manifest.appcache'
  68. assert_includes last_response.body, '# dark'
  69. refute_includes last_response.body, '# default'
  70. end
  71. it "sets default size" do
  72. get '/manifest.appcache'
  73. assert_includes last_response.body, '18rem'
  74. end
  75. it "sets size from cookie" do
  76. set_cookie('size=42')
  77. get '/manifest.appcache'
  78. assert_includes last_response.body, '42px'
  79. end
  80. end
  81. describe "/[doc]" do
  82. it "renders when the doc exists and isn't enabled" do
  83. set_cookie('docs=css')
  84. get '/html/', {}, 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0'
  85. assert last_response.ok?
  86. end
  87. it "redirects to root when the doc exists and is enabled" do
  88. set_cookie('docs=html')
  89. get '/html/', {}, 'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0) Gecko/20100101 Firefox/39.0'
  90. assert last_response.redirect?
  91. assert_equal 'http://example.org/#/html/', last_response['Location']
  92. end
  93. it "renders when the doc exists and is enabled, and the request is from Googlebot" do
  94. set_cookie('docs=html')
  95. get '/html/', {}, 'HTTP_USER_AGENT' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'
  96. assert last_response.ok?
  97. end
  98. it "returns 404 when the doc doesn't exist" do
  99. get '/foo/'
  100. assert last_response.not_found?
  101. end
  102. it "redirects with trailing slash" do
  103. get '/html'
  104. assert last_response.redirect?
  105. assert_equal 'http://example.org/html/', last_response['Location']
  106. get '/html', bar: 'baz'
  107. assert last_response.redirect?
  108. assert_equal 'http://example.org/html/?bar=baz', last_response['Location']
  109. end
  110. end
  111. describe "/[doc]-[type]" do
  112. it "works when the doc exists" do
  113. get '/html-foo-bar_42/'
  114. assert last_response.ok?
  115. end
  116. it "returns 404 when the type is blank" do
  117. get '/html-/'
  118. assert last_response.not_found?
  119. end
  120. it "returns 404 when the type is not alpha-numeric" do
  121. get '/html-foo:bar/'
  122. assert last_response.not_found?
  123. end
  124. it "returns 404 when the doc doesn't exist" do
  125. get '/foo-bar/'
  126. assert last_response.not_found?
  127. end
  128. it "redirects with trailing slash" do
  129. get '/html-foo'
  130. assert last_response.redirect?
  131. assert_equal 'http://example.org/html-foo/', last_response['Location']
  132. get '/html-foo', bar: 'baz'
  133. assert last_response.redirect?
  134. assert_equal 'http://example.org/html-foo/?bar=baz', last_response['Location']
  135. end
  136. end
  137. describe "/[doc+type]/[path]" do
  138. it "works when the doc exists" do
  139. get '/html/foo'
  140. assert last_response.ok?
  141. get '/html-bar/foo'
  142. assert last_response.ok?
  143. end
  144. it "returns 404 when the doc doesn't exist" do
  145. get '/foo/bar'
  146. assert last_response.not_found?
  147. end
  148. it "redirects without trailing slash" do
  149. get '/html/foo/'
  150. assert last_response.redirect?
  151. assert_equal 'http://example.org/html/foo', last_response['Location']
  152. get '/html/foo/', bar: 'baz'
  153. assert last_response.redirect?
  154. assert_equal 'http://example.org/html/foo?bar=baz', last_response['Location']
  155. end
  156. end
  157. describe "/docs.json" do
  158. it "returns to the asset path" do
  159. get '/docs.json'
  160. assert last_response.redirect?
  161. assert_equal 'http://example.org/assets/docs.json', last_response['Location']
  162. end
  163. end
  164. describe "/application.js" do
  165. it "returns to the asset path" do
  166. get '/application.js'
  167. assert last_response.redirect?
  168. assert_equal 'http://example.org/assets/application.js', last_response['Location']
  169. end
  170. end
  171. describe "/application.css" do
  172. it "returns to the asset path" do
  173. get '/application.css'
  174. assert last_response.redirect?
  175. assert_equal 'http://example.org/assets/application.css', last_response['Location']
  176. end
  177. end
  178. describe "/feed" do
  179. it "returns an atom feed" do
  180. get '/feed'
  181. assert last_response.ok?
  182. assert_equal 'application/atom+xml', last_response['Content-Type']
  183. get '/feed.atom'
  184. assert last_response.ok?
  185. assert_equal 'application/atom+xml', last_response['Content-Type']
  186. end
  187. end
  188. describe "/s/[link]" do
  189. it "redirects" do
  190. %w(maxcdn shopify code-school jetbrains tw fb re).each do |link|
  191. get "/s/#{link}"
  192. assert last_response.redirect?
  193. end
  194. end
  195. end
  196. describe "/ping" do
  197. it "works" do
  198. get '/ping'
  199. assert last_response.ok?
  200. end
  201. end
  202. describe "404" do
  203. it "works" do
  204. get '/foo'
  205. assert last_response.not_found?
  206. end
  207. end
  208. end