notice_spec.rb
4.28 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
describe Notice, type: 'model' do
context 'validations' do
it 'requires a backtrace' do
notice = Fabricate.build(:notice, :backtrace => nil)
expect(notice).to_not be_valid
expect(notice.errors[:backtrace_id]).to include("can't be blank")
end
it 'requires the server_environment' do
notice = Fabricate.build(:notice, :server_environment => nil)
expect(notice).to_not be_valid
expect(notice.errors[:server_environment]).to include("can't be blank")
end
it 'requires the notifier' do
notice = Fabricate.build(:notice, :notifier => nil)
expect(notice).to_not be_valid
expect(notice.errors[:notifier]).to include("can't be blank")
end
end
describe "key sanitization" do
before do
@hash = { "some.key" => { "$nested.key" => {"$Path" => "/", "some$key" => "key"}}}
@hash_sanitized = { "some.key" => { "$nested.key" => {"$Path" => "/", "some$key" => "key"}}}
end
[:server_environment, :request, :notifier].each do |key|
it "replaces . with . and $ with $ in keys used in #{key}" do
err = Fabricate(:err)
notice = Fabricate(:notice, :err => err, key => @hash)
expect(notice.send(key)).to eq @hash_sanitized
end
end
end
describe "to_curl" do
let(:notice) { Fabricate.build(:notice, request: request) }
context "when it has a request url" do
let(:request) { {'url' => "http://example.com/resource/12", 'cgi-data' => {'HTTP_USER_AGENT' => 'Mozilla/5.0'}} }
it 'has a curl representation' do
cmd = notice.to_curl
expect(cmd).to eq(%q[curl -X GET -H 'User-Agent: Mozilla/5.0' http://example.com/resource/12])
end
end
context "when it has not a request url" do
let(:request) { {'cgi-data' => {'HTTP_USER_AGENT' => 'Mozilla/5.0'}} }
it 'has a curl representation' do
cmd = notice.to_curl
expect(cmd).to eq "N/A"
end
end
end
describe "user agent" do
it "should be parsed and human-readable" do
notice = Fabricate.build(:notice, :request => {'cgi-data' => {
'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16'
}})
expect(notice.user_agent.browser).to eq 'Chrome'
expect(notice.user_agent.version.to_s).to match( /^10\.0/ )
end
it "should be nil if HTTP_USER_AGENT is blank" do
notice = Fabricate.build(:notice)
expect(notice.user_agent).to eq nil
end
end
describe "user agent string" do
it "should be parsed and human-readable" do
notice = Fabricate.build(:notice, :request => {'cgi-data' => {'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16'}})
expect(notice.user_agent_string).to eq 'Chrome 10.0.648.204 (OS X 10.6.7)'
end
it "should be nil if HTTP_USER_AGENT is blank" do
notice = Fabricate.build(:notice)
expect(notice.user_agent_string).to eq "N/A"
end
end
describe "host" do
it "returns host if url is valid" do
notice = Fabricate.build(:notice, :request => {'url' => "http://example.com/resource/12"})
expect(notice.host).to eq 'example.com'
end
it "returns 'N/A' when url is not valid" do
notice = Fabricate.build(:notice, :request => {'url' => "file:///path/to/some/resource/12"})
expect(notice.host).to eq 'N/A'
end
it "returns 'N/A' when url is not valid" do
notice = Fabricate.build(:notice, :request => {'url' => "some string"})
expect(notice.host).to eq 'N/A'
end
it "returns 'N/A' when url is empty" do
notice = Fabricate.build(:notice, :request => {})
expect(notice.host).to eq 'N/A'
end
end
describe "request" do
it "returns empty hash if not set" do
notice = Notice.new
expect(notice.request).to eq({})
end
end
describe "env_vars" do
it "returns the cgi-data" do
notice = Notice.new
notice.request = { 'cgi-data' => { 'ONE' => 'TWO' } }
expect(notice.env_vars).to eq({ 'ONE' => 'TWO' })
end
it "always returns a hash" do
notice = Notice.new
notice.request = { 'cgi-data' => [] }
expect(notice.env_vars).to eq({})
end
end
end