org.mockserver.model.HttpRequest.withHeader()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(13.9k)|赞(0)|评价(0)|浏览(115)

本文整理了Java中org.mockserver.model.HttpRequest.withHeader()方法的一些代码示例,展示了HttpRequest.withHeader()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.withHeader()方法的具体详情如下:
包路径:org.mockserver.model.HttpRequest
类名称:HttpRequest
方法名:withHeader

HttpRequest.withHeader介绍

[英]Adds one header to match which can specified using plain strings or regular expressions (for more details of the supported regex syntax see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)
[中]添加一个标头以匹配可以使用普通字符串或正则表达式指定的标头(有关支持的正则表达式语法的更多详细信息,请参阅http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html)

代码示例

代码示例来源:origin: jamesdbloom/mockserver

public void handle(final ActionHandler actionHandler, final HttpObjectCallback httpObjectCallback, final HttpRequest request, final ResponseWriter responseWriter, final boolean synchronous) {
  final String clientId = httpObjectCallback.getClientId();
  final String webSocketCorrelationId = UUID.randomUUID().toString();
  webSocketClientRegistry.registerResponseCallbackHandler(webSocketCorrelationId, new WebSocketResponseCallback() {
    @Override
    public void handle(HttpResponse response) {
      webSocketClientRegistry.unregisterResponseCallbackHandler(webSocketCorrelationId);
      actionHandler.writeResponseActionResponse(response.removeHeader(WEB_SOCKET_CORRELATION_ID_HEADER_NAME), responseWriter, request, httpObjectCallback, synchronous);
    }
  });
  webSocketClientRegistry.sendClientMessage(clientId, request.clone().withHeader(WEB_SOCKET_CORRELATION_ID_HEADER_NAME, webSocketCorrelationId));
}

代码示例来源:origin: jamesdbloom/mockserver

public void handle(final ActionHandler actionHandler, final HttpObjectCallback httpObjectCallback, final HttpRequest request, final ResponseWriter responseWriter, final boolean synchronous) {
  final String clientId = httpObjectCallback.getClientId();
  final String webSocketCorrelationId = UUID.randomUUID().toString();
  webSocketClientRegistry.registerForwardCallbackHandler(webSocketCorrelationId, new WebSocketRequestCallback() {
    @Override
    public void handle(final HttpRequest request) {
      final HttpForwardActionResult responseFuture = sendRequest(request.removeHeader(WEB_SOCKET_CORRELATION_ID_HEADER_NAME), null);
      webSocketClientRegistry.unregisterForwardCallbackHandler(webSocketCorrelationId);
      actionHandler.writeForwardActionResponse(responseFuture, responseWriter, request, httpObjectCallback, synchronous);
    }
    @Override
    public void handleError(HttpResponse httpResponse) {
      webSocketClientRegistry.unregisterForwardCallbackHandler(webSocketCorrelationId);
      actionHandler.writeResponseActionResponse(httpResponse, responseWriter, request, httpObjectCallback, synchronous);
    }
  });
  webSocketClientRegistry.sendClientMessage(clientId, request.clone().withHeader(WEB_SOCKET_CORRELATION_ID_HEADER_NAME, webSocketCorrelationId));
}

代码示例来源:origin: jamesdbloom/mockserver

@Override
  public HttpRequest handle(HttpRequest httpRequest) {
    return request()
      .withHeader("Host", "localhost:" + httpRequest.getFirstHeader("x-echo-server-port"))
      .withHeader("x-test", httpRequest.getFirstHeader("x-test"))
      .withBody("some_overridden_body")
      .withSecure(httpRequest.isSecure());
  }
}

代码示例来源:origin: jamesdbloom/mockserver

@Test
public void shouldReturnErrorForInvalidRequestToVerify() throws Exception {
  // when
  org.mockserver.model.HttpResponse httpResponse = new NettyHttpClient(clientEventLoopGroup, null).sendRequest(
    request()
      .withMethod("PUT")
      .withHeader(HOST.toString(), "localhost:" + getProxyPort())
      .withPath(addContextToPath("mockserver/verify"))
      .withBody("{" + NEW_LINE +
        "    \"httpRequest\": {" + NEW_LINE +
        "        \"path\": \"/simple\"" + NEW_LINE +
        "    }, " + NEW_LINE +
        "    \"times\": 1" + NEW_LINE +
        "}")
  ).get(10, TimeUnit.SECONDS);
  // then
  assertThat(httpResponse.getStatusCode(), Is.is(400));
  assertThat(httpResponse.getBodyAsString(), Is.is("1 error:" + NEW_LINE +
    " - instance type (integer) does not match any allowed primitive type (allowed: [\"object\"]) for field \"/times\""));
}

