This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 657
/
tests.py
114 lines (94 loc) · 4.34 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
import logging
from flask import Flask
from raven.base import Client
from raven.contrib.flask import Sentry
from unittest2 import TestCase
class TempStoreClient(Client):
def __init__(self, servers=None, **kwargs):
self.events = []
super(TempStoreClient, self).__init__(servers=servers, **kwargs)
def is_enabled(self):
return True
def send(self, **kwargs):
self.events.append(kwargs)
def create_app():
app = Flask(__name__)
@app.route('/an-error/', methods=['GET', 'POST'])
def an_error():
raise ValueError('hello world')
return app
class FlaskTest(TestCase):
def setUp(self):
self.app = create_app()
self.client = self.app.test_client()
def test_does_add_to_extensions(self):
client = TempStoreClient()
sentry = Sentry(self.app, client=client)
self.assertIn('sentry', self.app.extensions)
self.assertEquals(self.app.extensions['sentry'], sentry)
def test_error_handler(self):
client = TempStoreClient()
sentry = Sentry(self.app, client=client)
response = self.client.get('/an-error/')
self.assertEquals(response.status_code, 500)
self.assertEquals(len(client.events), 1)
event = client.events.pop(0)
self.assertTrue('sentry.interfaces.Exception' in event)
exc = event['sentry.interfaces.Exception']
self.assertEquals(exc['type'], 'ValueError')
self.assertEquals(exc['value'], 'hello world')
self.assertEquals(event['level'], logging.ERROR)
self.assertEquals(event['message'], 'ValueError: hello world')
self.assertEquals(event['culprit'], 'tests.contrib.flask.tests.an_error')
def test_get(self):
client = TempStoreClient()
sentry = Sentry(self.app, client=client)
response = self.client.get('/an-error/?foo=bar')
self.assertEquals(response.status_code, 500)
self.assertEquals(len(client.events), 1)
event = client.events.pop(0)
self.assertTrue('sentry.interfaces.Http' in event)
http = event['sentry.interfaces.Http']
self.assertEquals(http['url'], 'http://localhost/an-error/')
self.assertEquals(http['query_string'], 'foo=bar')
self.assertEquals(http['method'], 'GET')
self.assertEquals(http['data'], {})
self.assertTrue('headers' in http)
headers = http['headers']
self.assertTrue('Content-Length' in headers, headers.keys())
self.assertEquals(headers['Content-Length'], '0')
self.assertTrue('Content-Type' in headers, headers.keys())
self.assertEquals(headers['Content-Type'], '')
self.assertTrue('Host' in headers, headers.keys())
self.assertEquals(headers['Host'], 'localhost')
env = http['env']
self.assertTrue('SERVER_NAME' in env, env.keys())
self.assertEquals(env['SERVER_NAME'], 'localhost')
self.assertTrue('SERVER_PORT' in env, env.keys())
self.assertEquals(env['SERVER_PORT'], '80')
def test_post(self):
client = TempStoreClient()
sentry = Sentry(self.app, client=client)
response = self.client.post('/an-error/?biz=baz', data={'foo': 'bar'})
self.assertEquals(response.status_code, 500)
self.assertEquals(len(client.events), 1)
event = client.events.pop(0)
self.assertTrue('sentry.interfaces.Http' in event)
http = event['sentry.interfaces.Http']
self.assertEquals(http['url'], 'http://localhost/an-error/')
self.assertEquals(http['query_string'], 'biz=baz')
self.assertEquals(http['method'], 'POST')
self.assertEquals(http['data'], {'foo': 'bar'})
self.assertTrue('headers' in http)
headers = http['headers']
self.assertTrue('Content-Length' in headers, headers.keys())
self.assertEquals(headers['Content-Length'], '7')
self.assertTrue('Content-Type' in headers, headers.keys())
self.assertEquals(headers['Content-Type'], 'application/x-www-form-urlencoded')
self.assertTrue('Host' in headers, headers.keys())
self.assertEquals(headers['Host'], 'localhost')
env = http['env']
self.assertTrue('SERVER_NAME' in env, env.keys())
self.assertEquals(env['SERVER_NAME'], 'localhost')
self.assertTrue('SERVER_PORT' in env, env.keys())
self.assertEquals(env['SERVER_PORT'], '80')