Http.Post json格式文件,总是报错

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Http.Post json格式文件,总是报错

moon
import static net.grinder.script.Grinder.grinder
import static org.junit.Assert.*
import static org.hamcrest.Matchers.*
import net.grinder.plugin.http.HTTPRequest
import net.grinder.plugin.http.HTTPPluginControl
import net.grinder.script.GTest
import net.grinder.script.Grinder
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith

import java.util.Date
import java.util.List
import java.util.ArrayList

import HTTPClient.Cookie
import HTTPClient.CookieModule
import HTTPClient.HTTPResponse
import HTTPClient.NVPair

/**
 * A simple example using the HTTP plugin that shows the retrieval of a
 * single page via HTTP.
 *
 * This script is automatically generated by ngrinder.
 *
 * @author admin
 */
@RunWith(GrinderRunner)
class TestRunner {

        public static GTest test
        public static HTTPRequest request
        public static NVPair[] headers = []
        public static String body = "{\n  \"kind\": \"Service\", \n  \"apiVersion\": \"v1\", \n  \"metadata\": {\n    \"name\": \"omc-tomcat7\"\n  }, \n  \"spec\": {\n    \"selector\": {\n      \"role\": \"omc-tomcat7\"\n    }, \n    \"ports\": [\n      {\n        \"protocol\": \"TCP\"\n      }, \n      {\n        \"port\": \"8080\"\n      }, \n      {\n        \"targetPort\": \"8080\"\n      }\n    ]\n  }\n}"
        public static Cookie[] cookies = []

        @BeforeProcess
        public static void beforeProcess() {
                HTTPPluginControl.getConnectionDefaults().timeout = 6000
                test = new GTest(1, "172.18.1.181")
                request = new HTTPRequest()
                // Set header datas
                List<NVPair> headerList = new ArrayList<NVPair>()
                headerList.add(new NVPair("Content-Type", "application/json"))
                headers = headerList.toArray()
                grinder.logger.info("before process.");
        }

        @BeforeThread
        public void beforeThread() {
                test.record(this, "test")
                grinder.statistics.delayReports=true;
                grinder.logger.info("before thread.");
        }
       
        @Before
        public void before() {
                request.setHeaders(headers)
                cookies.each { CookieModule.addCookie(it, HTTPPluginControl.getThreadHTTPClientContext()) }
                grinder.logger.info("before thread. init headers and cookies");
        }

        @Test
        public void test(){
                HTTPResponse result = request.POST("http://172.18.1.181:9326/api/v1/namespaces/default/services", body.getBytes())

                //print logInfo
                grinder.logger.info(result.getText());
                grinder.logger.info(result.getHeader("Content-type"));

                if (result.statusCode == 301 || result.statusCode == 302) {
                        grinder.logger.warn("Warning. The response may not be correct. The response code was {}.", result.statusCode);
                } else {
                        assertThat(result.statusCode, is(200));
                }
        }
}



--------------------------------------------------------------
result as follow(error info):


2017-08-07 15:58:32,699 INFO  The Grinder version 3.9.1
2017-08-07 15:58:32,714 INFO  Java(TM) SE Runtime Environment 1.7.0_51-b13: Java HotSpot(TM) 64-Bit Server VM (24.51-b03, mixed mode) on Windows 7 amd64 6.1
2017-08-07 15:58:32,721 INFO  time zone is CST (+0800)
2017-08-07 15:58:32,980 INFO  worker process 0 of agent number 0
2017-08-07 15:58:33,032 INFO  Instrumentation agents: byte code transforming instrumenter for Java; byte code transforming instrumenter for Java
2017-08-07 15:58:33,602 ERROR Script error - Error while initialize test runner
net.grinder.engine.common.EngineException: Error while initialize test runner
        at net.grinder.scriptengine.groovy.GroovyScriptEngine.<init>(GroovyScriptEngine.java:71) ~[ngrinder-groovy-3.4.jar:na]
        at net.grinder.scriptengine.groovy.GroovyScriptEngineService.createScriptEngine(GroovyScriptEngineService.java:109) ~[ngrinder-groovy-3.4.jar:na]
        at net.grinder.engine.process.ScriptEngineContainer.getScriptEngine(ScriptEngineContainer.java:105) ~[grinder-core-3.9.1.jar:na]
        at net.grinder.engine.process.GrinderProcess.run(GrinderProcess.java:330) ~[ngrinder-runtime-3.4.jar:na]
        at net.grinder.engine.process.WorkerProcessEntryPoint.run(WorkerProcessEntryPoint.java:87) [grinder-core-3.9.1.jar:na]
        at net.grinder.engine.process.WorkerProcessEntryPoint.main(WorkerProcessEntryPoint.java:60) [grinder-core-3.9.1.jar:na]
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
${NGRINDER_HOME}\script\admin\groovy181_002.groovy: 40: unexpected token: & @ line 40, column 30.
    public static String body = "{\n  \"kind\": \"Service\", \n  \"apiVersion\": \"v1\", \n  \"metadata\": {\n    \"name\": \"omc-tomcat7\"\n  }, \n  \"spec\": {\n    \"selector\": {\n      \"role\": \"omc-tomcat7\"\n    }, \n    \"ports\": [\n      {\n        \"protocol\": \"TCP\"\n      }, \n      {\n        \"port\": \"8080\"\n      }, \n      {\n        \"targetPort\": \"8080\"\n      }\n    ]\n  }\n}"
                                ^

