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

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

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

HttpRequest.getBody介绍

暂无

代码示例

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

private boolean bodyMatches(HttpRequest context, HttpRequest request) {
  boolean bodyMatches = true;
  String bodyAsString = request.getBody() != null ? new String(request.getBody().getRawBytes(), request.getBody().getCharset(StandardCharsets.UTF_8)) : "";
  if (!bodyAsString.isEmpty()) {
    if (bodyMatcher instanceof BinaryMatcher) {
      bodyMatches = matches(context, bodyMatcher, request.getBodyAsRawBytes());
    } else {
      if (bodyMatcher instanceof ExactStringMatcher ||
        bodyMatcher instanceof SubStringMatcher ||
        bodyMatcher instanceof RegexStringMatcher ||
        bodyMatcher instanceof XmlStringMatcher) {
        bodyMatches = matches(context, bodyMatcher, string(bodyAsString));
      } else {
        bodyMatches = matches(context, bodyMatcher, bodyAsString);
      }
    }
    if (!bodyMatches) {
      try {
        bodyMatches = bodyDTOMatcher.equals(objectMapper.readValue(bodyAsString, BodyDTO.class));
      } catch (Throwable e) {
        // ignore this exception as this exception would typically get thrown for "normal" HTTP requests (i.e. not clear or retrieve)
      }
    }
  }
  return bodyMatches;
}

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

private ByteBuf getBody(HttpRequest httpRequest) {
  ByteBuf content = Unpooled.buffer(0, 0);
  Body body = httpRequest.getBody();
  if (body != null) {
    Object bodyContents = body.getValue();
    Charset bodyCharset = body.getCharset(ContentTypeMapper.getCharsetFromContentTypeHeader(httpRequest.getFirstHeader(CONTENT_TYPE.toString())));
    if (bodyContents instanceof byte[]) {
      content = Unpooled.copiedBuffer((byte[]) bodyContents);
    } else if (bodyContents instanceof String) {
      content = Unpooled.copiedBuffer(((String) bodyContents).getBytes(bodyCharset));
    } else if (body.toString() != null) {
      content = Unpooled.copiedBuffer(body.toString().getBytes(bodyCharset));
    }
  }
  return content;
}

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

