2017-02-08 1 views
0
val tName = System.getProperty("tName", "asdflkjh") 

에서 "TNAME"을 통과하면서 나는 "tName" 동적으로 만들 계획입니다 표시되어 있지만 대신 "asdflkjh"의 출력에 "$tName"을 인쇄합니다. 내가 stringBody에 'session=>s'를 사용하는 경우 .body(StringBody("""{"objectId":${assetid},"objectType":"m-asset","name1": "$tName","accountId":4,"userId":5}"""))유형 불일치 오류가 stringbody

는 오류 아래 .body(StringBody(session=>s"""{"objectId":${assetid},"objectType":"m-asset","name1": "$tName","accountId":4,"userId":5}"""))

////////////////////// 오류가 발생합니다 : ///// ///////////////////////////

3894 [main] ERROR io.gatling.compiler.ZincCompiler$ - type mismatch; 
found : String("${ThumbIdList}") 
required: ?{def apply: ?} 
Note that implicit conversions are not applicable because they are ambiguous: 
both method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps 
and method stringToExpression in object Predef of type [T](string: String)(implicit evidence$1: scala.reflect.ClassTag[T])io.gatling.core.session.Expression[T] 
are possible conversion functions from String("${ThumbIdList}") to ?{def apply: ?} 
3894 [main] ERROR io.gatling.compiler.ZincCompiler$ -  .foreach("${ThumbIdList}", "thumbid") { 
3894 [main] ERROR io.gatling.compiler.ZincCompiler$ -    ^
3914 [main] ERROR io.gatling.compiler.ZincCompiler$ - D:\myTags.scala:110: type mismatch; 
found : String("thumbid") 
required: ?{def apply: ?} 
Note that implicit conversions are not applicable because they are ambiguous: 
both method augmentString in object Predef of type (x: String)scala.collection.immutable.StringOps 
and method stringToExpression in object Predef of type [T](string: String)(implicit evidence$1: scala.reflect.ClassTag[T])io.gatling.core.session.Expression[T] 
are possible conversion functions from String("thumbid") to ?{def apply: ?} 
3914 [main] ERROR io.gatling.compiler.ZincCompiler$ -  .foreach("${ThumbIdList}", "thumbid") { 
3924 [main] ERROR io.gatling.compiler.ZincCompiler$ -        ^
4004 [main] ERROR io.gatling.compiler.ZincCompiler$ - D:\myTags.scala:98: not found: value assetid 
4004 [main] ERROR io.gatling.compiler.ZincCompiler$ -   .body(StringBody(session=>s"""{"objectId":${assetid},"objectType":"m-asset","name1": "$tName","accountId":4,"userId":5}""")) 
4004 [main] ERROR io.gatling.compiler.ZincCompiler$ -              ^
4054 [main] ERROR io.gatling.compiler.ZincCompiler$ - three errors found 
4054 [main] DEBUG io.gatling.compiler.ZincCompiler$ - Compilation failed (CompilerInterface) 

///////////// 스크립트 여기에서 시작하십시오 ///////////

import scala.concurrent.duration._ 
import io.gatling.core.Predef._ 
import io.gatling.http.Predef._ 
import io.gatling.jdbc.Predef._ 

class myTags extends Simulation { 

    val testServerUrl = System.getProperty("testServerUrl", "https:someurl") 
    val username = System.getProperty("username", "ma") 
    val password = System.getProperty("password", "ma") 
    val userCount = Integer.getInteger("userCount", 1).toInt 
    val accountname = System.getProperty("accountname", "ma1") 
    val tName = System.getProperty("tName", "asdflkjh") 

    val httpProtocol = http 
     .baseURL(testServerUrl) 

    val headers_6 = Map(
     "Accept" -> "application/json, text/plain, */*", 
     "Cache-Control" -> "no-cache", 
     "If-Modified-Since" -> "Mon, 26 Jul 1997 05:00:00 GMT", 
     "Pragma" -> "no-cache", 
     "X-Requested-With" -> "XMLHttpRequest") 

    val headers_52 = Map(
     "Accept" -> "application/json, text/plain, */*", 
     "Accept-Encoding" -> "gzip, deflate, br", 
     "Cache-Control" -> "no-cache", 
     "Origin" -> testServerUrl, 
     "Pragma" -> "no-cache", 
     "X-Requested-With" -> "XMLHttpRequest", 
     "Content-Type" -> "application/json;charset=UTF-8", 
     "Connection" -> "keep-alive", 
     "User-Agent" -> "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36") 

    val scn = scenario("Add") 
     .exec(http("Fetch") 
      .get("/someurl2") 
      .headers(headers_6) 
      .check(jsonPath("$.resources[*].ids").findAll.saveAs("IdList")) 
      .check(jsonPath("$.resources[*].thumbIds").findAll.saveAs("ThumbIdList"))) 

     .foreach("${IdList}", "assetid") { 
      exec(http("Load_Details") 
      .get("/mmm/images/loader.svg") 
      .resources(
      http("C1_request") 
      .get("/mmm/ast/${assetid}/c1") 
      .headers(headers_6), 
      http("T1_request") 
      .get("/mmm/ast/${assetid}/t1") 
      .headers(headers_6), 
      http("A1_request") 
      .post("/mmm/actions") 
      .headers(headers_52) 
      .body(StringBody("""{"objects":[{"id":${assetid},"resource":"m-asset"}]}""")), 
      http("R1_request") 
      .get("/mmm/variants%3BresourceType=m-asset") 
      .headers(headers_6), 
      http("S1_request") 
      .get("/mmm/ast/${assetid}/keyframes") 
      .headers(headers_6))) 

     .exec(http("Add Tags") 
      .post("/mmm/objs/${assetid}/tags") 
      .headers(headers_52) 

      // The PROBLEM IS Here. I want to pass "$tName" dynamically 
      .body(StringBody("""{"objectId":${assetid},"objectType":"m-asset","name1": "$tName","accountId":4,"userId":5}""")) 
      ) 
     } 

     .foreach("${ThumbIdList}", "thumbid") { 
      doIf(session => session("thumbid").as[String] != "-1") 
      { 
       exec(http("Set_Keyframes") 
       .get("/mmm/keyframes/${thumbid};width=185;height=103") 
       ) 
      } 
     } 

    setUp(scn.inject(atOnceUsers(userCount))).protocols(httpProtocol) 
} 

답변

1

오류는 문자열 보간 (s"""...""")로 전환 할 때 그래서 오해, 당신은 이미 문자열에 $을 가지고 그냥 : 예,이 일을하려고하면. $$을 사용하여 이스케이프 처리하십시오 (s"""{"objectId":$${assetid},"objectType":"m-asset","name1": "$tName","accountId":4,"userId":5}"""). session => ... 부분은 문자열 보간법과 완전히 직교합니다.

나는 안전하지 않습니다 : tName에 따옴표가 포함 된 경우 어떻게해야합니까? 이 경우 환경 변수를 제어하는 ​​시스템에서 스크립트를 실행하려고하지만 "SQL injection"을 찾아보고 문제가 SQL에 국한되지 않음을 알고 있다고 가정하면 중요하지 않을 수 있습니다.

+0

고마워, Alexey Romanov. 그것은 효과가 있었다. 감사 :-) – Peter

0

환경 변수 값을 얻으려고합니까?

val tName = sys.env("tName").getOrElse(doSomething here...) 
+0

아니요 tName의 값을 가져 오려고합니다. 위의 대답은 Alexey Romanov가 나를 위해 일했습니다. 답장과 노력에 감사드립니다. – Peter