test_httptrans.py 2.72 KB
Newer Older
qunfeng qiu's avatar
qunfeng qiu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#!/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)