代码示例来源:origin: jamesdbloom/mockserver

@Test
public void shouldReturnErrorForInvalidRequestToClear() throws Exception {
  // when
  org.mockserver.model.HttpResponse httpResponse = new NettyHttpClient(clientEventLoopGroup, null).sendRequest(
    request()
      .withMethod("PUT")
      .withHeader(HOST.toString(), "localhost:" + getProxyPort())
      .withPath(addContextToPath("mockserver/clear"))
      .withBody("{" + NEW_LINE +
        "    \"path\" : 500," + NEW_LINE +
        "    \"method\" : true," + NEW_LINE +
        "    \"keepAlive\" : \"false\"" + NEW_LINE +
        "  }")
  ).get(10, TimeUnit.SECONDS);
  // then
  assertThat(httpResponse.getStatusCode(), Is.is(400));
  assertThat(httpResponse.getBodyAsString(), Is.is("3 errors:" + NEW_LINE +
    " - instance type (string) does not match any allowed primitive type (allowed: [\"boolean\"]) for field \"/keepAlive\"" + NEW_LINE +
    " - instance type (boolean) does not match any allowed primitive type (allowed: [\"string\"]) for field \"/method\"" + NEW_LINE +
    " - instance type (integer) does not match any allowed primitive type (allowed: [\"string\"]) for field \"/path\""));
}

代码示例来源:origin: jamesdbloom/mockserver

@Override
  public HttpResponse call() throws Exception {
    return httpClient.sendRequest(
      request(addContextToPath(calculatePath("delayed")))
        .withHeader(HOST.toString(), "localhost:" + getServerPort())
    ).get(10, TimeUnit.SECONDS);
  }
});

代码示例来源:origin: Netflix/eureka

