app_test.rb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "includes has the word 'light' by default" do
  61. get '/manifest.appcache'
  62. assert_includes last_response.body, '# light'
  63. refute_includes last_response.body, '# dark'
  64. end
  65. it "includes 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, '# light'
  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 "works when the doc exists" do
  83. get '/html/'
  84. assert last_response.ok?
  85. end
  86. it "returns 404 when the doc doesn't exist" do
  87. get '/foo/'
  88. assert last_response.not_found?
  89. end
  90. it "redirects with trailing slash" do
  91. get '/html'
  92. assert last_response.redirect?
  93. assert_equal 'http://example.org/html/', last_response['Location']
  94. get '/html', bar: 'baz'
  95. assert last_response.redirect?
  96. assert_equal 'http://example.org/html/?bar=baz', last_response['Location']
  97. end
  98. end
  99. describe "/[doc]-[type]" do
  100. it "works when the doc exists" do
  101. get '/html-foo-bar_42/'
  102. assert last_response.ok?
  103. end
  104. it "returns 404 when the type is blank" do
  105. get '/html-/'
  106. assert last_response.not_found?
  107. end
  108. it "returns 404 when the type is not alpha-numeric" do
  109. get '/html-foo:bar/'
  110. assert last_response.not_found?
  111. end
  112. it "returns 404 when the doc doesn't exist" do
  113. get '/foo-bar/'
  114. assert last_response.not_found?
  115. end
  116. it "redirects with trailing slash" do
  117. get '/html-foo'
  118. assert last_response.redirect?
  119. assert_equal 'http://example.org/html-foo/', last_response['Location']
  120. get '/html-foo', bar: 'baz'
  121. assert last_response.redirect?
  122. assert_equal 'http://example.org/html-foo/?bar=baz', last_response['Location']
  123. end
  124. end
  125. describe "/[doc+type]/[path]" do
  126. it "works when the doc exists" do
  127. get '/html/foo'
  128. assert last_response.ok?
  129. get '/html-bar/foo'
  130. assert last_response.ok?
  131. end
  132. it "returns 404 when the doc doesn't exist" do
  133. get '/foo/bar'
  134. assert last_response.not_found?
  135. end
  136. it "redirects without trailing slash" do
  137. get '/html/foo/'
  138. assert last_response.redirect?
  139. assert_equal 'http://example.org/html/foo', last_response['Location']
  140. get '/html/foo/', bar: 'baz'
  141. assert last_response.redirect?
  142. assert_equal 'http://example.org/html/foo?bar=baz', last_response['Location']
  143. end
  144. end
  145. describe "/docs.json" do
  146. it "returns to the asset path" do
  147. get '/docs.json'
  148. assert last_response.redirect?
  149. assert_equal 'http://example.org/assets/docs.json', last_response['Location']
  150. end
  151. end
  152. describe "/feed" do
  153. it "returns an atom feed" do
  154. get '/feed'
  155. assert last_response.ok?
  156. assert_equal 'application/atom+xml', last_response['Content-Type']
  157. get '/feed.atom'
  158. assert last_response.ok?
  159. assert_equal 'application/atom+xml', last_response['Content-Type']
  160. end
  161. end
  162. describe "/s/[link]" do
  163. it "redirects" do
  164. %w(maxcdn shopify tw fb re).each do |link|
  165. get "/s/#{link}"
  166. assert last_response.redirect?
  167. end
  168. end
  169. end
  170. describe "/ping" do
  171. it "works" do
  172. get '/ping'
  173. assert last_response.ok?
  174. end
  175. end
  176. describe "404" do
  177. it "works" do
  178. get '/foo'
  179. assert last_response.not_found?
  180. end
  181. end
  182. end