Interface testing-method for processing encrypted return data

Question: If the data returned by the interface is encrypted or the format is changed, how to deal with it

Carry out a layer of packaging, display conversion

  • The data obtained will further determine the data format and be converted according to the conditions. As in the following demo
class TestEncode:
   def test_api(self):
       req = ApiRequest()
       req_data = {
           "schema": "http",
           "encoding": "base64",  # 这个字段根据数据格式而定
           "method": "get",
           "url": "http://1.1.1.1/topics.txt",
           "headers": None
       }
       r = req.send(req_data)  # 将数据发送到已封装的函数,返回一个json格式的数据
       assert len(r["topics"]) == 2


class ApiRequest:
   """对数据进行相应的封装,进行格式转换"""
   def send(self, data: dict):
       if data["schema"] == "http":
           res = requests.request(data["method"], data["url"], headers=data["headers"])
           if data["encoding"] == "base64":  # 判断数据的编码格式
               # 使用base64进行解密,将解密后的数据转换成json结构体的数据
               return json.loads(base64.b64decode(res.content))
           else:
               return json.loads(res.content)  # 若数据格式不是base64,将原生内容进行json格式化
       elif data["schema"] == "dubbo":
           pass
       elif data["schema"] == "websocket":
           pass
       else:
           pass
  • Modify requests to add hooks to implicitly convert data. Modify the method of the underlying library to convert the data
  • If it is encrypted with base64, you can use the base64.b64decode (keyword) function to decrypt it, and then convert it to json format or other formats.
  • If you do n’t know the encryption algorithm, you can discuss with the developer, given an interface, request data from the interface, and return the required data in the corresponding format
  • Encryption and decryption should not be reflected in test cases

Encryption method

  • Request encryption: Digest encryption encrypts the original text or some fields in the original request as the digest algorithm, and sends the encrypted result to the original request.
  • Response encryption: The whole response returned or some fields in the response are encrypted, we need to find a decryption method, and there is a digest algorithm called the digest.

Decryption method

  • Solve the general decryption algorithm yourself
  • Need to develop lib to provide encryption and decryption
  • The encryption party is required to provide remote resolution services, so that the algorithm is still confidential.

The difference between encryption and signature

  • Encryption usually represents symmetric encryption, which can be decrypted. For example, base64 is mainly used for data transmission.
  • The signature usually represents asymmetric encryption, which is irreversible and not decryptable. For example, rsa, md5. Usually used to verify that the content has not been tampered with.

Guess you like

Origin www.cnblogs.com/chenri/p/12683458.html