.withHeaders(request.getHeaderList());
if (request.getBody() instanceof BodyWithContentType) {
  httpResponse.withBody((BodyWithContentType) request.getBody());
} else {
  httpResponse.withBody(request.getBodyAsString());

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

if (request.getBody() != null) {
  if (request.getBody() instanceof JsonBody) {
    appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output);
    output.append(".withBody(");
    JsonBody jsonBody = (JsonBody) request.getBody();
    output.append("new JsonBody(\"").append(StringEscapeUtils.escapeJava(jsonBody.getValue())).append("\", JsonBodyMatchType.").append(jsonBody.getMatchType()).append(")");
    output.append(")");
  } else if (request.getBody() instanceof JsonPathBody) {
    appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output);
    output.append(".withBody(");
    JsonPathBody jsonPathBody = (JsonPathBody) request.getBody();
    output.append("new JsonPathBody(\"").append(StringEscapeUtils.escapeJava(jsonPathBody.getValue())).append("\")");
    output.append(")");
  } else if (request.getBody() instanceof JsonSchemaBody) {
    appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output);
    output.append(".withBody(");
    JsonSchemaBody jsonSchemaBody = (JsonSchemaBody) request.getBody();
    output.append("new JsonSchemaBody(\"").append(StringEscapeUtils.escapeJava(jsonSchemaBody.getValue())).append("\")");
    output.append(")");
  } else if (request.getBody() instanceof XmlBody) {
    appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output);
    output.append(".withBody(");
    XmlBody xmlBody = (XmlBody) request.getBody();
    output.append("new XmlBody(\"").append(StringEscapeUtils.escapeJava(xmlBody.getValue())).append("\")");
    output.append(")");
  } else if (request.getBody() instanceof XPathBody) {
    appendNewLineAndIndent((numberOfSpacesToIndent + 1) * INDENT_SIZE, output);
    output.append(".withBody(");
    XPathBody xPathBody = (XPathBody) request.getBody();

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

private boolean bodyMatches(HttpRequest context, HttpRequest request) {
  boolean bodyMatches = true;
  String bodyAsString = request.getBody() != null ? new String(request.getBody().getRawBytes(), request.getBody().getCharset(StandardCharsets.UTF_8)) : "";
  if (!bodyAsString.isEmpty()) {
    if (bodyMatcher instanceof BinaryMatcher) {
      bodyMatches = matches(context, bodyMatcher, request.getBodyAsRawBytes());
    } else {
      if (bodyMatcher instanceof ExactStringMatcher ||
        bodyMatcher instanceof SubStringMatcher ||
        bodyMatcher instanceof RegexStringMatcher ||
        bodyMatcher instanceof XmlStringMatcher) {
        bodyMatches = matches(context, bodyMatcher, string(bodyAsString));
      } else {
        bodyMatches = matches(context, bodyMatcher, bodyAsString);
      }
    }
    if (!bodyMatches) {
      try {
        bodyMatches = bodyDTOMatcher.equals(objectMapper.readValue(bodyAsString, BodyDTO.class));
      } catch (Throwable e) {
        // ignore this exception as this exception would typically get thrown for "normal" HTTP requests (i.e. not clear or retrieve)
      }
    }
  }
  return bodyMatches;
}

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

jgen.writeBooleanField("secure", httpRequest.isSecure());
if (httpRequest.getBody() != null && !Strings.isNullOrEmpty(String.valueOf(httpRequest.getBody().getValue()))) {
  jgen.writeObjectField("body", httpRequest.getBody());

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

if (httpRequest.getBody() != null
  && httpRequest.getBody().getContentType() != null) {
  request.headers().set(CONTENT_TYPE, httpRequest.getBody().getContentType());

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

public HttpRequestDTO(HttpRequest httpRequest, Boolean not) {
  super(not);
  if (httpRequest != null) {
    method = httpRequest.getMethod();
    path = httpRequest.getPath();
    headers = httpRequest.getHeaders();
    cookies = httpRequest.getCookies();
    queryStringParameters = httpRequest.getQueryStringParameters();
    body = BodyDTO.createDTO(httpRequest.getBody());
    keepAlive = httpRequest.isKeepAlive();
    secure = httpRequest.isSecure();
  }
}

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

public HttpRequestTemplateObject(HttpRequest httpRequest) {
  if (httpRequest != null) {
    method = httpRequest.getMethod().getValue();
    path = httpRequest.getPath().getValue();
    for (Header header : httpRequest.getHeaderList()) {
      headers.put(header.getName().getValue(), Lists.transform(header.getValues(), new Function<NottableString, String>() {
        public String apply(NottableString input) {
          return input.getValue();
        }
      }));
    }
    for (Cookie cookie : httpRequest.getCookieList()) {
      cookies.put(cookie.getName().getValue(), cookie.getValue().getValue());
    }
    for (Parameter parameter : httpRequest.getQueryStringParameterList()) {
      queryStringParameters.put(parameter.getName().getValue(), Lists.transform(parameter.getValues(), new Function<NottableString, String>() {
        public String apply(NottableString input) {
          return input.getValue();
        }
      }));
    }
    body = BodyDTO.createDTO(httpRequest.getBody());
    keepAlive = httpRequest.isKeepAlive();
    secure = httpRequest.isSecure();
  }
}

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

public HttpRequest update(HttpRequest replaceRequest) {
    if (!Strings.isNullOrEmpty(replaceRequest.getMethod().getValue())) {
      withMethod(replaceRequest.getMethod());
    }
    if (!Strings.isNullOrEmpty(replaceRequest.getPath().getValue())) {
      withPath(replaceRequest.getPath());
    }
    for (Header header : replaceRequest.getHeaderList()) {
      getHeaders().replaceEntry(header);
    }
    for (Cookie cookie : replaceRequest.getCookieList()) {
      withCookie(cookie);
    }
    for (Parameter parameter : replaceRequest.getQueryStringParameterList()) {
      getQueryStringParameters().replaceEntry(parameter);
    }
    if (replaceRequest.getBody() != null) {
      withBody(replaceRequest.getBody());
    }
    if (replaceRequest.isSecure() != null) {
      withSecure(replaceRequest.isSecure());
    }
    if (replaceRequest.isKeepAlive() != null) {
      withKeepAlive(replaceRequest.isKeepAlive());
    }
    return this;
  }
}

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

public HttpRequestMatcher(HttpRequest httpRequest, MockServerLogger mockServerLogger) {
  this.httpRequest = httpRequest;
  this.mockServerLogger = mockServerLogger;
  if (httpRequest != null) {
    withMethod(httpRequest.getMethod());
    withPath(httpRequest.getPath());
    withQueryStringParameters(httpRequest.getQueryStringParameters());
    withBody(httpRequest.getBody());
    withHeaders(httpRequest.getHeaders());
    withCookies(httpRequest.getCookies());
    withKeepAlive(httpRequest.isKeepAlive());
    withSsl(httpRequest.isSecure());
  }
}

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

public HttpRequestMatcher(Expectation expectation, MockServerLogger mockServerLogger) {
  this.expectation = expectation;
  this.httpRequest = expectation.getHttpRequest();
  this.mockServerLogger = mockServerLogger;
  if (httpRequest != null) {
    withMethod(httpRequest.getMethod());
    withPath(httpRequest.getPath());
    withQueryStringParameters(httpRequest.getQueryStringParameters());
    withBody(httpRequest.getBody());
    withHeaders(httpRequest.getHeaders());
    withCookies(httpRequest.getCookies());
    withKeepAlive(httpRequest.isKeepAlive());
    withSsl(httpRequest.isSecure());
  }
}

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

headersToIgnore)
);
assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(0).getBody().getValue(), "an_example_body_http");
assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(0).getPath().getValue(), calculatePath("callback"));
assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(1).getBody().getValue(), "an_example_body_https");
assertEquals(StaticTestExpectationResponseCallback.httpRequests.get(1).getPath().getValue(), calculatePath("callback"));

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

