app_test.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. end
  72. describe "/[doc]" do
  73. it "works when the doc exists" do
  74. get '/html/'
  75. assert last_response.ok?
  76. end
  77. it "returns 404 when the doc doesn't exist" do
  78. get '/foo/'
  79. assert last_response.not_found?
  80. end
  81. it "redirects with trailing slash" do
  82. get '/html'
  83. assert last_response.redirect?
  84. assert_equal 'http://example.org/html/', last_response['Location']
  85. get '/html', bar: 'baz'
  86. assert last_response.redirect?
  87. assert_equal 'http://example.org/html/?bar=baz', last_response['Location']
  88. end
  89. end
  90. describe "/[doc]-[type]" do
  91. it "works when the doc exists" do
  92. get '/html-foo-bar_42/'
  93. assert last_response.ok?
  94. end
  95. it "returns 404 when the type is blank" do
  96. get '/html-/'
  97. assert last_response.not_found?
  98. end
  99. it "returns 404 when the type is not alpha-numeric" do
  100. get '/html-foo:bar/'
  101. assert last_response.not_found?
  102. end
  103. it "returns 404 when the doc doesn't exist" do
  104. get '/foo-bar/'
  105. assert last_response.not_found?
  106. end
  107. it "redirects with trailing slash" do
  108. get '/html-foo'
  109. assert last_response.redirect?
  110. assert_equal 'http://example.org/html-foo/', last_response['Location']
  111. get '/html-foo', bar: 'baz'
  112. assert last_response.redirect?
  113. assert_equal 'http://example.org/html-foo/?bar=baz', last_response['Location']
  114. end
  115. end
  116. describe "/[doc+type]/[path]" do
  117. it "works when the doc exists" do
  118. get '/html/foo'
  119. assert last_response.ok?
  120. get '/html-bar/foo'
  121. assert last_response.ok?
  122. end
  123. it "returns 404 when the doc doesn't exist" do
  124. get '/foo/bar'
  125. assert last_response.not_found?
  126. end
  127. it "redirects without trailing slash" do
  128. get '/html/foo/'
  129. assert last_response.redirect?
  130. assert_equal 'http://example.org/html/foo', last_response['Location']
  131. get '/html/foo/', bar: 'baz'
  132. assert last_response.redirect?
  133. assert_equal 'http://example.org/html/foo?bar=baz', last_response['Location']
  134. end
  135. end
  136. describe "/feed" do
  137. it "returns an atom feed" do
  138. get '/feed'
  139. assert last_response.ok?
  140. assert_equal 'application/atom+xml', last_response['Content-Type']
  141. get '/feed.atom'
  142. assert last_response.ok?
  143. assert_equal 'application/atom+xml', last_response['Content-Type']
  144. end
  145. end
  146. describe "/s/[link]" do
  147. it "redirects" do
  148. %w(maxcdn shopify tw fb re).each do |link|
  149. get "/s/#{link}"
  150. assert last_response.redirect?
  151. end
  152. end
  153. end
  154. describe "/ping" do
  155. it "works" do
  156. get '/ping'
  157. assert last_response.ok?
  158. end
  159. end
  160. describe "404" do
  161. it "works" do
  162. get '/foo'
  163. assert last_response.not_found?
  164. end
  165. end
  166. end