예제 #1
0
 /** {@inheritDoc} */
 public void renderResult(SampleResult sampleResult) {
   if (!sampleResult.getContentType().equals("application/x-amf")) {
     xmlDataField.setText(
         AmfResources.getResString("invalid_amf_content_type") + sampleResult.getContentType());
   } else if (sampleResult.getResponseData().length == 0) {
     xmlDataField.setText(AmfResources.getResString("empty_response"));
   } else if (sampleResult.getResponseData().length > MAX_RESPONSE_SIZE * 1024) {
     xmlDataField.setText(
         AmfResources.getResString("response_too_large") + MAX_RESPONSE_SIZE + "k");
   } else {
     String xml = AmfXmlConverter.convertAmfMessageToXml(sampleResult.getResponseData());
     xmlDataField.setText(xml);
   }
   xmlDataField.setCaretPosition(0);
 }
예제 #2
0
  public void process() {
    Sampler sampler = JMeterContextService.getContext().getCurrentSampler();
    SampleResult responseText = JMeterContextService.getContext().getPreviousResult();
    if (responseText == null) {
      return;
    }
    initRegex(getArgumentName());
    String text = new String(responseText.getResponseData());
    Perl5Matcher matcher = JMeterUtils.getMatcher();
    String value = "";
    if (matcher.contains(text, case1)) {
      MatchResult result = matcher.getMatch();
      value = result.group(1);
    } else if (matcher.contains(text, case2)) {
      MatchResult result = matcher.getMatch();
      value = result.group(1);
    } else if (matcher.contains(text, case3)) {
      MatchResult result = matcher.getMatch();
      value = result.group(1);
    } else if (matcher.contains(text, case4)) {
      MatchResult result = matcher.getMatch();
      value = result.group(1);
    }

    modify((HTTPSampler) sampler, value);
  }
예제 #3
0
 /**
  * ********************************************************** !ToDoo (Method description)
  *
  * @param response !ToDo (Parameter description)
  * @return !ToDo (Return description) *********************************************************
  */
 public AssertionResult getResult(SampleResult response) {
   AssertionResult result;
   if (!response.isSuccessful()) {
     result = new AssertionResult();
     result.setError(true);
     result.setFailureMessage(new String((byte[]) response.getResponseData()));
     return result;
   }
   result = evaluateResponse(response);
   return result;
 }
예제 #4
0
 /**
  * Make sure the response satisfies the specified assertion requirements.
  *
  * @param response an instance of SampleResult
  * @return an instance of AssertionResult
  */
 private AssertionResult evaluateResponse(SampleResult response) {
   boolean pass = true;
   boolean not = (NOT & getTestType()) > 0;
   AssertionResult result = new AssertionResult();
   if (response.getResponseData() == null) {
     return setResultForNull(result);
   }
   String responseString = new String(response.getResponseData());
   try {
     // Get the Matcher for this thread
     Perl5Matcher localMatcher = (Perl5Matcher) matcher.get();
     PropertyIterator iter = getTestStrings().iterator();
     while (iter.hasNext()) {
       String stringPattern = iter.next().getStringValue();
       Pattern pattern = patternCache.getPattern(stringPattern, Perl5Compiler.READ_ONLY_MASK);
       boolean found;
       if ((CONTAINS & getTestType()) > 0) {
         found = localMatcher.contains(responseString, pattern);
       } else {
         found = localMatcher.matches(responseString, pattern);
       }
       pass = not ? !found : found;
       if (!pass) {
         result.setFailure(true);
         result.setFailureMessage(
             "Test Failed, expected " + notMessage + failMessage + stringPattern);
         break;
       }
     }
     if (pass) {
       result.setFailure(false);
     }
     result.setError(false);
   } catch (MalformedCachePatternException e) {
     result.setError(true);
     result.setFailure(false);
     result.setFailureMessage("Bad test configuration" + e);
   }
   return result;
 }
  @Override
  public AssertionResult getResult(SampleResult samplerResult) {
    AssertionResult result = new AssertionResult(getName());
    byte[] responseData = samplerResult.getResponseData();
    if (responseData.length == 0) {
      return result.setResultForNull();
    }

    if (isJsonValidationBool()) {
      try {
        if (checkJSONPathWithValidation(
            new String(responseData, "UTF-8"), getJsonPath(), getExpectedValue())) {
          result.setFailure(false);
          result.setFailureMessage("");
        }
      } catch (ParseException e) {
        result.setFailure(true);
        result.setFailureMessage(e.getClass().toString() + " - " + e.getMessage());
      } catch (Exception e) {
        result.setFailure(true);
        result.setFailureMessage(e.getMessage());
      }
    }

    if (!isJsonValidationBool()) {
      try {
        if (checkJSONPathWithoutValidation(new String(responseData), getJsonPath())) {
          result.setFailure(false);
          result.setFailureMessage("");
        }
      } catch (ParseException e) {
        result.setFailure(true);
        result.setFailureMessage(e.getClass().toString() + " - " + e.getMessage());
      } catch (Exception e) {
        result.setFailure(true);
        result.setFailureMessage(e.getMessage());
      }
    }

    return result;
  }
