/**
 * Copyright (c) 2015-2021, Michael Yang 杨福海 (fuhai999@gmail.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.jboot.test;

import io.jboot.test.web.MockHttpServletRequest;
import io.jboot.test.web.MockHttpServletResponse;
import io.jboot.test.web.MockServletInputStream;
import io.jboot.utils.StrUtil;

import java.util.HashMap;
import java.util.Map;

public class MockMvc {

    public MockMvcResult get(String target) {
        return get(target, null);
    }


    public MockMvcResult get(String target, Map<String, Object> paras) {
        return get(target, paras, null);
    }


    public MockMvcResult get(String target, Map<String, Object> paras, Map<String, String> headers) {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setMethod("GET");

        int indexOf = target.lastIndexOf("?");
        if (indexOf != -1){
            Map<String, String> targetParas = StrUtil.queryStringToMap(target.substring(indexOf + 1));
            if (paras == null){
                paras = new HashMap<>();
            }
            paras.putAll(targetParas);
            target = target.substring(0,indexOf);
        }

        request.setServletPath(target);

        if (headers != null) {
            request.setHeaders(headers);
        }

        if (paras != null) {
            paras.forEach(request::addParameter);
            request.setQueryString(StrUtil.mapToQueryString(paras));
        }

        MockHttpServletResponse response = new MockHttpServletResponse();
        sendRequest(request, response);
        return new MockMvcResult(response);
    }

    public MockMvcResult post(String target) {
        return post(target, null, null, null);
    }

    public MockMvcResult post(String target, String postData) {
        return post(target, null, null, postData);
    }


    public MockMvcResult post(String target, Map<String, Object> paras) {
        return post(target, paras, null, null);
    }

    public MockMvcResult post(String target, Map<String, Object> paras, String postData) {
        return post(target, paras, null, postData);
    }

    public MockMvcResult post(String target, Map<String, Object> paras, Map<String, String> headers, String postData) {
        MockHttpServletRequest request = new MockHttpServletRequest();
        request.setMethod("POST");

        int indexOf = target.lastIndexOf("?");
        if (indexOf != -1){
            Map<String, String> targetParas = StrUtil.queryStringToMap(target.substring(indexOf + 1));
            if (paras == null){
                paras = new HashMap<>();
            }
            paras.putAll(targetParas);
            target = target.substring(0,indexOf);
        }

        request.setServletPath(target);

        if (headers != null) {
            request.setHeaders(headers);
        }

        if (paras != null) {
            paras.forEach(request::addParameter);
        }

        if (StrUtil.isNotBlank(postData)) {
            request.setInputStream(new MockServletInputStream(postData));
        }

        MockHttpServletResponse response = new MockHttpServletResponse();
        sendRequest(request, response);
        return new MockMvcResult(response);
    }

    public void sendRequest(MockHttpServletRequest request, MockHttpServletResponse response) {
        MockApp.mockRequest(request, response);
    }

}

@@ -890,31 +890,521 @@
 ap;%0A
-%0Apublic class MockMvc %7B
+import java.util.function.Function;%0A%0Apublic class MockMvc %7B%0A%0A    private static Map%3CString, String%3E globalParas = new HashMap%3C%3E();%0A    private static Map%3CString, Function%3CMockHttpServletRequest, String%3E%3E globalParaFunctions = new HashMap%3C%3E();%0A%0A    public void addGlobalParas(String key, String value) %7B%0A        globalParas.put(key, value);%0A    %7D%0A%0A    public void addGlobalParaFunction(String key, Function%3CMockHttpServletRequest, String%3E valueFunction) %7B%0A        globalParaFunctions.put(key, valueFunction);%0A    %7D
 %0A%0A  
@@ -1884,32 +1884,33 @@
  (indexOf != -1)
+ 
 %7B%0A            Ma
@@ -2020,32 +2020,33 @@
  (paras == null)
+ 
 %7B%0A              
@@ -2157,32 +2157,33 @@
 get.substring(0,
+ 
 indexOf);%0A      
@@ -2462,32 +2462,286 @@
 s));%0A        %7D%0A%0A
+        if (!globalParas.isEmpty())%7B%0A            globalParas.forEach(request::addParameter);%0A        %7D%0A%0A        if (!globalParaFunctions.isEmpty())%7B%0A            globalParaFunctions.forEach((s, f) -%3E request.addParameter(s,f.apply(request)));%0A        %7D%0A%0A%0A
         MockHttp
@@ -3692,16 +3692,17 @@
 f != -1)
+ 
 %7B%0A      
@@ -3828,16 +3828,17 @@
 == null)
+ 
 %7B%0A      
@@ -3965,16 +3965,17 @@
 tring(0,
+ 
 indexOf)
@@ -4193,32 +4193,285 @@
 er);%0A        %7D%0A%0A
+        if (!globalParas.isEmpty())%7B%0A            globalParas.forEach(request::addParameter);%0A        %7D%0A%0A        if (!globalParaFunctions.isEmpty())%7B%0A            globalParaFunctions.forEach((s, f) -%3E request.addParameter(s,f.apply(request)));%0A        %7D%0A%0A
         if (StrU

add global paras for MockMvc