Java Code Examples for org.apache.jmeter.samplers.SampleResult#setRequestHeaders()

The following examples show how to use org.apache.jmeter.samplers.SampleResult#setRequestHeaders() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WebSocketAbstractSampler.java    From jmeter-bzm-plugins with Apache License 2.0 4 votes vote down vote up
public WebSocketConnection getConnectionWS (String connectionId, SampleResult sampleResult) throws Exception{
  	
  	
  	
  	URI uri = getUri();
  
  	String closeConnectionPattern = getCloseConnectionPattern();
  	int connectionTimeout;
  	
  	try {
  		connectionTimeout = Integer.parseInt(getConnectionTimeout());
      } catch (NumberFormatException ex) {
          log.warn("Request timeout is not a number; using the default connection timeout of " + DEFAULT_CONNECTION_TIMEOUT + "ms");
          connectionTimeout = DEFAULT_CONNECTION_TIMEOUT;
      }

  	SslContextFactory sslContexFactory = new SslContextFactory();
      sslContexFactory.setTrustAll(true);
      WebSocketClient webSocketClient = new WebSocketClient(sslContexFactory);
      WebSocketConnection webSocketConnection = factory.getWebSocketHandler(webSocketClient, closeConnectionPattern, getContentEncoding());
      
webSocketClient.start();
      
      ClientUpgradeRequest request = new ClientUpgradeRequest();
      setConnectionHeaders(request, getHeaderManager(), null);
      setConnectionCookie(webSocketClient, uri, getCookieManager());
      webSocketClient.connect(webSocketConnection, uri, request);
      
      for (Map.Entry<String, List<String>> entry : request.getHeaders().entrySet()){
      	sampleResult.setRequestHeaders(sampleResult.getRequestHeaders() + entry.getKey() + ": ");
      	for (String s : entry.getValue()){
      		sampleResult.setRequestHeaders(sampleResult.getRequestHeaders() + entry.getValue() + " ");
      	}
      	sampleResult.setRequestHeaders(sampleResult.getRequestHeaders() + "\n");
      }
      
      sampleResult.setRequestHeaders(sampleResult.getRequestHeaders() + "\n\nCookies: \n\n");
      
      for (HttpCookie h : webSocketClient.getCookieStore().getCookies()){
      	sampleResult.setRequestHeaders(sampleResult.getRequestHeaders() + h);
      }

webSocketConnection.awaitOpen(connectionTimeout, TimeUnit.MILLISECONDS);

      
      if (!webSocketConnection.isConnected()){
  		return null;
      }
      
      connectionList.put(connectionId, webSocketConnection);
      
      return webSocketConnection;
  }
 
Example 2
Source File: JMeterXMPPSampler.java    From jmeter-bzm-plugins with Apache License 2.0 4 votes vote down vote up
@Override
public SampleResult sample(Entry entry) {
    SampleResult res = new SampleResult();
    res.setSampleLabel(getName());
    res.setDataType(SampleResult.TEXT);
    res.setSuccessful(true);
    res.setResponseCode("200");
    res.setResponseMessage("OK");

    res.sampleStart();
    try {
        if (connConfig == null) {
            throw new RuntimeException("Cannot sample XMPP without XMPP Connection component");
        }

        XMPPConnection conn = getXMPPConnection();

        if (conn == null) {
            throw new RuntimeException("No XMPP Connection available");
        }

        String headers = "Connection ID: " + conn.getConnectionID() + "\r\n";

        String action = getAction();
        if (!conn.isConnected() && !action.equals(Connect.class.getCanonicalName())) {
            log.error("Please call Connect before calling other actions");
            throw new SmackException.NotConnectedException();
        }

        headers += "User: " + conn.getUser() + "\r\n";

        res.setRequestHeaders(headers);

        AbstractXMPPAction actObject = connConfig.getActions().get(action);
        if (actObject.perform(this, res) == null) {
            return null;
        }
    } catch (Exception e) {
        log.error("Error in XMPP Sampler: ", e);
        res.setSuccessful(false);
        res.setResponseCode("500");
        res.setResponseMessage((e.getMessage() == null || e.getMessage().isEmpty()) ? e.toString() : e.getMessage());
        res.setResponseData(ExceptionUtils.getStackTrace(e).getBytes());
    }

    res.sampleEnd();
    return res;
}