1 error

        at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:149) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:142) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:162) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:912) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:574) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:550) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:527) ~[groovy-all-2.2.1.jar:2.2.1]
        at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:279) ~[groovy-all-2.2.1.jar:2.2.1]
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258) ~[groovy-all-2.2.1.jar:2.2.1]
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:244) ~[groovy-all-2.2.1.jar:2.2.1]
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:185) ~[groovy-all-2.2.1.jar:2.2.1]
        at net.grinder.scriptengine.groovy.GroovyScriptEngine.<init>(GroovyScriptEngine.java:62) ~[ngrinder-groovy-3.4.jar:na]
        ... 5 common frames omitted


2017-08-07 15:58:33,607 ERROR worker-bootstrap: Error running worker process
net.grinder.engine.common.EngineException: Error while initialize test runner
        at net.grinder.scriptengine.groovy.GroovyScriptEngine.<init>(GroovyScriptEngine.java:71) ~[ngrinder-groovy-3.4.jar:na]
        at net.grinder.scriptengine.groovy.GroovyScriptEngineService.createScriptEngine(GroovyScriptEngineService.java:109) ~[ngrinder-groovy-3.4.jar:na]
        at net.grinder.engine.process.ScriptEngineContainer.getScriptEngine(ScriptEngineContainer.java:105) ~[grinder-core-3.9.1.jar:na]
        at net.grinder.engine.process.GrinderProcess.run(GrinderProcess.java:330) ~[ngrinder-runtime-3.4.jar:na]
        at net.grinder.engine.process.WorkerProcessEntryPoint.run(WorkerProcessEntryPoint.java:87) [grinder-core-3.9.1.jar:na]
        at net.grinder.engine.process.WorkerProcessEntryPoint.main(WorkerProcessEntryPoint.java:60) [grinder-core-3.9.1.jar:na]
Caused by: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
${NGRINDER_HOME}\script\admin\groovy181_002.groovy: 40: unexpected token: & @ line 40, column 30.
    public static String body = "{\n  \"kind\": \"Service\", \n  \"apiVersion\": \"v1\", \n  \"metadata\": {\n    \"name\": \"omc-tomcat7\"\n  }, \n  \"spec\": {\n    \"selector\": {\n      \"role\": \"omc-tomcat7\"\n    }, \n    \"ports\": [\n      {\n        \"protocol\": \"TCP\"\n      }, \n      {\n        \"port\": \"8080\"\n      }, \n      {\n        \"targetPort\": \"8080\"\n      }\n    ]\n  }\n}"
                                ^

1 error

        at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:309) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.ErrorCollector.addFatalError(ErrorCollector.java:149) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:119) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.ErrorCollector.addError(ErrorCollector.java:131) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.SourceUnit.addError(SourceUnit.java:359) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:142) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:108) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:162) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:912) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:574) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:550) ~[groovy-all-2.2.1.jar:2.2.1]
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:527) ~[groovy-all-2.2.1.jar:2.2.1]
        at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:279) ~[groovy-all-2.2.1.jar:2.2.1]
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258) ~[groovy-all-2.2.1.jar:2.2.1]
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:244) ~[groovy-all-2.2.1.jar:2.2.1]
        at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:185) ~[groovy-all-2.2.1.jar:2.2.1]
        at net.grinder.scriptengine.groovy.GroovyScriptEngine.<init>(GroovyScriptEngine.java:62) ~[ngrinder-groovy-3.4.jar:na]
Reply | Threaded
Open this post in threaded view
|

Re: Http.Post json格式文件,总是报错

moon
但是貌似并没有多余的&符号,那个是引号(")的第一个字符   如下

${NGRINDER_HOME}\script\admin\groovy181_002.groovy: 40: unexpected token: & @ line 40, column 30.
    public static String body = "{\n  \"kind\": \"Service\", \n  \"apiVersion\": \"v1\", \n  \"metadata\": {\n    \"name\": \"omc-tomcat7\"\n  }, \n  \"spec\": {\n    \"selector\": {\n      \"role\": \"omc-tomcat7\"\n    }, \n    \"ports\": [\n      {\n        \"protocol\": \"TCP\"\n      }, \n      {\n        \"port\": \"8080\"\n      }, \n      {\n        \"targetPort\": \"8080\"\n      }\n    ]\n  }\n}"
Reply | Threaded
Open this post in threaded view
|

Re: Http.Post json格式文件,总是报错

moon
引号  & quote ;
Reply | Threaded
Open this post in threaded view
|

Re: Http.Post json格式文件,总是报错

moon
In reply to this post by moon
soluted by update to nGrinder v3.4.1