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

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

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

HttpResponse.withHeader介绍

[英]Add a header to return as a Header object, if a header with the same name already exists this will NOT be modified but two headers will exist
[中]添加一个要作为header对象返回的header,如果已经存在同名的header,则不会修改该header,但将存在两个header

代码示例

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

private void setHeaderIfNotAlreadyExists(HttpResponse response, String name, String value) {
  if (response.getFirstHeader(name).isEmpty()) {
    response.withHeader(name, value);
  }
}

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

response =
    response()
      .withHeader(HttpHeaderNames.CONTENT_TYPE.toString(), MIME_MAP.get(extension))
      .withHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), String.valueOf(content.length()))
      .withBody(content);
} else {
  response =
    response()
      .withHeader(HttpHeaderNames.CONTENT_TYPE.toString(), MIME_MAP.get(extension))
      .withHeader(HttpHeaderNames.CONTENT_LENGTH.toString(), String.valueOf(bytes.length))
      .withBody(bytes);
  response.withHeader(HttpHeaderNames.CONNECTION.toString(), HttpHeaderValues.KEEP_ALIVE.toString());

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

@Override
public void writeResponse(HttpRequest request, HttpResponse response, boolean apiResponse) {
  if (response == null) {
    response = notFoundResponse();
  }
  if (enableCORSForAllResponses()) {
    addCORSHeaders.addCORSHeaders(request, response);
  } else if (apiResponse && enableCORSForAPI()) {
    addCORSHeaders.addCORSHeaders(request, response);
  }
  if (apiResponse) {
    response.withHeader("version", org.mockserver.Version.getVersion());
    final String path = request.getPath().getValue();
    if (!path.startsWith(PATH_PREFIX)) {
      response.withHeader("deprecated",
        "\"" + path + "\" is deprecated use \"" + PATH_PREFIX + path + "\" instead");
    }
  }
  addConnectionHeader(request, response);
  writeAndCloseSocket(ctx, request, response);
}

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

@Override
public void writeResponse(HttpRequest request, HttpResponse response, boolean apiResponse) {
  if (response == null) {
    response = notFoundResponse();
  }
  if (enableCORSForAllResponses()) {
    addCORSHeaders.addCORSHeaders(request, response);
  } else if (apiResponse && enableCORSForAPI()) {
    addCORSHeaders.addCORSHeaders(request, response);
  }
  if (apiResponse) {
    response.withHeader("version", org.mockserver.Version.getVersion());
    final String path = request.getPath().getValue();
    if (!path.startsWith(PATH_PREFIX)) {
      response.withHeader("deprecated",
        "\"" + path + "\" is deprecated use \"" + PATH_PREFIX + path + "\" instead");
    }
  }
  addConnectionHeader(request, response);
  mockServerResponseToHttpServletResponseEncoder.mapMockServerResponseToHttpServletResponse(response, httpServletResponse);
}

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

ctx.writeAndFlush(response()
    .withStatusCode(PROXY_AUTHENTICATION_REQUIRED.code())
    .withHeader(PROXY_AUTHENTICATE.toString(), "Basic realm=\"" + ConfigurationProperties.proxyAuthenticationRealm().replace("\"", "\\\"") + "\""));
} else {
  ctx.channel().attr(PROXYING).set(Boolean.TRUE);

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

private void returnNotFound(ResponseWriter responseWriter, HttpRequest request) {
    HttpResponse response = notFoundResponse();
    if (request.getHeaders().containsEntry("x-forwarded-by", "MockServer")) {
      response.withHeader("x-forwarded-by", "MockServer");
      mockServerLogger.trace(request, "no expectation for:{}returning response:{}", request, notFoundResponse());
    } else {
      httpStateHandler.log(new RequestLogEntry(request));
      mockServerLogger.info(EXPECTATION_NOT_MATCHED, request, "no expectation for:{}returning response:{}", request, notFoundResponse());
    }
    responseWriter.writeResponse(request, response, false);
  }
}

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

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

