basic_controller_test.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class BasicControllerTest < ActionDispatch::IntegrationTest
  4. test 'json requests' do
  5. @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
  6. # 404
  7. get '/not_existing_url', {}, @headers
  8. assert_response(404)
  9. result = JSON.parse(@response.body)
  10. assert_equal(result.class, Hash)
  11. assert(result['error'], 'No route matches [GET] /not_existing_url')
  12. # 401
  13. get '/api/v1/organizations', {}, @headers
  14. assert_response(401)
  15. result = JSON.parse(@response.body)
  16. assert_equal(result.class, Hash)
  17. assert(result['error'], 'authentication failed')
  18. # 422
  19. get '/tests/unprocessable_entity', {}, @headers
  20. assert_response(422)
  21. result = JSON.parse(@response.body)
  22. assert_equal(result.class, Hash)
  23. assert(result['error'], 'some error message')
  24. # 401
  25. get '/tests/not_authorized', {}, @headers
  26. assert_response(401)
  27. result = JSON.parse(@response.body)
  28. assert_equal(result.class, Hash)
  29. assert(result['error'], 'some error message')
  30. # 401
  31. get '/tests/ar_not_found', {}, @headers
  32. assert_response(404)
  33. result = JSON.parse(@response.body)
  34. assert_equal(result.class, Hash)
  35. assert(result['error'], 'some error message')
  36. # 500
  37. get '/tests/standard_error', {}, @headers
  38. assert_response(500)
  39. result = JSON.parse(@response.body)
  40. assert_equal(result.class, Hash)
  41. assert(result['error'], 'some error message')
  42. # 422
  43. get '/tests/argument_error', {}, @headers
  44. assert_response(422)
  45. result = JSON.parse(@response.body)
  46. assert_equal(result.class, Hash)
  47. assert(result['error'], 'some error message')
  48. end
  49. test 'html requests' do
  50. # 404
  51. get '/not_existing_url', {}, @headers
  52. assert_response(404)
  53. assert_match(/<html/, @response.body)
  54. assert_match(%r{<title>404: Not Found</title>}, @response.body)
  55. assert_match(%r{<h1>404: Requested Ressource was not found.</h1>}, @response.body)
  56. assert_match(%r{No route matches \[GET\] /not_existing_url}, @response.body)
  57. # 401
  58. get '/api/v1/organizations', {}, @headers
  59. assert_response(401)
  60. assert_match(/<html/, @response.body)
  61. assert_match(%r{<title>401: Unauthorized</title>}, @response.body)
  62. assert_match(%r{<h1>401: Unauthorized</h1>}, @response.body)
  63. assert_match(/authentication failed/, @response.body)
  64. # 422
  65. get '/tests/unprocessable_entity', {}, @headers
  66. assert_response(422)
  67. assert_match(/<html/, @response.body)
  68. assert_match(%r{<title>422: Unprocessable Entity</title>}, @response.body)
  69. assert_match(%r{<h1>422: The change you wanted was rejected.</h1>}, @response.body)
  70. assert_match(/some error message/, @response.body)
  71. # 401
  72. get '/tests/not_authorized', {}, @headers
  73. assert_response(401)
  74. assert_match(/<html/, @response.body)
  75. assert_match(%r{<title>401: Unauthorized</title>}, @response.body)
  76. assert_match(%r{<h1>401: Unauthorized</h1>}, @response.body)
  77. assert_match(/some error message/, @response.body)
  78. # 401
  79. get '/tests/ar_not_found', {}, @headers
  80. assert_response(404)
  81. assert_match(/<html/, @response.body)
  82. assert_match(%r{<title>404: Not Found</title>}, @response.body)
  83. assert_match(%r{<h1>404: Requested Ressource was not found.</h1>}, @response.body)
  84. assert_match(/some error message/, @response.body)
  85. # 500
  86. get '/tests/standard_error', {}, @headers
  87. assert_response(500)
  88. assert_match(/<html/, @response.body)
  89. assert_match(%r{<title>500: Something went wrong</title>}, @response.body)
  90. assert_match(%r{<h1>500: We're sorry, but something went wrong.</h1>}, @response.body)
  91. assert_match(/some error message/, @response.body)
  92. # 422
  93. get '/tests/argument_error', {}, @headers
  94. assert_response(422)
  95. assert_match(/<html/, @response.body)
  96. assert_match(%r{<title>422: Unprocessable Entity</title>}, @response.body)
  97. assert_match(%r{<h1>422: The change you wanted was rejected.</h1>}, @response.body)
  98. assert_match(/some error message/, @response.body)
  99. end
  100. end