Conditions | 1 |
Total Lines | 55 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # Licensed to the StackStorm, Inc ('StackStorm') under one or more |
||
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 | |||
150 |