@Test
public void testHeartbeatReplicationWithNoResponseBody() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("PUT")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + instanceInfo.getAppName() + '/' + instanceInfo.getId())
  ).respond(
      response().withStatusCode(200)
  );
  EurekaHttpResponse<InstanceInfo> response = replicationClient.sendHeartBeat(instanceInfo.getAppName(), instanceInfo.getId(), instanceInfo, InstanceStatus.DOWN);
  assertThat(response.getStatusCode(), is(equalTo(200)));
  assertThat(response.getEntity(), is(nullValue()));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testRegistrationReplication() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("POST")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + instanceInfo.getAppName())
  ).respond(
      response().withStatusCode(200)
  );
  EurekaHttpResponse<Void> response = replicationClient.register(instanceInfo);
  assertThat(response.getStatusCode(), is(equalTo(200)));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testRegistrationReplication() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("POST")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + instanceInfo.getAppName())
  ).respond(
      response().withStatusCode(200)
  );
  EurekaHttpResponse<Void> response = replicationClient.register(instanceInfo);
  assertThat(response.getStatusCode(), is(equalTo(200)));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testHeartbeatReplicationWithNoResponseBody() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("PUT")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + instanceInfo.getAppName() + '/' + instanceInfo.getId())
  ).respond(
      response().withStatusCode(200)
  );
  EurekaHttpResponse<InstanceInfo> response = replicationClient.sendHeartBeat(instanceInfo.getAppName(), instanceInfo.getId(), instanceInfo, InstanceStatus.DOWN);
  assertThat(response.getStatusCode(), is(equalTo(200)));
  assertThat(response.getEntity(), is(nullValue()));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testAsgStatusUpdateReplication() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("PUT")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/asg/" + instanceInfo.getASGName() + "/status")
  ).respond(
      response().withStatusCode(200)
  );
  EurekaHttpResponse<Void> response = replicationClient.statusUpdate(instanceInfo.getASGName(), ASGStatus.ENABLED);
  assertThat(response.getStatusCode(), is(equalTo(200)));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testAsgStatusUpdateReplication() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("PUT")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/asg/" + instanceInfo.getASGName() + "/status")
  ).respond(
      response().withStatusCode(200)
  );
  EurekaHttpResponse<Void> response = replicationClient.statusUpdate(instanceInfo.getASGName(), ASGStatus.ENABLED);
  assertThat(response.getStatusCode(), is(equalTo(200)));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testStatusUpdateReplication() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("PUT")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + instanceInfo.getAppName() + '/' + instanceInfo.getId() + "/status")
  ).respond(
      response().withStatusCode(200)
  );
  EurekaHttpResponse<Void> response = replicationClient.statusUpdate(instanceInfo.getAppName(), instanceInfo.getId(), InstanceStatus.DOWN, instanceInfo);
  assertThat(response.getStatusCode(), is(equalTo(200)));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testStatusUpdateReplication() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("PUT")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + instanceInfo.getAppName() + '/' + instanceInfo.getId() + "/status")
  ).respond(
      response().withStatusCode(200)
  );
  EurekaHttpResponse<Void> response = replicationClient.statusUpdate(instanceInfo.getAppName(), instanceInfo.getId(), InstanceStatus.DOWN, instanceInfo);
  assertThat(response.getStatusCode(), is(equalTo(200)));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testCancelReplication() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("DELETE")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + instanceInfo.getAppName() + '/' + instanceInfo.getId())
  ).respond(
      response().withStatusCode(204)
  );
  EurekaHttpResponse<Void> response = replicationClient.cancel(instanceInfo.getAppName(), instanceInfo.getId());
  assertThat(response.getStatusCode(), is(equalTo(204)));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testCancelReplication() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("DELETE")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + instanceInfo.getAppName() + '/' + instanceInfo.getId())
  ).respond(
      response().withStatusCode(204)
  );
  EurekaHttpResponse<Void> response = replicationClient.cancel(instanceInfo.getAppName(), instanceInfo.getId());
  assertThat(response.getStatusCode(), is(equalTo(204)));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testDeleteStatusOverrideReplication() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("DELETE")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + instanceInfo.getAppName() + '/' + instanceInfo.getId() + "/status")
  ).respond(
      response().withStatusCode(204)
  );
  EurekaHttpResponse<Void> response = replicationClient.deleteStatusOverride(instanceInfo.getAppName(), instanceInfo.getId(), instanceInfo);
  assertThat(response.getStatusCode(), is(equalTo(204)));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testDeleteStatusOverrideReplication() throws Exception {
  serverMockClient.when(
      request()
          .withMethod("DELETE")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + instanceInfo.getAppName() + '/' + instanceInfo.getId() + "/status")
  ).respond(
      response().withStatusCode(204)
  );
  EurekaHttpResponse<Void> response = replicationClient.deleteStatusOverride(instanceInfo.getAppName(), instanceInfo.getId(), instanceInfo);
  assertThat(response.getStatusCode(), is(equalTo(204)));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testHeartbeatReplicationWithResponseBody() throws Exception {
  InstanceInfo remoteInfo = new InstanceInfo(this.instanceInfo);
  remoteInfo.setStatus(InstanceStatus.DOWN);
  byte[] responseBody = toGzippedJson(remoteInfo);
  serverMockClient.when(
      request()
          .withMethod("PUT")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + this.instanceInfo.getAppName() + '/' + this.instanceInfo.getId())
  ).respond(
      response()
          .withStatusCode(Status.CONFLICT.getStatusCode())
          .withHeader(header("Content-Type", MediaType.APPLICATION_JSON))
          .withHeader(header("Content-Encoding", "gzip"))
          .withBody(responseBody)
  );
  EurekaHttpResponse<InstanceInfo> response = replicationClient.sendHeartBeat(this.instanceInfo.getAppName(), this.instanceInfo.getId(), this.instanceInfo, null);
  assertThat(response.getStatusCode(), is(equalTo(Status.CONFLICT.getStatusCode())));
  assertThat(response.getEntity(), is(notNullValue()));
}

代码示例来源:origin: Netflix/eureka

@Test
public void testHeartbeatReplicationWithResponseBody() throws Exception {
  InstanceInfo remoteInfo = new InstanceInfo(this.instanceInfo);
  remoteInfo.setStatus(InstanceStatus.DOWN);
  byte[] responseBody = toGzippedJson(remoteInfo);
  serverMockClient.when(
      request()
          .withMethod("PUT")
          .withHeader(header(PeerEurekaNode.HEADER_REPLICATION, "true"))
          .withPath("/eureka/v2/apps/" + this.instanceInfo.getAppName() + '/' + this.instanceInfo.getId())
  ).respond(
      response()
          .withStatusCode(Status.CONFLICT.getStatusCode())
          .withHeader(header("Content-Type", MediaType.APPLICATION_JSON))
          .withHeader(header("Content-Encoding", "gzip"))
          .withBody(responseBody)
  );
  EurekaHttpResponse<InstanceInfo> response = replicationClient.sendHeartBeat(this.instanceInfo.getAppName(), this.instanceInfo.getId(), this.instanceInfo, null);
  assertThat(response.getStatusCode(), is(equalTo(Status.CONFLICT.getStatusCode())));
  assertThat(response.getEntity(), is(notNullValue()));
}

相关文章