assertEquals(
  response("some_body_one")
    .withHeader("some", "header")
    .withHeader("cookie", "some=parameter")
    .withHeader("set-cookie", "some=parameter")
    .withCookie("some", "parameter"),
  makeRequest(

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

.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
    .withBody(body)
);
  .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
  .withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())
  .withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
  .withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())

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

.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_16).toString())
.withStatusCode(OK_200.code())
.withReasonPhrase(OK_200.reasonPhrase())
.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_16).toString())
.withStatusCode(OK_200.code())
.withReasonPhrase(OK_200.reasonPhrase())

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

.withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())
  .withHeader("name", "value")
  .withBody("some_request_body"),
makeRequest(
  .withStatusCode(OK_200.code())
  .withReasonPhrase(OK_200.reasonPhrase())
  .withHeader("name", "value")
  .withBody("some_request_body"),
makeRequest(

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

.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
.withStatusCode(OK_200.code())
.withReasonPhrase(OK_200.reasonPhrase())
.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
.withStatusCode(OK_200.code())
.withReasonPhrase(OK_200.reasonPhrase())

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

.withReasonPhrase(OK_200.reasonPhrase())
  .withCookie("name", "value")
  .withHeader("set-cookie", "name=value")
  .withBody("{\"method\":\"GET\",\"path\":\"/some_path\",\"body\":\"some_request_body\"}"),
makeRequest(
  .withReasonPhrase(OK_200.reasonPhrase())
  .withCookie("name", "value")
  .withHeader("set-cookie", "name=value")
  .withBody("{\"method\":\"GET\",\"path\":\"/some_path\",\"body\":\"some_request_body\"}"),
makeRequest(

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

.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
.withStatusCode(OK_200.code())
.withReasonPhrase(OK_200.reasonPhrase())
.withHeader(CONTENT_TYPE.toString(), MediaType.create("text", "plain").withCharset(StandardCharsets.UTF_8).toString())
.withStatusCode(OK_200.code())
.withReasonPhrase(OK_200.reasonPhrase())

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

response()
        .withStatusCode(302)
        .withHeader(new Header("Location", targetServerBaseUri + "/eureka/v2/apps/"))
);
targetServerMockClient.client.when(
    response()
        .withStatusCode(200)
        .withHeader(new Header("Content-Type", "application/json"))
        .withBody(fullFetchJson)
);
    response()
        .withStatusCode(200)
        .withHeader(new Header("Content-Type", "application/json"))
        .withBody(deltaFetchJson)
);

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

response()
        .withStatusCode(302)
        .withHeader(new Header("Location", targetServerBaseUri + "/eureka/v2/apps/"))
);
targetServerMockClient.client.when(
    response()
        .withStatusCode(200)
        .withHeader(new Header("Content-Type", "application/json"))
        .withBody(fullFetchJson1)
);
    response()
        .withStatusCode(200)
        .withHeader(new Header("Content-Type", "application/json"))
        .withBody(fullFetchJson2)
);

代码示例来源:origin: lv.ctco.cukes/cukes-http-mock

public void addResponseHeader(String headerName, String headerValue) {
  response.withHeader(headerName, headerValue);
}

代码示例来源:origin: org.mock-server/mockserver-core

private void setHeaderIfNotAlreadyExists(HttpResponse response, String name, String value) {
  if (response.getFirstHeader(name).isEmpty()) {
    response.withHeader(name, value);
  }
}

代码示例来源:origin: org.mock-server/mockserver-core

private void returnNotFound(ResponseWriter responseWriter, HttpRequest request) {
    HttpResponse response = notFoundResponse();
    if (request.getHeaders().containsEntry("x-forwarded-by", "MockServer")) {
      response.withHeader("x-forwarded-by", "MockServer");
    } else {
      httpStateHandler.log(new RequestLogEntry(request));
      mockServerLogger.info(EXPECTATION_NOT_MATCHED, request, "no expectation for:{}returning response:{}", request, notFoundResponse());
    }
    responseWriter.writeResponse(request, response, false);
  }
}

相关文章