| changeset 15 | 5d552b6a0e55 |
| parent 12 | 4daf47fcf792 |
| child 207 | 621fa6caec0c |
| 12:4daf47fcf792 | 15:5d552b6a0e55 |
|---|---|
4 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
4 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
5 |
5 |
6 from nose.tools import assert_raises |
6 from nose.tools import assert_raises |
7 from tweetstream import TweetStream, FollowStream, TrackStream |
7 from tweetstream import TweetStream, FollowStream, TrackStream |
8 from tweetstream import ConnectionError, AuthenticationError |
8 from tweetstream import ConnectionError, AuthenticationError |
9 from tweetstream import auth |
|
9 |
10 |
10 from servercontext import test_server |
11 from servercontext import test_server |
11 |
12 |
12 single_tweet = r"""{"in_reply_to_status_id":null,"in_reply_to_user_id":null,"favorited":false,"created_at":"Tue Jun 16 10:40:14 +0000 2009","in_reply_to_screen_name":null,"text":"record industry just keeps on amazing me: http:\/\/is.gd\/13lFo - $150k per song you've SHARED, not that somebody has actually DOWNLOADED.","user":{"notifications":null,"profile_background_tile":false,"followers_count":206,"time_zone":"Copenhagen","utc_offset":3600,"friends_count":191,"profile_background_color":"ffffff","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/250715794\/profile_normal.png","description":"Digital product developer, currently at Opera Software. My tweets are my opinions, not those of my employer.","verified_profile":false,"protected":false,"favourites_count":0,"profile_text_color":"3C3940","screen_name":"eiriksnilsen","name":"Eirik Stridsklev N.","following":null,"created_at":"Tue May 06 12:24:12 +0000 2008","profile_background_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_background_images\/10531192\/160x600opera15.gif","profile_link_color":"0099B9","profile_sidebar_fill_color":"95E8EC","url":"http:\/\/www.stridsklev-nilsen.no\/eirik","id":14672543,"statuses_count":506,"profile_sidebar_border_color":"5ED4DC","location":"Oslo, Norway"},"id":2190767504,"truncated":false,"source":"<a href=\"http:\/\/widgets.opera.com\/widget\/7206\">Twitter Opera widget<\/a>"}""" |
13 single_tweet = r"""{"in_reply_to_status_id":null,"in_reply_to_user_id":null,"favorited":false,"created_at":"Tue Jun 16 10:40:14 +0000 2009","in_reply_to_screen_name":null,"text":"record industry just keeps on amazing me: http:\/\/is.gd\/13lFo - $150k per song you've SHARED, not that somebody has actually DOWNLOADED.","user":{"notifications":null,"profile_background_tile":false,"followers_count":206,"time_zone":"Copenhagen","utc_offset":3600,"friends_count":191,"profile_background_color":"ffffff","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/250715794\/profile_normal.png","description":"Digital product developer, currently at Opera Software. My tweets are my opinions, not those of my employer.","verified_profile":false,"protected":false,"favourites_count":0,"profile_text_color":"3C3940","screen_name":"eiriksnilsen","name":"Eirik Stridsklev N.","following":null,"created_at":"Tue May 06 12:24:12 +0000 2008","profile_background_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_background_images\/10531192\/160x600opera15.gif","profile_link_color":"0099B9","profile_sidebar_fill_color":"95E8EC","url":"http:\/\/www.stridsklev-nilsen.no\/eirik","id":14672543,"statuses_count":506,"profile_sidebar_border_color":"5ED4DC","location":"Oslo, Norway"},"id":2190767504,"truncated":false,"source":"<a href=\"http:\/\/widgets.opera.com\/widget\/7206\">Twitter Opera widget<\/a>"}""" |
13 |
14 |
18 def auth_denied(request): |
19 def auth_denied(request): |
19 request.send_error(401) |
20 request.send_error(401) |
20 |
21 |
21 with test_server(handler=auth_denied, methods=("post", "get"), |
22 with test_server(handler=auth_denied, methods=("post", "get"), |
22 port="random") as server: |
23 port="random") as server: |
23 stream = TweetStream("foo", "bar", url=server.baseurl) |
24 stream = TweetStream(auth.BasicAuthHandler("foo", "bar"), url=server.baseurl) |
24 assert_raises(AuthenticationError, stream.next) |
25 assert_raises(AuthenticationError, stream.next) |
25 |
26 |
26 stream = FollowStream("foo", "bar", [1, 2, 3], url=server.baseurl) |
27 stream = FollowStream(auth.BasicAuthHandler("foo", "bar"), [1, 2, 3], url=server.baseurl) |
27 assert_raises(AuthenticationError, stream.next) |
28 assert_raises(AuthenticationError, stream.next) |
28 |
29 |
29 stream = TrackStream("foo", "bar", ["opera"], url=server.baseurl) |
30 stream = TrackStream(auth.BasicAuthHandler("foo", "bar"), ["opera"], url=server.baseurl) |
30 assert_raises(AuthenticationError, stream.next) |
31 assert_raises(AuthenticationError, stream.next) |
31 |
32 |
32 |
33 |
33 def test_404_url(): |
34 def test_404_url(): |
34 """Test that the proper exception is raised when the stream URL can't be |
35 """Test that the proper exception is raised when the stream URL can't be |
36 def not_found(request): |
37 def not_found(request): |
37 request.send_error(404) |
38 request.send_error(404) |
38 |
39 |
39 with test_server(handler=not_found, methods=("post", "get"), |
40 with test_server(handler=not_found, methods=("post", "get"), |
40 port="random") as server: |
41 port="random") as server: |
41 stream = TweetStream("foo", "bar", url=server.baseurl) |
42 stream = TweetStream(auth.BasicAuthHandler("foo", "bar"), url=server.baseurl) |
42 assert_raises(ConnectionError, stream.next) |
43 assert_raises(ConnectionError, stream.next) |
43 |
44 |
44 stream = FollowStream("foo", "bar", [1, 2, 3], url=server.baseurl) |
45 stream = FollowStream(auth.BasicAuthHandler("foo", "bar"), [1, 2, 3], url=server.baseurl) |
45 assert_raises(ConnectionError, stream.next) |
46 assert_raises(ConnectionError, stream.next) |
46 |
47 |
47 stream = TrackStream("foo", "bar", ["opera"], url=server.baseurl) |
48 stream = TrackStream(auth.BasicAuthHandler("foo", "bar"), ["opera"], url=server.baseurl) |
48 assert_raises(ConnectionError, stream.next) |
49 assert_raises(ConnectionError, stream.next) |
49 |
50 |
50 |
51 |
51 def test_bad_content(): |
52 def test_bad_content(): |
52 """Test error handling if we are given invalid data""" |
53 """Test error handling if we are given invalid data""" |
59 yield "[1,2,3]" |
60 yield "[1,2,3]" |
60 |
61 |
61 def do_test(klass, *args): |
62 def do_test(klass, *args): |
62 with test_server(handler=bad_content, methods=("post", "get"), |
63 with test_server(handler=bad_content, methods=("post", "get"), |
63 port="random") as server: |
64 port="random") as server: |
64 stream = klass("foo", "bar", *args, url=server.baseurl) |
65 stream = klass(auth.BasicAuthHandler("foo", "bar"), *args, url=server.baseurl) |
65 for tweet in stream: |
66 for tweet in stream: |
66 pass |
67 pass |
67 |
68 |
68 assert_raises(ConnectionError, do_test, TweetStream) |
69 assert_raises(ConnectionError, do_test, TweetStream) |
69 assert_raises(ConnectionError, do_test, FollowStream, [1, 2, 3]) |
70 assert_raises(ConnectionError, do_test, FollowStream, [1, 2, 3]) |
80 yield "[1,2,3]" |
81 yield "[1,2,3]" |
81 |
82 |
82 def do_test(klass, *args): |
83 def do_test(klass, *args): |
83 with test_server(handler=bad_content, methods=("post", "get"), |
84 with test_server(handler=bad_content, methods=("post", "get"), |
84 port="random") as server: |
85 port="random") as server: |
85 stream = klass("foo", "bar", *args, url=server.baseurl) |
86 stream = klass(auth.BasicAuthHandler("foo", "bar"), *args, url=server.baseurl) |
86 for tweet in stream: |
87 for tweet in stream: |
87 pass |
88 pass |
88 |
89 |
89 assert_raises(ConnectionError, do_test, TweetStream) |
90 assert_raises(ConnectionError, do_test, TweetStream) |
90 assert_raises(ConnectionError, do_test, FollowStream, [1, 2, 3]) |
91 assert_raises(ConnectionError, do_test, FollowStream, [1, 2, 3]) |
91 assert_raises(ConnectionError, do_test, TrackStream, ["opera"]) |
92 assert_raises(ConnectionError, do_test, TrackStream, ["opera"]) |
92 |
93 |
93 |
94 |
94 def test_bad_host(): |
95 def test_bad_host(): |
95 """Test behaviour if we can't connect to the host""" |
96 """Test behaviour if we can't connect to the host""" |
96 stream = TweetStream("foo", "bar", url="http://bad.egewdvsdswefdsf.com/") |
97 stream = TweetStream(auth.BasicAuthHandler("foo", "bar"), url="http://bad.egewdvsdswefdsf.com/") |
97 assert_raises(ConnectionError, stream.next) |
98 assert_raises(ConnectionError, stream.next) |
98 |
99 |
99 stream = FollowStream("foo", "bar", [1, 2, 3], url="http://zegwefdsf.com/") |
100 stream = FollowStream(auth.BasicAuthHandler("foo", "bar"), [1, 2, 3], url="http://zegwefdsf.com/") |
100 assert_raises(ConnectionError, stream.next) |
101 assert_raises(ConnectionError, stream.next) |
101 |
102 |
102 stream = TrackStream("foo", "bar", ["foo"], url="http://aswefdsews.com/") |
103 stream = TrackStream(auth.BasicAuthHandler("foo", "bar"), ["foo"], url="http://aswefdsews.com/") |
103 assert_raises(ConnectionError, stream.next) |
104 assert_raises(ConnectionError, stream.next) |
104 |
105 |
105 |
106 |
106 def smoke_test_receive_tweets(): |
107 def smoke_test_receive_tweets(): |
107 """Receive 100k tweets and disconnect (slow)""" |
108 """Receive 100k tweets and disconnect (slow)""" |
112 yield single_tweet + "\n" |
113 yield single_tweet + "\n" |
113 |
114 |
114 def do_test(klass, *args): |
115 def do_test(klass, *args): |
115 with test_server(handler=tweetsource, |
116 with test_server(handler=tweetsource, |
116 methods=("post", "get"), port="random") as server: |
117 methods=("post", "get"), port="random") as server: |
117 stream = klass("foo", "bar", *args, url=server.baseurl) |
118 stream = klass(auth.BasicAuthHandler("foo", "bar"), *args, url=server.baseurl) |
118 for tweet in stream: |
119 for tweet in stream: |
119 if stream.count == total: |
120 if stream.count == total: |
120 break |
121 break |
121 |
122 |
122 do_test(TweetStream) |
123 do_test(TweetStream) |
143 yield "\n" |
144 yield "\n" |
144 |
145 |
145 def do_test(klass, *args): |
146 def do_test(klass, *args): |
146 with test_server(handler=tweetsource, methods=("post", "get"), |
147 with test_server(handler=tweetsource, methods=("post", "get"), |
147 port="random") as server: |
148 port="random") as server: |
148 stream = klass("foo", "bar", *args, url=server.baseurl) |
149 stream = klass(auth.BasicAuthHandler("foo", "bar"), *args, url=server.baseurl) |
149 try: |
150 try: |
150 for tweet in stream: |
151 for tweet in stream: |
151 pass |
152 pass |
152 except ConnectionError: |
153 except ConnectionError: |
153 assert stream.count == 3, "Got %s, wanted 3" % stream.count |
154 assert stream.count == 3, "Got %s, wanted 3" % stream.count |
176 yield single_tweet+"\n" |
177 yield single_tweet+"\n" |
177 |
178 |
178 def do_test(klass, *args): |
179 def do_test(klass, *args): |
179 with test_server(handler=tweetsource, methods=("post", "get"), |
180 with test_server(handler=tweetsource, methods=("post", "get"), |
180 port="random") as server: |
181 port="random") as server: |
181 stream = klass("foo", "bar", *args, url=server.baseurl) |
182 stream = klass(auth.BasicAuthHandler("foo", "bar"), *args, url=server.baseurl) |
182 |
183 |
183 start = time.time() |
184 start = time.time() |
184 stream.next() |
185 stream.next() |
185 first = time.time() |
186 first = time.time() |
186 diff = first - start |
187 diff = first - start |