basic_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. require 'rails_helper'
  2. RSpec.describe 'Basics', type: :request do
  3. describe 'request handling' do
  4. it 'does json requests' do
  5. # 404
  6. get '/not_existing_url', as: :json
  7. expect(response).to have_http_status(:not_found)
  8. expect(json_response).to be_a_kind_of(Hash)
  9. expect(json_response['error']).to eq('No route matches [GET] /not_existing_url')
  10. # 401
  11. get '/api/v1/organizations', as: :json
  12. expect(response).to have_http_status(:unauthorized)
  13. expect(json_response).to be_a_kind_of(Hash)
  14. expect(json_response['error']).to eq('authentication failed')
  15. # 422
  16. get '/tests/unprocessable_entity', as: :json
  17. expect(response).to have_http_status(:unprocessable_entity)
  18. expect(json_response).to be_a_kind_of(Hash)
  19. expect(json_response['error']).to eq('some error message')
  20. # 401
  21. get '/tests/not_authorized', as: :json
  22. expect(response).to have_http_status(:unauthorized)
  23. expect(json_response).to be_a_kind_of(Hash)
  24. expect(json_response['error']).to eq('some error message')
  25. # 401
  26. get '/tests/ar_not_found', as: :json
  27. expect(response).to have_http_status(:not_found)
  28. expect(json_response).to be_a_kind_of(Hash)
  29. expect(json_response['error']).to eq('some error message')
  30. # 500
  31. get '/tests/standard_error', as: :json
  32. expect(response).to have_http_status(:internal_server_error)
  33. expect(json_response).to be_a_kind_of(Hash)
  34. expect(json_response['error']).to eq('some error message')
  35. # 422
  36. get '/tests/argument_error', as: :json
  37. expect(response).to have_http_status(:unprocessable_entity)
  38. expect(json_response).to be_a_kind_of(Hash)
  39. expect(json_response['error']).to eq('some error message')
  40. end
  41. it 'does html requests' do
  42. # 404
  43. get '/not_existing_url'
  44. expect(response).to have_http_status(:not_found)
  45. expect(response.body).to match(/<html/)
  46. expect(response.body).to match(%r{<title>404: Not Found</title>})
  47. expect(response.body).to match(%r{<h1>404: Requested resource was not found</h1>})
  48. expect(response.body).to match(%r{No route matches \[GET\] /not_existing_url})
  49. # 401
  50. get '/api/v1/organizations'
  51. expect(response).to have_http_status(:unauthorized)
  52. expect(response.body).to match(/<html/)
  53. expect(response.body).to match(%r{<title>401: Unauthorized</title>})
  54. expect(response.body).to match(%r{<h1>401: Unauthorized</h1>})
  55. expect(response.body).to match(/authentication failed/)
  56. # 422
  57. get '/tests/unprocessable_entity'
  58. expect(response).to have_http_status(:unprocessable_entity)
  59. expect(response.body).to match(/<html/)
  60. expect(response.body).to match(%r{<title>422: Unprocessable Entity</title>})
  61. expect(response.body).to match(%r{<h1>422: The change you wanted was rejected.</h1>})
  62. expect(response.body).to match(/some error message/)
  63. # 401
  64. get '/tests/not_authorized'
  65. expect(response).to have_http_status(:unauthorized)
  66. expect(response.body).to match(/<html/)
  67. expect(response.body).to match(%r{<title>401: Unauthorized</title>})
  68. expect(response.body).to match(%r{<h1>401: Unauthorized</h1>})
  69. expect(response.body).to match(/some error message/)
  70. # 401
  71. get '/tests/ar_not_found'
  72. expect(response).to have_http_status(:not_found)
  73. expect(response.body).to match(/<html/)
  74. expect(response.body).to match(%r{<title>404: Not Found</title>})
  75. expect(response.body).to match(%r{<h1>404: Requested resource was not found</h1>})
  76. expect(response.body).to match(/some error message/)
  77. # 500
  78. get '/tests/standard_error'
  79. expect(response).to have_http_status(:internal_server_error)
  80. expect(response.body).to match(/<html/)
  81. expect(response.body).to match(%r{<title>500: Something went wrong</title>})
  82. expect(response.body).to match(%r{<h1>500: We're sorry, but something went wrong.</h1>})
  83. expect(response.body).to match(/some error message/)
  84. # 422
  85. get '/tests/argument_error'
  86. expect(response).to have_http_status(:unprocessable_entity)
  87. expect(response.body).to match(/<html/)
  88. expect(response.body).to match(%r{<title>422: Unprocessable Entity</title>})
  89. expect(response.body).to match(%r{<h1>422: The change you wanted was rejected.</h1>})
  90. expect(response.body).to match(/some error message/)
  91. end
  92. end
  93. end