예제 #6
0
 private String getInputString(SampleResult result) {
   String inputString =
       useUrl()
           ? result.getUrlAsString() // Bug 39707
           : useHeaders()
               ? result.getResponseHeaders()
               : useRequestHeaders()
                   ? result.getRequestHeaders()
                   : useCode()
                       ? result.getResponseCode() // Bug 43451
                       : useMessage()
                           ? result.getResponseMessage() // Bug 43451
                           : useUnescapedBody()
                               ? StringEscapeUtils.unescapeHtml4(result.getResponseDataAsString())
                               : useBodyAsDocument()
                                   ? Document.getTextFromDocument(result.getResponseData())
                                   : result.getResponseDataAsString() // Bug 36898
       ;
   if (log.isDebugEnabled()) {
     log.debug("Input = " + inputString);
   }
   return inputString;
 }
예제 #7
0
  /*
   * @param response @return
   */
  public AssertionResult getResult(SampleResult response) {

    AssertionResult result = new AssertionResult(getName());
    result.setFailure(false);
    byte[] resultData = response.getResponseData();

    if (resultData.length == 0) {
      result.setError(false);
      result.setFailure(true);
      result.setFailureMessage("Response was null");
      return result;
    }

    // no point in checking if we don't have anything to compare against
    if (getAllowedMD5Hex().equals("")) {
      result.setError(false);
      result.setFailure(true);
      result.setFailureMessage("MD5Hex to test against is empty");
      return result;
    }

    String md5Result = baMD5Hex(resultData);

    // String md5Result = DigestUtils.md5Hex(resultData);

    if (!md5Result.equalsIgnoreCase(getAllowedMD5Hex())) {
      result.setFailure(true);

      Object[] arguments = {md5Result, getAllowedMD5Hex()};
      String message =
          MessageFormat.format(
              JMeterUtils.getResString("md5hex_assertion_failure"), arguments); // $NON-NLS-1$
      result.setFailureMessage(message);
    }

    return result;
  }
예제 #8
0
  /** {@inheritDoc} */
  @Override
  public AssertionResult getResult(SampleResult response) {
    AssertionResult result = new AssertionResult(getName());

    final BeanShellInterpreter bshInterpreter = getBeanShellInterpreter();
    if (bshInterpreter == null) {
      result.setFailure(true);
      result.setError(true);
      result.setFailureMessage("BeanShell Interpreter not found");
      return result;
    }
    try {

      // Add SamplerData for consistency with BeanShell Sampler
      bshInterpreter.set("SampleResult", response); // $NON-NLS-1$
      bshInterpreter.set("Response", response); // $NON-NLS-1$
      bshInterpreter.set("ResponseData", response.getResponseData()); // $NON-NLS-1$
      bshInterpreter.set("ResponseCode", response.getResponseCode()); // $NON-NLS-1$
      bshInterpreter.set("ResponseMessage", response.getResponseMessage()); // $NON-NLS-1$
      bshInterpreter.set("ResponseHeaders", response.getResponseHeaders()); // $NON-NLS-1$
      bshInterpreter.set("RequestHeaders", response.getRequestHeaders()); // $NON-NLS-1$
      bshInterpreter.set("SampleLabel", response.getSampleLabel()); // $NON-NLS-1$
      bshInterpreter.set("SamplerData", response.getSamplerData()); // $NON-NLS-1$
      bshInterpreter.set("Successful", response.isSuccessful()); // $NON-NLS-1$

      // The following are used to set the Result details on return from
      // the script:
      bshInterpreter.set("FailureMessage", ""); // $NON-NLS-1$ //$NON-NLS-2$
      bshInterpreter.set("Failure", false); // $NON-NLS-1$

      processFileOrScript(bshInterpreter);

      result.setFailureMessage(bshInterpreter.get("FailureMessage").toString()); // $NON-NLS-1$
      result.setFailure(
          Boolean.parseBoolean(
              bshInterpreter
                  .get("Failure") // $NON-NLS-1$
                  .toString()));
      result.setError(false);
    }
    /*
     * To avoid class loading problems when the BSH jar is missing, we don't
     * try to catch this error separately catch (bsh.EvalError ex) {
     * log.debug("",ex); result.setError(true);
     * result.setFailureMessage(ex.toString()); }
     */
    // but we do trap this error to make tests work better
    catch (NoClassDefFoundError ex) {
      log.error("BeanShell Jar missing? " + ex.toString());
      result.setError(true);
      result.setFailureMessage("BeanShell Jar missing? " + ex.toString());
      response.setStopThread(true); // No point continuing
    } catch (Exception ex) // Mainly for bsh.EvalError
    {
      result.setError(true);
      result.setFailureMessage(ex.toString());
      log.warn(ex.toString());
    }

    return result;
  }
