Today, while watching TV series, more text, steadfast. This article mainly shares the problems and solutions encountered in writing the interface automation framework last week. One person to explore, level and food, a day is only enough to solve one problem.

How time flies, the weekend two days passed, happy house.

Yesterday, I re-opened “The Art of Love”, and suddenly I felt particularly sensible when I read the following.

Job programming

Everyone performs the tasks assigned by the organization at a prescribed speed and in a prescribed manner. Even the emotions are prescribed: cheerful, generous, faithful, ambitious, and friendly with everyone.

Entertainment formulation

Entertain in the same way as the custom, but not too much; Books are selected by index, films by cinema and theatre owners, who pay for advertising; The same goes for other activities: Sunday drives, TV time, card games, social gatherings. From birth to death, from this Monday to the next Monday, from morning to night, everything was orderly and predetermined.

Still, everyone seems to be doing well, and that’s good. – small buried

Today I will watch drama + nap + more text. I was woken up in the morning by takeout.Naps because the WIFI suddenly doesn’t work, and you sleep it off.

Yesterday, I watched a Taiwanese drama that was rated 9 on Douban, and I cried several times with the news of Grandpa Yuan. So today, I will choose a relaxing urban drama to watch, which can be distracted and won’t shed tears.

To be honest, the biggest accomplishment of the past week, aside from the task of testing the May milestone, has to do with putting in place the infrastructure for an interface automation framework on the Web side of withholding. The interface automation of the withholding end mentioned in the previous article is divided into “client end” and “Web end”. Because there is too big difference between the login of the two, I wrote it separately. The writing of the client end is still fast, and the login of the Web end is nested on other platforms, so I took the login of the existing platform for transformation. Let’s start today’s sharing.

Transformation point

The transformation point is the Cookie in headers.

Original platform login Cookie


I want to modify the platform login Cookie


There are two fields in the login Cookie to be modified.

Transformation ideas

Now it is clear that the Cookie of the existing platform needs to be filled with one X-authorization, while the Cookie of the platform to be transformed needs to be filled with two fields, X-authorization and X-Etax-KJYWR.

How to get x-authorization basically can reuse the original, but how to get X-Etax-KJyWr needs to find out from the developer. At the beginning, the developer provided me with the idea of requesting two interfaces to get these two values respectively. But after writing it, I found it very messy, and I was thinking about finding a better way to discuss with developers. The final result of our discussion is that the developer will change the code for me and let me request an interface to directly return these two fields to me.

Interface:

http://IP: port number /web/kjweb/token? secret=11111111&qysecret=${qy_secret}&zrrdah=${zrrdah}&zhxh=${zhxh}&device=web&dwdjxh=${dwdjxh}&zgswjgDm=&{zgswjgDm}

Copy the code

Response message:

{

    "code""SUCCESS".

    "params"null.

    "message"null.

    "data": {

        "qytoken""XXXXXXXeSxFfmIsJHXLoW/wTJeHLbk62aTTsWbXRXDrWtRsHbP36+fH7ya".

        "zrrtoken""XXXXXXXdn1VrzslpU3weQzYQ7Ag5IrhQ0jsGUCLiHoZIByChQveUEkRFpD_fj_LHY1Zg"

    },

    "appCodeForEx"null.

    "originalErrorCode"null.

    "rid"null

}

Copy the code

X-authorization and X-Etax-KJyWR in Cookie correspond to ZrrToken and QyToken respectively. As long as these two data are obtained, login can be completed.

Modified content

Original platform login method

Here the original login method is also written out mainly want to have a comparison with the transformation of the login method, although there is a field, but the value method is completely different.

protected ApiResponse requestHlw(String api, HttpMethod method, Object params, String yhm,String device) {



        device=device.toLowerCase();

        / / 1. Get auth

        HttpHeaders headers = getAuthorizationToken(yhm, device);

        if (headers == null) {

            return null;

        }



        //2. Make the actual request

        String url = hlwWebUri + api;



        return RequestUtil.sendRequest(url, method, params, headers, ApiResponse.class);

    }



Copy the code

Cookie is obtained by getAuthorizationToken.

