def test_create_json_response_stack_calls(self, mock_stack):
        mock_stack.return_value = 'foo'
        obj = Mock(status_int=401, location='http://example.com/api')
        jsonex.create_json_response(obj, None, encoder=_JSONEncoder)
        assert mock_stack.call_count == 0

        obj = Mock(status_int=500, location='http://example.com/api')
        jsonex.create_json_response(obj, None, encoder=_JSONEncoder)
        mock_stack.assert_called_with()
        assert mock_stack.call_count == 1

        obj = Mock(status_int=401, location='http://example.com/api')
        jsonex.create_json_response(obj,
                                    None,
                                    encoder=_JSONEncoder,
                                    show_stack=True)
        mock_stack.assert_called_with()
        assert mock_stack.call_count == 2

        obj = Mock(status_int=401, location='http://example.com/api')
        jsonex.create_json_response(obj,
                                    None,
                                    encoder=_JSONEncoder,
                                    log_it=True)
        mock_stack.assert_called_with()
        assert mock_stack.call_count == 3
 def test_create_json_response(self):
     request = Mock(
         url='http://example.com',
         client_addr='127.0.0.1',
         remote_addr='127.0.0.2')
     obj = Mock(
         status_int=401,
         location='http://example.com/api')
     obj2 = jsonex.create_json_response(
         obj, request, encoder=_JSONEncoder,
         status_code=402, explanation='success',
         message='foo', title='bar')
     assert obj2.content_type == 'application/json'
     assert isinstance(obj2.body, basestring)
     body = json.loads(obj2.body)
     assert body.keys() == [
         'remote_addr', 'status_code', 'explanation', 'title',
         'message', 'id', 'timestamp', 'request_url', 'client_addr'
     ]
     assert body['remote_addr'] == '127.0.0.2'
     assert body['client_addr'] == '127.0.0.1'
     assert body['status_code'] == 402
     assert body['explanation'] == 'success'
     assert body['title'] == 'bar'
     assert body['message'] == 'foo'
     assert body['id'] == 'api'
     assert body['request_url'] == 'http://example.com'
 def test_create_json_response(self):
     request = Mock(url='http://example.com',
                    client_addr='127.0.0.1',
                    remote_addr='127.0.0.2')
     obj = Mock(status_int=401, location='http://example.com/api')
     obj2 = jsonex.create_json_response(obj,
                                        request,
                                        encoder=_JSONEncoder,
                                        status_code=402,
                                        explanation='success',
                                        message='foo',
                                        title='bar')
     assert obj2.content_type == 'application/json'
     assert isinstance(obj2.body, six.binary_type)
     body = json.loads(obj2.body.decode('utf-8'))
     assert sorted(body.keys()) == [
         '_pk', 'client_addr', 'explanation', 'message', 'remote_addr',
         'request_url', 'status_code', 'timestamp', 'title'
     ]
     assert body['remote_addr'] == '127.0.0.2'
     assert body['client_addr'] == '127.0.0.1'
     assert body['status_code'] == 402
     assert body['explanation'] == 'success'
     assert body['title'] == 'bar'
     assert body['message'] == 'foo'
     assert body['_pk'] == 'api'
     assert body['request_url'] == 'http://example.com'
 def test_create_json_response_with_body(self):
     obj = Mock(
         status_int=401,
         location='http://example.com/api')
     obj2 = jsonex.create_json_response(
         obj, None, encoder=_JSONEncoder,
         status_code=402, explanation='success',
         message='foo', title='bar', body={'zoo': 'zoo'})
     assert obj2.content_type == 'application/json'
     assert isinstance(obj2.body, six.binary_type)
     body = json.loads(obj2.body.decode('utf-8'))
     assert body == {'zoo': 'zoo'}
 def test_create_json_response_with_body(self):
     obj = Mock(
         status_int=401,
         location='http://example.com/api')
     obj2 = jsonex.create_json_response(
         obj, None, encoder=_JSONEncoder,
         status_code=402, explanation='success',
         message='foo', title='bar', body={'zoo': 'zoo'})
     assert obj2.content_type == 'application/json'
     assert isinstance(obj2.body, six.binary_type)
     body = json.loads(obj2.body.decode('utf-8'))
     assert body == {'zoo': 'zoo'}
 def test_create_json_response_obj_properties(self, mock_stack):
     mock_stack.return_value = 'foo'
     obj = Mock(
         status_int=401,
         location='http://example.com/api',
         status_code=402, explanation='success',
         message='foo', title='bar')
     obj2 = jsonex.create_json_response(
         obj, None, encoder=_JSONEncoder)
     body = json.loads(obj2.body)
     assert body['status_code'] == 402
     assert body['explanation'] == 'success'
     assert body['title'] == 'bar'
     assert body['message'] == 'foo'
     assert body['id'] == 'api'
 def test_create_json_response_obj_properties(self, mock_stack):
     mock_stack.return_value = 'foo'
     obj = Mock(status_int=401,
                location='http://example.com/api',
                status_code=402,
                explanation='success',
                message='foo',
                title='bar')
     obj2 = jsonex.create_json_response(obj, None, encoder=_JSONEncoder)
     body = json.loads(obj2.body.decode('utf-8'))
     assert body['status_code'] == 402
     assert body['explanation'] == 'success'
     assert body['title'] == 'bar'
     assert body['message'] == 'foo'
     assert body['_pk'] == 'api'
    def test_create_json_response_stack_calls(self, mock_stack):
        obj = Mock(status_int=401, location='http://example.com/api')
        jsonex.create_json_response(obj, None, encoder=_JSONEncoder)
        assert mock_stack.call_count == 0

        obj = Mock(status_int=500, location='http://example.com/api')
        jsonex.create_json_response(obj, None, encoder=_JSONEncoder)
        mock_stack.assert_called()
        assert mock_stack.call_count == 1

        obj = Mock(status_int=401, location='http://example.com/api')
        jsonex.create_json_response(
            obj, None, encoder=_JSONEncoder, show_stack=True)
        mock_stack.assert_called()
        assert mock_stack.call_count == 2

        obj = Mock(status_int=401, location='http://example.com/api')
        jsonex.create_json_response(
            obj, None, encoder=_JSONEncoder, log_it=True)
        mock_stack.assert_called()
        assert mock_stack.call_count == 3