예제 #9
0
  public String execute(SampleResult previousResult, Sampler currentSampler)
      throws InvalidVariableException {
    try {
      searchPattern = compiler.compile(((CompoundVariable) values[0]).execute());
      generateTemplate(((CompoundVariable) values[1]).execute());

      if (values.length > 2) {
        valueIndex = ((CompoundVariable) values[2]).execute();
      }
      if (valueIndex.equals("")) {
        valueIndex = "1";
      }

      if (values.length > 3) {
        between = ((CompoundVariable) values[3]).execute();
      }

      if (values.length > 4) {
        String dv = ((CompoundVariable) values[4]).execute();
        if (!dv.equals("")) {
          defaultValue = dv;
        }
      }

      if (values.length > 5) {
        name = ((CompoundVariable) values[values.length - 1]).execute();
      }
    } catch (MalformedPatternException e) {
      log.error("", e);
      throw new InvalidVariableException("Bad regex pattern");
    } catch (Exception e) {
      throw new InvalidVariableException(e.getMessage());
    }

    getVariables().put(name, defaultValue);
    if (previousResult == null || previousResult.getResponseData() == null) {
      return defaultValue;
    }

    List collectAllMatches = new ArrayList();
    try {
      PatternMatcher matcher = (PatternMatcher) localMatcher.get();
      String responseText = new String(previousResult.getResponseData());
      PatternMatcherInput input = new PatternMatcherInput(responseText);
      while (matcher.contains(input, searchPattern)) {
        MatchResult match = matcher.getMatch();
        collectAllMatches.add(match);
      }
    } catch (NumberFormatException e) {
      log.error("", e);
      return defaultValue;
    } catch (Exception e) {
      return defaultValue;
    }

    if (collectAllMatches.size() == 0) {
      return defaultValue;
    }

    if (valueIndex.equals(ALL)) {
      StringBuffer value = new StringBuffer();
      Iterator it = collectAllMatches.iterator();
      boolean first = true;
      while (it.hasNext()) {
        if (!first) {
          value.append(between);
        } else {
          first = false;
        }
        value.append(generateResult((MatchResult) it.next()));
      }
      return value.toString();
    } else if (valueIndex.equals(RAND)) {
      MatchResult result =
          (MatchResult) collectAllMatches.get(rand.nextInt(collectAllMatches.size()));
      return generateResult(result);
    } else {
      try {
        int index = Integer.parseInt(valueIndex) - 1;
        MatchResult result = (MatchResult) collectAllMatches.get(index);
        return generateResult(result);
      } catch (NumberFormatException e) {
        float ratio = Float.parseFloat(valueIndex);
        MatchResult result =
            (MatchResult) collectAllMatches.get((int) (collectAllMatches.size() * ratio + .5) - 1);
        return generateResult(result);
      } catch (IndexOutOfBoundsException e) {
        return defaultValue;
      }
    }
  }