#!/usr/bin/env python # -*- encoding: utf-8 -*- """ @Version: 1.0 @Python Version:3.6.6 @Author: wenhx8 @File: test_httpTrans @Time: 2019/1/8 008 9:35 @Description: """ from unittest import TestCase from cucc_common_pkg.globalutility import Utility, GlobalConst from cucc_common_pkg.httptrans import HttpTrans from cucc_common_pkg.globalconst import HttpConst class TestHttpTrans(TestCase): r''' TestHttpTrans ''' def test_generate_header(self): r''' Returns: ''' http_trans = HttpTrans(None) expect_header_dict = { HttpConst.HEADER_KEY_CONTENT_TYPE: HttpConst.CONTENT_TYPE_URLENCODED} gen_header_dict = http_trans.generate_header( content_type=None, dict_header=None ) self.assertEqual(gen_header_dict, expect_header_dict) header = {'x-request-id': 'xx', 'x-b3-traceid': 'yy'} arg_extra_dict_header = {} arg_extra_dict_header['token'] = '123456' http_trans = HttpTrans(header) gen_header_dict = http_trans.generate_header( content_type=None, dict_header=arg_extra_dict_header ) expect_header_dict = dict(header.items(), **arg_extra_dict_header) expect_header_dict.update({ HttpConst.HEADER_KEY_CONTENT_TYPE: HttpConst.CONTENT_TYPE_URLENCODED }) self.assertEqual(gen_header_dict, expect_header_dict) def test_post_encodedata(self): r''' Returns: ''' http_trans = HttpTrans(None) # url = 'http://10.0.209.147/auth/relogin/logincheck' # url = 'http://www.baidu.com' url = 'http://10.124.151.110:31380/csm-microsrv/webutility/getjsonpath/get' list = [] dict__data = { "jsonPath": "metadata.name" , "value": 123} list.append(dict__data) dict_trans_data = { 'transjsonarray': list } ret_str = http_trans.post_encodedata( urlstr=url, trans_data=dict_trans_data, dict_headers=None ) ret_dict = Utility.jsonstr2dict(ret_str) self.assertTrue(GlobalConst.RETKEY_RET_CODE in ret_dict.keys()) self.assertTrue(GlobalConst.RETKEY_RET_VAL in ret_dict.keys()) def test_post_encodeddata_standard(self): r''' Returns: ''' http_trans = HttpTrans(None) # url = 'http://10.0.209.147/auth/relogin/logincheck' url = 'http://www.baidu.com' dict_trans_data = None resp = http_trans.post_encodeddata_standard(url, trans_data=dict_trans_data) self.assertTrue(resp.ok)