Python interface test handles parameter and encoding issues of escape characters!

Introduction

The author, after being very busy at work, decided to take time to record the problems encountered when testing the interface for future reference. It can also be provided to those partners who are learning interface testing to avoid detours. If it helps you, give it a like, thank you.

Today’s article mainly talks about the processing of request parameters containing escape characters and return parameters containing escape characters in interface testing.

1. First, take a look at the data style of the parameters in my interface:

There are two parameters in this body: “body” and “method”. The entire data variable is a dictionary, but “body” is a string and contains escape characters. The entire data variable is an escape string. There is no problem in writing this kind of parameter in the code, but when used as an interface request, sometimes it cannot be parsed by json, which ultimately leads to the failure of the interface request.

This parameter is not processed and is requested directly, as shown in the figure below:

2. The target has been determined, and the next step is to process it. I first de-escape the data. It is actually very simple. I use a json method loads(), as shown in the figure:

When using this method, you need to process the data in advance because the types supported by this method are limited, such as:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

twenty one

twenty two

twenty three

twenty four

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None,

parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):

"""Deserialize ``s`` (a ``str``, ``bytes`` or ``bytearray`` instance

containing a JSON document) to a Python object.

``object_hook`` is an optional function that will be called with the

result of any object literal decode (a ``dict``). The return code> value of

``object_hook`` will be used instead of the ``dict``. This feature

can be used to implement custom decoders (e.g. JSON-RPC class hinting).

``object_pairs_hook`` is an optional function that will be called with the

result of any object literal decoded with an ordered list of pairs. The

return value of ``object_pairs_hook`` will be used instead of the ``dict``.

This feature can be used to implement custom decoders that rely on the

order that the key and value pairs are decoded (for example,

collections.OrderedDict will remember the order of insertion). If

``object_hook`` is also defined, the ``object_pairs_hook`` takes priority.

``parse_float``, if specified, will be called with the string

of every JSON float to be decoded. By default this is equivalent to

float(num_str). This can be used to use another datatype or parser

for JSON floats (e.g. decimal.Decimal).

``parse_int``, if specified, will be called with the string

of every JSON int to be decoded. By default this is equivalent to

int(num_str). This can be used to use another datatype or parser

for JSON integers (e.g. float).

``parse_constant``, if specified, will be called with one of the

following strings: -Infinity, Infinity, NaN.

This can be used to raise an exception if invalid JSON numbers

are encountered.

To use a custom ``JSONDecoder`` subclass, specify it with the ``cls``

kwarg; otherwise ``JSONDecoder`` is used.

The ``encoding`` argument is ignored and deprecated.

"""

I first convert data into str, as shown in the figure:

The reason why I posted a screenshot of the error report is that I have a knowledge point to teach you.

Python strings are hierarchical, such as using ”’ ”’ and ” ” and ‘ ‘, so you cannot use two ” ” like the picture above.

After modification, execute the calling interface program:

This return result is what I want.

Now that we’ve talked about the escaping of input parameters, what about the external parameters? Leave it to everyone to think.

Now I have also found a lot of test friends and created a communication group to share technology, sharing a lot of technical documents and video tutorials we collected.
If you don’t want to experience the feeling of not being able to find resources when studying on your own, having no one to answer your questions, and persisting for a few days before giving up.
You can join us to communicate. And there are many technical experts who have made certain achievements in automation, performance, security, test development, etc.
Share their experience, and also share many live lectures and technical salons
You can learn for free! Focus on it! Open source! ! !
QQ group number: 110685036

Encoding processing

Many times, the data returned includes Chinese and binary data. Let’s first look at the unprocessed data returned by the interface, which is shown as follows:

This kind of data is firstly inconvenient to view, and secondly it is difficult to find the value you want.

1

print(r2.content.decode(),end=' ')

Run the script:

1

UnicodeEncodeError: 'gbk' codec can't encode character '\xe2' in position 15788: illegal multibyte sequence

What this sentence says is that gbk cannot encode, but my code encoding is utf-8, which is obviously not a code problem. The error location at ‘\xe2’ cannot be decoded. Add the standard output code:

1

2

3

import io

import sys

sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') #Change the default encoding of standard output

Execute the program again and the result shows success:

