-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_controller_spec.rb
More file actions
138 lines (107 loc) · 4.65 KB
/
errors_controller_spec.rb
File metadata and controls
138 lines (107 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# typed: false
# frozen_string_literal: true
require "rails_helper"
RSpec.describe ErrorsController, type: :request do
describe "GET /404 (not_found)" do
context "when requesting HTML format" do
it "renders the 404 page without requiring authentication" do
get "/404"
expect(response).to have_http_status(:not_found)
expect(response.body).to include(I18n.t("errors.not_found.title"))
expect(response.body).to include(I18n.t("errors.not_found.message"))
end
end
context "when requesting JSON format" do
it "returns JSON error response" do
get "/404", headers: {"Accept" => "application/json"}
expect(response).to have_http_status(:not_found)
expect(response.content_type).to match(/application\/json/)
json_response = JSON.parse(response.body)
expect(json_response["error"]).to eq(I18n.t("errors.not_found.title"))
end
end
context "when requesting other formats" do
it "returns only status code for XML format" do
get "/404", headers: {"Accept" => "application/xml"}
expect(response).to have_http_status(:not_found)
expect(response.body).to be_blank
end
it "returns only status code for text format" do
get "/404", headers: {"Accept" => "text/plain"}
expect(response).to have_http_status(:not_found)
expect(response.body).to be_blank
end
end
end
describe "GET /500 (internal_server_error)" do
context "when requesting HTML format" do
it "renders the 500 page without requiring authentication" do
get "/500"
expect(response).to have_http_status(:internal_server_error)
# Check that the error view is rendered (content is inside <main> tag)
expect(response.body).to include("<main>")
expect(response.body).to include(I18n.t("errors.internal_server_error.title"))
# The message might be escaped in HTML, so just check for key parts
expect(response.body).to match(/something went wrong/i)
end
end
context "when requesting JSON format" do
it "returns JSON error response" do
get "/500", headers: {"Accept" => "application/json"}
expect(response).to have_http_status(:internal_server_error)
expect(response.content_type).to match(/application\/json/)
json_response = JSON.parse(response.body)
expect(json_response["error"]).to eq(I18n.t("errors.internal_server_error.title"))
end
end
context "when requesting other formats" do
it "returns only status code for XML format" do
get "/500", headers: {"Accept" => "application/xml"}
expect(response).to have_http_status(:internal_server_error)
expect(response.body).to be_blank
end
it "returns only status code for text format" do
get "/500", headers: {"Accept" => "text/plain"}
expect(response).to have_http_status(:internal_server_error)
expect(response.body).to be_blank
end
end
end
describe "private methods" do
let(:controller) { ErrorsController.new }
describe "#capture_exception_for_sentry" do
context "when in production environment" do
before do
allow(Rails.env).to receive(:production?).and_return(true)
allow(Sentry).to receive(:capture_exception)
end
it "captures exception when present in request env" do
exception = StandardError.new("Test exception")
request = double(env: {"action_dispatch.exception" => exception})
allow(controller).to receive(:request).and_return(request)
controller.send(:capture_exception_for_sentry)
expect(Sentry).to have_received(:capture_exception).with(exception)
end
it "does not capture when no exception in request env" do
request = double(env: {})
allow(controller).to receive(:request).and_return(request)
controller.send(:capture_exception_for_sentry)
expect(Sentry).not_to have_received(:capture_exception)
end
end
context "when not in production environment" do
before do
allow(Rails.env).to receive(:production?).and_return(false)
allow(Sentry).to receive(:capture_exception)
end
it "does not capture exception even when present" do
exception = StandardError.new("Test exception")
request = double(env: {"action_dispatch.exception" => exception})
allow(controller).to receive(:request).and_return(request)
controller.send(:capture_exception_for_sentry)
expect(Sentry).not_to have_received(:capture_exception)
end
end
end
end
end