private HttpHeaders getAuthorizationToken(String yhm, String device) {

        HttpHeaders headers = new HttpHeaders();



        String zrrdah=XmlParserUtil.getZhxx(yhm,env).getZrrdah();

        String zhxh=XmlParserUtil.getZhxx(yhm,env).getZhxh();



        Map<String, String> params = new HashMap<>();

        params.put("zrrdah", zrrdah);

        params.put("zhxh", zhxh);

        params.put("device", device);

        params.put("secret", webSecret);



        ResponseEntity<String> authResp = RequestUtil.sendRequest(authorizationUri, HttpMethod.GET, params, null);



        String authorizationToken = fetchToken(authResp);

        logger.info("Obtain token:{} according to natural person file number :{}", zrrdah, authorizationToken);



        if (authorizationToken == null) {

            return null;

        }



        / / 3. Return to the header

        headers.set("Authorization", authorizationToken);



        return headers;

    }





    // Retrieve token

    private String fetchToken(ResponseEntity<String> authResp) {

        String bodyStr = authResp.getBody().toString();

        JSONObject bodyJson = JSONObject.parseObject(bodyStr);



        String isSuccess = bodyJson.getString("code");

        if (StringUtils.equals(isSuccess, "SUCCESS")) {

            String accessToken = bodyJson.getString("data");



            logger.info("accessToken: " + accessToken);

            return accessToken;

        } else {

            return null;

        }

    }



Copy the code

Login method to be modified

The login method to be modified is obtained from the data of the response packet.

Write a SEPARATE DTO to obtain the Web terminal login token for withholding

package com.XXXXX.dto;

import com.alibaba.fastjson.JSON;

import lombok.Data;

import java.io.Serializable;

/ * *

 * create by lxl01 on 2021/5/20

* /


@Data

public class KjwebTokenDTO<dataimplements Serializable {



    private Object data;



    public Object getData(a) {

        return data;

    }



    public void setData(Object data) {

        this.data = data;

    }



    public <T> getDataBean(Class<T> clazz) return JSON.parseObject(data.toString(), clazz); }

}

Copy the code

You can then write the request interface in the HttpWebRequest class.

 protected ApiWebResponse requestKjd(String api, HttpMethod method, Object params, String yhm,String device) {



        device=device.toLowerCase();

        / / 1. Get auth

        HttpHeaders headers = getToken(yhm, device);

        if (headers == null) {

            return null;

        }



        //2. Make the actual request

        String url = kjdWebUri + api;



        return RequestUtil.sendRequest(url, method, params, headers, ApiWebResponse.class);

    }

Copy the code

Cookie is obtained by getToken, which can take out x-authorization and X-Etax-KJyWR in Cookie.

private HttpHeaders getToken(String yhm, String device) {



        HttpHeaders headers = new HttpHeaders();



        String zrrdah = XmlParserUtil.getZhxx(yhm, env).getZrrdah();

        String zhxh = XmlParserUtil.getZhxx(yhm, env).getZhxh();



        Map<String, String> params = new HashMap<>();

        params.put("zrrdah", zrrdah);

        params.put("zhxh", zhxh);

        params.put("device", device);

        params.put("secret", Secret);

        params.put("qysecret", qySecret);

        params.put("dwdjxh", dwdjxh);

        params.put("zgswjgDm", zgswjgDm);

        System.out.println("params is:" + params.toString());

        System.out.println("authorizationUri is:" + authorizationUri);

        ApiWebResponse apiWebResponse = RequestUtil.sendRequest(authorizationUri, HttpMethod.GET, params, null, ApiWebResponse.class);

        

        String data= JSON.toJSONString(apiWebResponse.getData());

        JSONObject jsonObject = JSON.parseObject(data);

        String qyToken=jsonObject.getString("qytoken");

        String zrrToken =jsonObject.getString("zrrtoken");





        String cookie = "X-ETAX-KJYWR=" + qyToken+ "; X-Authorization=" + zrrToken;

        System.out.println(cookie);

        / / 3. Return to the header

        headers.set("cookie", cookie);

        return headers;

    }

Copy the code

What is different from the original platform login is that the Cookie is obtained from

 String authorizationToken = fetchToken(authResp);

/ / 3. Return to the header

        headers.set("Authorization", authorizationToken);

Copy the code

to

String cookie = "X-ETAX-KJYWR=" + qyToken+ "; X-Authorization=" + zrrToken;

/ / 3. Return to the header

      headers.set("cookie", cookie);

Copy the code

Problems encountered

Class conversion exception

Because my old DTO was wrapped like this

In fact, I got zrrToken and qyToken in data. Data is an Object type and should be written as



Well, that’s it. See you next time


This issue of small buried quotations

1. Grocery stores are where children’s friendships begin. What about adult friendships?

2. Most of the time, it’s hard to tell whether fate decides our direction or we decide our fate.

3. Life will find its way.

4. My problems are these, other people’s problems are those, everyone has problems. Just like God closes this window for you, opens another window, who knows what window is closed, what window is opened, there is no choice, don’t be too entangled.