package helper;

import com.yanzuoguang.util.helper.BaiduHelper;
import com.yanzuoguang.util.helper.HttpHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import helper.vo.BaiduDate;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpHelperTest {

    @Test
    public void test() {
        long time = System.currentTimeMillis();

        Map<String, Object> map = new HashMap<>();
        map.put("query", "2021年10月");
        map.put("co", "");
        map.put("resource_id", "39043");
        map.put("t", time);
        map.put("ie", "utf8");
        map.put("oe", "gbk");
        map.put("cb", "op_aladdin_callback");
        map.put("format", "json");
        map.put("tn", "wisetpl");
        map.put("cb", "jQuery1102007795278844876075_" + (time - 30));
        map.put("_", time - 50);

        String para = HttpHelper.getUrlParameter(map);

        String url = "https://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?";
        String fullUrl = url + para;

        String post = HttpHelper.get(fullUrl, "gbk");
        System.out.println(post);

        Pattern compile = Pattern.compile(".*\\((.*)\\).*");
        Matcher matcher = compile.matcher(post);
        if (matcher.find()) {
            String group = matcher.group(1);
            System.out.println(group);
            BaiduDate result = JsonHelper.deserialize(group, BaiduDate.class);
            System.out.println(result.getStatus());
        }
    }

    @Test
    public void testBaidu() {
        BaiduHelper.BaiduDate result = BaiduHelper.getBaiduData(new Date());
        System.out.println(result.getStatus());
    }

    @Test
    public void testRequest() {
        String url = "http://cd2da444e859.ngrok.io/reserveInfo/addSgReserveInfo.do";
        String post = "{\"appointmentDate\":\"2021-07-27\",\"arriveAddress\":\"40288ae86ccd4bc8016ccd4c84cf0001\",\"arriveTime\":\"0\",\"cardType\":\"二代身份证\",\"openid\":\"TBD\",\"visitorCardId\":\"43052419871104817X\",\"visitorName\":\"燕子\",\"visitorPhone\":\"13800138000\"}";
        String result = HttpHelper.postApplicationJSON(url, post);
        System.out.println(result);
    }
}