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

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

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

HttpResponse.response介绍

[英]Static builder to create a response.
[中]创建响应的静态生成器。

代码示例

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

@Override
public void writeResponse(HttpRequest request, HttpResponseStatus responseStatus, String body, String contentType) {
  HttpResponse response = response()
    .withStatusCode(responseStatus.code())
    .withBody(body);
  if (body != null && !body.isEmpty()) {
    response.replaceHeader(header(CONTENT_TYPE.toString(), contentType + "; charset=utf-8"));
  }
  writeResponse(request, response, true);
}

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

@Override
public void writeResponse(HttpRequest request, HttpResponseStatus responseStatus, String body, String contentType) {
  HttpResponse response = response()
    .withStatusCode(responseStatus.code())
    .withReasonPhrase(responseStatus.reasonPhrase())
    .withBody(body);
  if (body != null && !body.isEmpty()) {
    response.replaceHeader(header(CONTENT_TYPE.toString(), contentType + "; charset=utf-8"));
  }
  writeResponse(request, response, true);
}

代码示例来源:origin: testcontainers/testcontainers-java

private static String responseFromMockserver(MockServerContainer mockServer, String expectedBody, String path) throws IOException {
    new MockServerClient(mockServer.getContainerIpAddress(), mockServer.getServerPort())
      .when(request(path))
      .respond(response(expectedBody));

    URLConnection urlConnection = new URL(mockServer.getEndpoint() + path).openConnection();
    @Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    return reader.readLine();
  }
}

代码示例来源:origin: apache/incubator-gobblin

private void mockExample()
  throws IOException {
 mockServer.when(HttpRequest.request().withMethod("CONNECT").withPath("example.org:80"))
   .respond(HttpResponse.response().withStatusCode(200));
 mockServer.when(HttpRequest.request().withMethod("GET").withPath("/"))
   .respond(HttpResponse.response(IOUtils.toString(getClass().getResourceAsStream("/example.org.html"))));
}

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

public HttpResponse clone() {
    return response()
      .withStatusCode(statusCode)
      .withReasonPhrase(reasonPhrase)
      .withBody(body)
      .withHeaders(headers.clone())
      .withCookies(cookies.clone())
      .withDelay(getDelay())
      .withConnectionOptions(connectionOptions);
  }
}

代码示例来源: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 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 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 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: apache/incubator-gobblin

@Test(enabled=false, expectedExceptions = SocketException.class)
public void mustRefuseConnectionWhenProxyRefuses() throws Exception{
 mockServer.when(HttpRequest.request().withMethod("CONNECT").withPath("www.us.apache.org:80"))
   .respond(HttpResponse.response().withStatusCode(403));
 Tunnel tunnel = Tunnel.build("example.org", 80, "localhost", PORT);
 try {
  int tunnelPort = tunnel.getPort();
  fetchContent(tunnelPort);
 } finally {
  tunnel.close();
 }
}

代码示例来源: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()));
}

相关文章