/
tests.py
153 lines (127 loc) · 6.82 KB
/
tests.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import mock
from sentry.tasks.fetch_source import (
UrlResult, expand_javascript_source, discover_sourcemap,
fetch_sourcemap, generate_module)
from sentry.utils.sourcemaps import (SourceMap, SourceMapIndex)
from sentry.testutils import TestCase
base64_sourcemap = 'data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZ2VuZXJhdGVkLmpzIiwic291cmNlcyI6WyIvdGVzdC5qcyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEiLCJzb3VyY2VzQ29udGVudCI6WyJjb25zb2xlLmxvZyhcImhlbGxvLCBXb3JsZCFcIikiXX0='
class DiscoverSourcemapTest(TestCase):
# discover_sourcemap(result)
def test_simple(self):
result = UrlResult('http://example.com', {}, '')
assert discover_sourcemap(result) is None
result = UrlResult('http://example.com', {
'x-sourcemap': 'http://example.com/source.map.js'
}, '')
assert discover_sourcemap(result) == 'http://example.com/source.map.js'
result = UrlResult('http://example.com', {
'sourcemap': 'http://example.com/source.map.js'
}, '')
assert discover_sourcemap(result) == 'http://example.com/source.map.js'
result = UrlResult('http://example.com', {}, '//@ sourceMappingURL=http://example.com/source.map.js\nconsole.log(true)')
assert discover_sourcemap(result) == 'http://example.com/source.map.js'
result = UrlResult('http://example.com', {}, '//# sourceMappingURL=http://example.com/source.map.js\nconsole.log(true)')
assert discover_sourcemap(result) == 'http://example.com/source.map.js'
result = UrlResult('http://example.com', {}, 'console.log(true)\n//@ sourceMappingURL=http://example.com/source.map.js')
assert discover_sourcemap(result) == 'http://example.com/source.map.js'
result = UrlResult('http://example.com', {}, 'console.log(true)\n//# sourceMappingURL=http://example.com/source.map.js')
assert discover_sourcemap(result) == 'http://example.com/source.map.js'
class ExpandJavascriptSourceTest(TestCase):
@mock.patch('sentry.models.Event.update')
@mock.patch('sentry.tasks.fetch_source.fetch_url')
@mock.patch('sentry.tasks.fetch_source.fetch_sourcemap')
@mock.patch('sentry.tasks.fetch_source.discover_sourcemap')
def test_simple(self, discover_sourcemap, fetch_sourcemap, fetch_url, update):
data = {
'sentry.interfaces.Exception': {
'values': [{
'stacktrace': {
'frames': [
{
'abs_path': 'http://example.com/foo.js',
'filename': 'foo.js',
'lineno': 4,
'colno': 0,
},
{
'abs_path': 'http://example.com/foo.js',
'filename': 'foo.js',
'lineno': 1,
'colno': 0,
},
],
},
}],
}
}
discover_sourcemap.return_value = None
fetch_sourcemap.return_value = None
fetch_url.return_value.body = '\n'.join('hello world')
expand_javascript_source(data)
fetch_url.assert_called_once_with('http://example.com/foo.js')
frame_list = data['sentry.interfaces.Exception']['values'][0]['stacktrace']['frames']
frame = frame_list[0]
assert frame['pre_context'] == ['h', 'e', 'l']
assert frame['context_line'] == 'l'
assert frame['post_context'] == ['o', ' ', 'w', 'o', 'r']
frame = frame_list[1]
assert frame['pre_context'] == []
assert frame['context_line'] == 'h'
assert frame['post_context'] == ['e', 'l', 'l', 'o', ' ']
@mock.patch('sentry.models.Event.update')
@mock.patch('sentry.tasks.fetch_source.fetch_url')
@mock.patch('sentry.tasks.fetch_source.discover_sourcemap')
def test_inlined_sources(self, discover_sourcemap, fetch_url, update):
data = {
'sentry.interfaces.Exception': {
'values': [{
'stacktrace': {
'frames': [
{
'abs_path': 'http://example.com/test.js',
'filename': 'test.js',
'lineno': 1,
'colno': 0,
},
],
},
}],
}
}
discover_sourcemap.return_value = base64_sourcemap
fetch_url.return_value.body = '\n'.join('<generated source>')
expand_javascript_source(data)
fetch_url.assert_called_once_with('http://example.com/test.js')
frame_list = data['sentry.interfaces.Exception']['values'][0]['stacktrace']['frames']
frame = frame_list[0]
assert frame['pre_context'] == []
assert frame['context_line'] == 'console.log("hello, World!")'
assert frame['post_context'] == []
class GenerateModuleTest(TestCase):
def test_simple(self):
assert generate_module(None) == '<unknown module>'
assert generate_module('http://example.com/foo.js') == 'foo'
assert generate_module('http://example.com/foo/bar.js') == 'foo/bar'
assert generate_module('http://example.com/js/foo/bar.js') == 'foo/bar'
assert generate_module('http://example.com/javascript/foo/bar.js') == 'foo/bar'
assert generate_module('http://example.com/1.0/foo/bar.js') == 'foo/bar'
assert generate_module('http://example.com/v1/foo/bar.js') == 'foo/bar'
assert generate_module('http://example.com/v1.0.0/foo/bar.js') == 'foo/bar'
assert generate_module('http://example.com/_baz/foo/bar.js') == 'foo/bar'
assert generate_module('http://example.com/1/2/3/foo/bar.js') == 'foo/bar'
assert generate_module('http://example.com/abcdef0/foo/bar.js') == 'foo/bar'
assert generate_module('http://example.com/92cd589eca8235e7b373bf5ae94ebf898e3b949c/foo/bar.js') == 'foo/bar'
assert generate_module('http://example.com/7d6d00eae0ceccdc7ee689659585d95f/foo/bar.js') == 'foo/bar'
assert generate_module('/foo/bar.js') == 'foo/bar'
assert generate_module('../../foo/bar.js') == 'foo/bar'
assert generate_module('/foo/bar-7d6d00eae0ceccdc7ee689659585d95f.js') == 'foo/bar'
class FetchBase64SourcemapTest(TestCase):
def test_simple(self):
index = fetch_sourcemap(base64_sourcemap)
states = [SourceMap(1, 0, '/test.js', 0, 0, None)]
sources = set(['/test.js'])
keys = [(1, 0)]
content = {'/test.js': ['console.log("hello, World!")']}
assert index == SourceMapIndex(states, keys, sources, content)