3
3
4
4
from __future__ import unicode_literals , print_function
5
5
6
- import json
7
- import os
8
6
import re
9
- import sys
10
- from tempfile import NamedTemporaryFile
11
- import unittest
12
- try :
13
- from unittest .mock import patch
14
- except ImportError :
15
- from mock import patch
16
- import warnings
17
7
18
8
import twitter
19
9
20
10
import responses
21
11
from responses import GET , POST
22
12
23
- DEFAULT_URL = re .compile (r'https?://.*\.twitter.com/1\.1/.*' )
13
+ DEFAULT_BASE_URL = re .compile (r'https?://api\.twitter.com/1\.1/.*' )
14
+ DEFAULT_UPLOAD_URL = re .compile (r'https?://upload\.twitter.com/1\.1/.*' )
24
15
25
16
global api
26
17
api = twitter .Api ('test' , 'test' , 'test' , 'test' , tweet_mode = 'extended' )
30
21
def test_get_direct_messages ():
31
22
with open ('testdata/direct_messages/get_direct_messages.json' ) as f :
32
23
resp_data = f .read ()
33
- responses .add (GET , DEFAULT_URL , body = resp_data )
24
+ responses .add (GET , DEFAULT_BASE_URL , body = resp_data )
34
25
35
26
resp = api .GetDirectMessages (count = 1 , page = 1 )
36
27
direct_message = resp [0 ]
@@ -49,7 +40,7 @@ def test_get_direct_messages():
49
40
def test_get_sent_direct_messages ():
50
41
with open ('testdata/direct_messages/get_sent_direct_messages.json' ) as f :
51
42
resp_data = f .read ()
52
- responses .add (GET , DEFAULT_URL , body = resp_data )
43
+ responses .add (GET , DEFAULT_BASE_URL , body = resp_data )
53
44
54
45
resp = api .GetSentDirectMessages (count = 1 , page = 1 )
55
46
direct_message = resp [0 ]
@@ -61,7 +52,7 @@ def test_get_sent_direct_messages():
61
52
@responses .activate
62
53
def test_post_direct_message ():
63
54
with open ('testdata/direct_messages/post_post_direct_message.json' , 'r' ) as f :
64
- responses .add (POST , DEFAULT_URL , body = f .read ())
55
+ responses .add (POST , DEFAULT_BASE_URL , body = f .read ())
65
56
resp = api .PostDirectMessage (user_id = '372018022' ,
66
57
text = 'hello' )
67
58
assert isinstance (resp , twitter .DirectMessage )
@@ -71,10 +62,13 @@ def test_post_direct_message():
71
62
@responses .activate
72
63
def test_post_direct_message_with_media ():
73
64
with open ('testdata/direct_messages/post_post_direct_message.json' , 'r' ) as f :
74
- responses .add (POST , DEFAULT_URL , body = f .read ())
65
+ responses .add (POST , DEFAULT_BASE_URL , body = f .read ())
66
+ with open ('testdata/post_upload_chunked_INIT.json' ) as f :
67
+ responses .add (POST , DEFAULT_UPLOAD_URL , body = f .read ())
68
+
75
69
resp = api .PostDirectMessage (user_id = '372018022' ,
76
70
text = 'hello' ,
77
- media_file_path = 'testdata/media/happy.png ' ,
71
+ media_file_path = 'testdata/media/happy.jpg ' ,
78
72
media_type = 'dm_image' )
79
73
assert isinstance (resp , twitter .DirectMessage )
80
74
assert resp .text == 'hello'
@@ -83,7 +77,7 @@ def test_post_direct_message_with_media():
83
77
@responses .activate
84
78
def test_destroy_direct_message ():
85
79
with open ('testdata/direct_messages/post_destroy_direct_message.json' , 'r' ) as f :
86
- responses .add (POST , DEFAULT_URL , body = f .read ())
80
+ responses .add (POST , DEFAULT_BASE_URL , body = f .read ())
87
81
resp = api .DestroyDirectMessage (message_id = 855194351294656515 )
88
82
89
83
assert isinstance (resp , twitter .DirectMessage )
0 commit comments