It should be noted that if gb18030 does not work, use utf-8, such as:

1

sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') #Change the default encoding of standard output

It can also be changed to:

1

sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gbk')

Below are some corresponding encoding tables for Chinese:

Encoding name Purpose
utf8 All languages
gbk Simplified Chinese
gb2312 Simplified Chinese
gb18030 Simplified Chinese
big5 Traditional Chinese
big5hkscs Traditional Chinese

Reason explanation: For Unicode characters, if you need to print them out, since the local system is cmd in Windows, the default codepage is CP936, which is the encoding of GBK, so the python interpreter needs to encode the above Unicode characters into GBK first, and then in cmd displayed in . However, because the Unicode string contains some characters that cannot be displayed in GBK, the error “‘gbk’ codec can’t encode” is prompted at this time. In fact, the limitation of the print() function is the limitation of Python’s default encoding. Because the system is Windows, the default encoding of python is not ‘utf-8’. Just change the default encoding of python to ‘utf-8’.

Interface request method

As mentioned in the introduction, if you don’t know about requests, you can refer to my article Python interface to test the difference between data and json parameters in the requests.post method. This article talks about the two data types data and json for post requests. So how to use these two parameter types to request data with escape characters in the body data at the same time? Before speaking, let’s review some knowledge points:

1

2

3

4

5

6

7

resp.text returns Unicode data.

resp.content returns bytes type data.

resp.json() returns json format data

#Remarks:

#If you want to get text, you can pass r.text.

#If you want to get pictures and files, you can pass r.content.

#If you want dict type data, you can pass r.json().

Specific script:

The result is as follows:

Regardless of whether you choose the data type or the json type, as long as the types of parameters passed correspond to each other, there will be no problem. There is no script code directly posted here. It is also for beginners to type by themselves. They cannot be directly copied and used, which will hinder their own coding improvement.

Extract parameters from the message

How to get the values corresponding to the key in the returned result. For example, with this interface, I want to get the value of the warehouseName field, as shown in the figure:

From the data point of view, the returned data type is a dictionary, and the dictionary warehouseName I want to obtain is in the dictionary data. data is a dictionary. There is also a list in it. There is also a dictionary in the list, which is equivalent to 4 levels of nesting. How? What about taking out the median value of the 4th layer? This is a two-step operation, please see the code for details:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

# Get the value corresponding to the key in the dictionary

a = r.json()

b =a['data']['wimslist']

# print(type(dict(b)))

c = json.dumps(b, ensure_ascii=False)

# Method 1

for item in b:print (item['warehouseName' ],end=' ')

# Method 2

# Get the key value of the dictionary in the list

list_result = []

for i in b:

list_result.append(i['warehouseName'])

print(list_result)

from common.loggers import Log

loggger = Log()

The output results are as follows:

Appendix

1. Here are the knowledge points about (u, r, b in front of the string) to deepen your impression. If you haven’t understood it before, just learn it and you will naturally use it in the future.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

twenty one

twenty two

twenty three

1. Add u before the string

Example: u"I am a string consisting of Chinese characters."

Function:

The string following is encoded in Unicode format and is generally used in front of Chinese strings to prevent garbled characters when used again due to source code storage format issues.

2. Add r before the string

Example: r"\
\
\
\
" # represents an ordinary raw string \
\
\
\
, not a line break.

Function:

Remove backslash escaping mechanism.

(Special characters: those, backslash plus corresponding letters, indicating the corresponding special meaning, such as the most common "\
" means line break, "\t" means Tab, etc.)

Application:

is often used in regular expressions and corresponds to the re module.

3. Add b before the string

Example: response = b'

Hello World!

' # b' ' Indicates that this is a bytes object

Function:

b" "The prefix means: the following string is of bytes type.

Use:

In network programming, servers and browsers only recognize bytes type data.

For example: the parameters of the send function and the return value of the recv function are both of type bytes

Attachment:

In Python3, the conversion method between bytes and str is

str.encode('utf-8')

bytes.decode('utf-8')

2. Regarding the basic knowledge of python encoding, the conversion between string and byte stream is as follows:

Finally, I would like to thank everyone who has read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software testing interview document

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill tree Home page Overview 379,410 people are learning the system