basic_controller_test.rb 4.2 KB

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