1
|
|
|
#!/usr/bin/env python |
2
|
|
|
# -*- coding: utf-8 -*- |
3
|
|
|
# |
4
|
|
|
# Copyright (c) 2016 dotzero <[email protected]> |
5
|
|
|
# |
6
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
# of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
# in the Software without restriction, including without limitation the rights |
9
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
# copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
# furnished to do so, subject to the following conditions: |
12
|
|
|
# |
13
|
|
|
# The above copyright notice and this permission notice shall be included |
14
|
|
|
# in all copies or substantial portions of the Software. |
15
|
|
|
# |
16
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
# SOFTWARE. |
23
|
|
|
|
24
|
|
|
"""This module contains a object that represents Tests for BaseResource""" |
25
|
|
|
|
26
|
|
|
import unittest |
27
|
|
|
import sys |
28
|
|
|
|
29
|
|
|
sys.path.append('.') |
30
|
|
|
|
31
|
|
|
import habrahabr |
32
|
|
|
from tests.base import BaseTest |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
class BaseResourceTest(BaseTest, unittest.TestCase): |
36
|
|
|
"""This object represents Tests for BaseResource.""" |
37
|
|
|
|
38
|
|
|
def setUp(self): |
39
|
|
|
auth = habrahabr.Auth(client='foo.bar', token='foobar') |
40
|
|
|
auth._endpoint = 'https://httpbin.org' |
41
|
|
|
self.base = habrahabr.BaseResource(auth) |
42
|
|
|
|
43
|
|
|
def test_request_get(self): |
44
|
|
|
"""Test GET BaseResource._request(url, method, data) method""" |
45
|
|
|
r = self.base._request('/get', 'GET', { |
46
|
|
|
'foo': 'bar' |
47
|
|
|
}) |
48
|
|
|
|
49
|
|
|
self.assertEqual(r['url'], 'https://httpbin.org/get?foo=bar') |
50
|
|
|
self.assertEqual(r['headers']['Client'], 'foo.bar') |
51
|
|
|
self.assertEqual(r['headers']['Token'], 'foobar') |
52
|
|
|
|
53
|
|
|
def test_request_post(self): |
54
|
|
|
"""Test POST BaseResource._request(url, method, data) method""" |
55
|
|
|
r = self.base._request('/post', 'POST', { |
56
|
|
|
'foo': 'bar' |
57
|
|
|
}) |
58
|
|
|
|
59
|
|
|
self.assertEqual(r['url'], 'https://httpbin.org/post') |
60
|
|
|
self.assertEqual(r['headers']['Client'], 'foo.bar') |
61
|
|
|
self.assertEqual(r['headers']['Token'], 'foobar') |
62
|
|
|
self.assertEqual(r['form']['foo'], 'bar') |
63
|
|
|
|
64
|
|
|
def test_request_put(self): |
65
|
|
|
"""Test PUT BaseResource._request(url, method, data) method""" |
66
|
|
|
r = self.base._request('/put', 'PUT', { |
67
|
|
|
'foo': 'bar' |
68
|
|
|
}) |
69
|
|
|
|
70
|
|
|
self.assertEqual(r['url'], 'https://httpbin.org/put') |
71
|
|
|
self.assertEqual(r['headers']['Client'], 'foo.bar') |
72
|
|
|
self.assertEqual(r['headers']['Token'], 'foobar') |
73
|
|
|
self.assertEqual(r['form']['foo'], 'bar') |
74
|
|
|
|
75
|
|
|
def test_request_delete(self): |
76
|
|
|
"""Test DELETE BaseResource._request(url, method, data) method""" |
77
|
|
|
r = self.base._request('/delete', 'DELETE', { |
78
|
|
|
'foo': 'bar' |
79
|
|
|
}) |
80
|
|
|
self.assertEqual(r['url'], 'https://httpbin.org/delete?foo=bar') |
81
|
|
|
self.assertEqual(r['headers']['Client'], 'foo.bar') |
82
|
|
|
self.assertEqual(r['headers']['Token'], 'foobar') |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
if __name__ == '__main__': |
86
|
|
|
unittest.main() |
87
|
|
|
|