private ByteBuf getBody(HttpRequest httpRequest) {
  ByteBuf content = Unpooled.buffer(0, 0);
  Body body = httpRequest.getBody();
  if (body != null) {
    Object bodyContents = body.getValue();
    Charset bodyCharset = body.getCharset(ContentTypeMapper.getCharsetFromContentTypeHeader(httpRequest.getFirstHeader(CONTENT_TYPE.toString())));
    if (bodyContents instanceof byte[]) {
      content = Unpooled.copiedBuffer((byte[]) bodyContents);
    } else if (bodyContents instanceof String) {
      content = Unpooled.copiedBuffer(((String) bodyContents).getBytes(bodyCharset));
    } else if (body.toString() != null) {
      content = Unpooled.copiedBuffer(body.toString().getBytes(bodyCharset));
    }
  }
  return content;
}

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

jgen.writeBooleanField("secure", httpRequest.isSecure());
if (httpRequest.getBody() != null && !Strings.isNullOrEmpty(String.valueOf(httpRequest.getBody().getValue()))) {
  jgen.writeObjectField("body", httpRequest.getBody());

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

public HttpRequestDTO(HttpRequest httpRequest, Boolean not) {
  super(not);
  if (httpRequest != null) {
    method = httpRequest.getMethod();
    path = httpRequest.getPath();
    headers = httpRequest.getHeaders();
    cookies = httpRequest.getCookies();
    queryStringParameters = httpRequest.getQueryStringParameters();
    body = BodyDTO.createDTO(httpRequest.getBody());
    keepAlive = httpRequest.isKeepAlive();
    secure = httpRequest.isSecure();
  }
}

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

public HttpRequestTemplateObject(HttpRequest httpRequest) {
  if (httpRequest != null) {
    method = httpRequest.getMethod().getValue();
    path = httpRequest.getPath().getValue();
    for (Header header : httpRequest.getHeaderList()) {
      headers.put(header.getName().getValue(), Lists.transform(header.getValues(), new Function<NottableString, String>() {
        public String apply(NottableString input) {
          return input.getValue();
        }
      }));
    }
    for (Cookie cookie : httpRequest.getCookieList()) {
      cookies.put(cookie.getName().getValue(), cookie.getValue().getValue());
    }
    for (Parameter parameter : httpRequest.getQueryStringParameterList()) {
      queryStringParameters.put(parameter.getName().getValue(), Lists.transform(parameter.getValues(), new Function<NottableString, String>() {
        public String apply(NottableString input) {
          return input.getValue();
        }
      }));
    }
    body = BodyDTO.createDTO(httpRequest.getBody());
    keepAlive = httpRequest.isKeepAlive();
    secure = httpRequest.isSecure();
  }
}

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

public HttpRequest update(HttpRequest replaceRequest) {
    if (!Strings.isNullOrEmpty(replaceRequest.getMethod().getValue())) {
      withMethod(replaceRequest.getMethod());
    }
    if (!Strings.isNullOrEmpty(replaceRequest.getPath().getValue())) {
      withPath(replaceRequest.getPath());
    }
    for (Header header : replaceRequest.getHeaderList()) {
      getHeaders().replaceEntry(header);
    }
    for (Cookie cookie : replaceRequest.getCookieList()) {
      withCookie(cookie);
    }
    for (Parameter parameter : replaceRequest.getQueryStringParameterList()) {
      getQueryStringParameters().replaceEntry(parameter);
    }
    if (replaceRequest.getBody() != null) {
      withBody(replaceRequest.getBody());
    }
    if (replaceRequest.isSecure() != null) {
      withSecure(replaceRequest.isSecure());
    }
    if (replaceRequest.isKeepAlive() != null) {
      withKeepAlive(replaceRequest.isKeepAlive());
    }
    return this;
  }
}

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

public HttpRequestMatcher(HttpRequest httpRequest, MockServerLogger mockServerLogger) {
  this.httpRequest = httpRequest;
  this.mockServerLogger = mockServerLogger;
  if (httpRequest != null) {
    withMethod(httpRequest.getMethod());
    withPath(httpRequest.getPath());
    withQueryStringParameters(httpRequest.getQueryStringParameters());
    withBody(httpRequest.getBody());
    withHeaders(httpRequest.getHeaders());
    withCookies(httpRequest.getCookies());
    withKeepAlive(httpRequest.isKeepAlive());
    withSsl(httpRequest.isSecure());
  }
}

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

public HttpRequestMatcher(Expectation expectation, MockServerLogger mockServerLogger) {
  this.expectation = expectation;
  this.httpRequest = expectation.getHttpRequest();
  this.mockServerLogger = mockServerLogger;
  if (httpRequest != null) {
    withMethod(httpRequest.getMethod());
    withPath(httpRequest.getPath());
    withQueryStringParameters(httpRequest.getQueryStringParameters());
    withBody(httpRequest.getBody());
    withHeaders(httpRequest.getHeaders());
    withCookies(httpRequest.getCookies());
    withKeepAlive(httpRequest.isKeepAlive());
    withSsl(httpRequest.isSecure());
  }
}

相关文章