|
1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
|
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
|
3
|
|
|
# this work for additional information regarding copyright ownership. |
|
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
|
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
|
6
|
|
|
# the License. You may obtain a copy of the License at |
|
7
|
|
|
# |
|
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
|
9
|
|
|
# |
|
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
|
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
|
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13
|
|
|
# See the License for the specific language governing permissions and |
|
14
|
|
|
# limitations under the License. |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
import mock |
|
18
|
|
|
import unittest2 |
|
19
|
|
|
|
|
20
|
|
|
from http_runner import HTTPClient |
|
21
|
|
|
import st2tests.config as tests_config |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
class MockResult(object): |
|
25
|
|
|
close = mock.Mock() |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class HTTPRunnerTestCase(unittest2.TestCase): |
|
29
|
|
|
@classmethod |
|
30
|
|
|
def setUpClass(cls): |
|
31
|
|
|
tests_config.parse_args() |
|
32
|
|
|
|
|
33
|
|
|
@mock.patch('http_runner.requests') |
|
34
|
|
|
def test_parse_response_body(self, mock_requests): |
|
35
|
|
|
client = HTTPClient(url='http://127.0.0.1') |
|
36
|
|
|
mock_result = MockResult() |
|
37
|
|
|
|
|
38
|
|
|
# Unknown content type, body should be returned raw |
|
39
|
|
|
mock_result.text = 'foo bar ponies' |
|
40
|
|
|
mock_result.headers = {'Content-Type': 'text/html'} |
|
41
|
|
|
mock_result.status_code = 200 |
|
42
|
|
|
|
|
43
|
|
|
mock_requests.request.return_value = mock_result |
|
44
|
|
|
result = client.run() |
|
45
|
|
|
|
|
46
|
|
|
self.assertEqual(result['body'], mock_result.text) |
|
47
|
|
|
self.assertEqual(result['status_code'], mock_result.status_code) |
|
48
|
|
|
self.assertEqual(result['headers'], mock_result.headers) |
|
49
|
|
|
|
|
50
|
|
|
# Unknown content type, JSON body |
|
51
|
|
|
mock_result.text = '{"test1": "val1"}' |
|
52
|
|
|
mock_result.headers = {'Content-Type': 'text/html'} |
|
53
|
|
|
|
|
54
|
|
|
mock_requests.request.return_value = mock_result |
|
55
|
|
|
result = client.run() |
|
56
|
|
|
|
|
57
|
|
|
self.assertEqual(result['body'], mock_result.text) |
|
58
|
|
|
|
|
59
|
|
|
# JSON content-type and JSON body |
|
60
|
|
|
mock_result.text = '{"test1": "val1"}' |
|
61
|
|
|
mock_result.headers = {'Content-Type': 'application/json'} |
|
62
|
|
|
|
|
63
|
|
|
mock_requests.request.return_value = mock_result |
|
64
|
|
|
result = client.run() |
|
65
|
|
|
|
|
66
|
|
|
self.assertTrue(isinstance(result['body'], dict)) |
|
67
|
|
|
self.assertEqual(result['body'], {'test1': 'val1'}) |
|
68
|
|
|
|
|
69
|
|
|
# JSON content-type with charset and JSON body |
|
70
|
|
|
mock_result.text = '{"test1": "val1"}' |
|
71
|
|
|
mock_result.headers = {'Content-Type': 'application/json; charset=UTF-8'} |
|
72
|
|
|
|
|
73
|
|
|
mock_requests.request.return_value = mock_result |
|
74
|
|
|
result = client.run() |
|
75
|
|
|
|
|
76
|
|
|
self.assertTrue(isinstance(result['body'], dict)) |
|
77
|
|
|
self.assertEqual(result['body'], {'test1': 'val1'}) |
|
78
|
|
|
|
|
79
|
|
|
# JSON content-type and invalid json body |
|
80
|
|
|
mock_result.text = 'not json' |
|
81
|
|
|
mock_result.headers = {'Content-Type': 'application/json'} |
|
82
|
|
|
|
|
83
|
|
|
mock_requests.request.return_value = mock_result |
|
84
|
|
|
result = client.run() |
|
85
|
|
|
|
|
86
|
|
|
self.assertFalse(isinstance(result['body'], dict)) |
|
87
|
|
|
self.assertEqual(result['body'], mock_result.text) |
|
88
|
|
|
|
|
89
|
|
|
@mock.patch('http_runner.requests') |
|
90
|
|
|
def test_https_verify(self, mock_requests): |
|
91
|
|
|
url = 'https://127.0.0.1:8888' |
|
92
|
|
|
client = HTTPClient(url=url, verify=True) |
|
93
|
|
|
mock_result = MockResult() |
|
94
|
|
|
|
|
95
|
|
|
mock_result.text = 'foo bar ponies' |
|
96
|
|
|
mock_result.headers = {'Content-Type': 'text/html'} |
|
97
|
|
|
mock_result.status_code = 200 |
|
98
|
|
|
|
|
99
|
|
|
mock_requests.request.return_value = mock_result |
|
100
|
|
|
client.run() |
|
101
|
|
|
|
|
102
|
|
|
self.assertTrue(client.verify) |
|
103
|
|
|
|
|
104
|
|
|
mock_requests.request.assert_called_with( |
|
105
|
|
|
'GET', url, allow_redirects=False, auth=None, cookies=None, |
|
106
|
|
|
data='', files=None, headers={}, params=None, proxies=None, |
|
107
|
|
|
timeout=60, verify=True) |
|
108
|
|
|
|
|
109
|
|
|
@mock.patch('http_runner.requests') |
|
110
|
|
|
def test_https_verify_false(self, mock_requests): |
|
111
|
|
|
url = 'https://127.0.0.1:8888' |
|
112
|
|
|
client = HTTPClient(url=url) |
|
113
|
|
|
mock_result = MockResult() |
|
114
|
|
|
|
|
115
|
|
|
mock_result.text = 'foo bar ponies' |
|
116
|
|
|
mock_result.headers = {'Content-Type': 'text/html'} |
|
117
|
|
|
mock_result.status_code = 200 |
|
118
|
|
|
|
|
119
|
|
|
mock_requests.request.return_value = mock_result |
|
120
|
|
|
client.run() |
|
121
|
|
|
|
|
122
|
|
|
self.assertFalse(client.verify) |
|
123
|
|
|
|
|
124
|
|
|
mock_requests.request.assert_called_with( |
|
125
|
|
|
'GET', url, allow_redirects=False, auth=None, cookies=None, |
|
126
|
|
|
data='', files=None, headers={}, params=None, proxies=None, |
|
127
|
|
|
timeout=60, verify=False) |
|
128
|
|
|
|
|
129
|
|
|
@mock.patch('http_runner.requests') |
|
130
|
|
|
def test_https_auth_basic(self, mock_requests): |
|
131
|
|
|
url = 'https://127.0.0.1:8888' |
|
132
|
|
|
username = 'misspiggy' |
|
133
|
|
|
password = 'kermit' |
|
134
|
|
|
client = HTTPClient(url=url, username=username, password=password) |
|
135
|
|
|
mock_result = MockResult() |
|
136
|
|
|
|
|
137
|
|
|
mock_result.text = 'muppet show' |
|
138
|
|
|
mock_result.headers = {'Authorization': 'bWlzc3BpZ2d5Omtlcm1pdA=='} |
|
139
|
|
|
mock_result.status_code = 200 |
|
140
|
|
|
|
|
141
|
|
|
mock_requests.request.return_value = mock_result |
|
142
|
|
|
result = client.run() |
|
143
|
|
|
|
|
144
|
|
|
self.assertEqual(result['headers'], mock_result.headers) |
|
145
|
|
|
|
|
146
|
|
|
mock_requests.request.assert_called_once_with( |
|
147
|
|
|
'GET', url, allow_redirects=False, auth=client.auth, cookies=None, |
|
148
|
|
|
data='', files=None, headers={}, params=None, proxies=None, |
|
149
|
|
|
timeout=60, verify=False) |
|
150
|
|
|
|