本文整理了Java中org.mockserver.model.HttpRequest.getFirstHeader()
方法的一些代码示例,展示了HttpRequest.getFirstHeader()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。HttpRequest.getFirstHeader()
方法的具体详情如下:
包路径:org.mockserver.model.HttpRequest
类名称:HttpRequest
方法名:getFirstHeader
暂无
代码示例来源:origin: jamesdbloom/mockserver
private String getHostAndPort(HttpRequest request, InetSocketAddress remoteAddress) {
String host = request.getFirstHeader("Host");
if (Strings.isNullOrEmpty(host)) {
host = remoteAddress.getHostName() + ":" + remoteAddress.getPort();
}
return host;
}
代码示例来源:origin: jamesdbloom/mockserver
public InetSocketAddress socketAddressFromHostHeader() {
if (!Strings.isNullOrEmpty(getFirstHeader(HOST.toString()))) {
boolean isSsl = isSecure() != null && isSecure();
String[] hostHeaderParts = getFirstHeader(HOST.toString()).split(":");
return new InetSocketAddress(hostHeaderParts[0], hostHeaderParts.length > 1 ? Integer.parseInt(hostHeaderParts[1]) : isSsl ? 443 : 80);
} else {
throw new IllegalArgumentException("Host header must be provided to determine remote socket address, the request does not include the \"Host\" header:" + NEW_LINE + this);
}
}
代码示例来源:origin: jamesdbloom/mockserver
@Override
public void run() {
final String hostHeader = request.getFirstHeader(HOST.toString());
KeyAndCertificateFactory.addSubjectAlternativeName(hostHeader);
}
});
代码示例来源: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
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
public String toCurl(HttpRequest request, @Nullable InetSocketAddress remoteAddress) {
StringBuilder curlString = new StringBuilder();
if (request != null) {
if (!Strings.isNullOrEmpty(request.getFirstHeader(HOST.toString())) || remoteAddress != null) {
boolean isSsl = request.isSecure() != null && request.isSecure();
curlString.append("curl -v");
代码示例来源:origin: jamesdbloom/mockserver
if (!Strings.isNullOrEmpty(httpRequest.getFirstHeader(HOST.toString()))) {
request.headers().add(HOST, httpRequest.getFirstHeader(HOST.toString()));
代码示例来源:origin: jamesdbloom/mockserver
void receivedTextWebSocketFrame(TextWebSocketFrame textWebSocketFrame) {
try {
availableWebSocketCallbackRegistrations.tryAcquire();
Object deserializedMessage = webSocketMessageSerializer.deserialize(textWebSocketFrame.text());
if (deserializedMessage instanceof HttpRequest) {
HttpRequest httpRequest = (HttpRequest) deserializedMessage;
String webSocketCorrelationId = httpRequest.getFirstHeader(WEB_SOCKET_CORRELATION_ID_HEADER_NAME);
if (expectationCallback != null) {
try {
T result = expectationCallback.handle(httpRequest);
result.withHeader(WEB_SOCKET_CORRELATION_ID_HEADER_NAME, webSocketCorrelationId);
channel.writeAndFlush(new TextWebSocketFrame(webSocketMessageSerializer.serialize(result)));
} catch (Throwable throwable) {
mockServerLogger.error("Exception thrown while handling callback", throwable);
channel.writeAndFlush(new TextWebSocketFrame(webSocketMessageSerializer.serialize(
new WebSocketErrorDTO()
.setMessage(throwable.getMessage())
.setWebSocketCorrelationId(webSocketCorrelationId)
)));
}
}
} else if (!(deserializedMessage instanceof WebSocketClientIdDTO)) {
throw new WebSocketException("Unsupported web socket message " + deserializedMessage);
}
} catch (Exception e) {
throw new WebSocketException("Exception while receiving web socket message", e);
} finally {
availableWebSocketCallbackRegistrations.release();
}
}
代码示例来源:origin: jamesdbloom/mockserver
public void add(Expectation... expectations) {
for (Expectation expectation : expectations) {
if (expectation.getHttpRequest() != null) {
final String hostHeader = expectation.getHttpRequest().getFirstHeader(HOST.toString());
if (!Strings.isNullOrEmpty(hostHeader)) {
scheduler.submit(new Runnable() {
@Override
public void run() {
addSubjectAlternativeName(hostHeader);
}
});
}
}
mockServerMatcher.add(expectation);
mockServerLogger.info(CREATED_EXPECTATION, expectation.getHttpRequest(), "creating expectation:{}", expectation.clone());
}
}
代码示例来源:origin: jamesdbloom/mockserver
final String hostHeader = request.getFirstHeader(HOST.toString());
scheduler.submit(new Runnable() {
@Override
代码示例来源:origin: jamesdbloom/mockserver
final String hostHeader = request.getFirstHeader(HOST.toString());
scheduler.submit(new Runnable() {
@Override
代码示例来源:origin: jamesdbloom/mockserver
public void addCORSHeaders(HttpRequest request, HttpResponse response) {
String origin = request.getFirstHeader(HttpHeaderNames.ORIGIN.toString());
if (NULL_ORIGIN.equals(origin)) {
setHeaderIfNotAlreadyExists(response, HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.toString(), NULL_ORIGIN);
} else if (!origin.isEmpty() && request.getFirstHeader(HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS.toString()).equals("true")) {
setHeaderIfNotAlreadyExists(response, HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.toString(), origin);
setHeaderIfNotAlreadyExists(response, HttpHeaderNames.ACCESS_CONTROL_ALLOW_CREDENTIALS.toString(), "true");
} else {
setHeaderIfNotAlreadyExists(response, HttpHeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN.toString(), ANY_ORIGIN);
}
setHeaderIfNotAlreadyExists(response, HttpHeaderNames.ACCESS_CONTROL_ALLOW_METHODS.toString(), "CONNECT, DELETE, GET, HEAD, OPTIONS, POST, PUT, PATCH, TRACE");
String headers = "Allow, Content-Encoding, Content-Length, Content-Type, ETag, Expires, Last-Modified, Location, Server, Vary, Authorization";
setHeaderIfNotAlreadyExists(response, HttpHeaderNames.ACCESS_CONTROL_ALLOW_HEADERS.toString(), headers);
setHeaderIfNotAlreadyExists(response, HttpHeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS.toString(), headers);
setHeaderIfNotAlreadyExists(response, HttpHeaderNames.ACCESS_CONTROL_MAX_AGE.toString(), "300");
if (!enableCORSForAPIHasBeenSetExplicitly()) {
setHeaderIfNotAlreadyExists(response, "x-cors", "MockServer CORS support enabled by default, to disable ConfigurationProperties.enableCORSForAPI(false) or -Dmockserver.enableCORSForAPI=false");
}
}
代码示例来源:origin: jamesdbloom/mockserver
if ((!username.isEmpty() && !password.isEmpty())
&& (!request.containsHeader(PROXY_AUTHORIZATION.toString())
|| !request.getFirstHeader(PROXY_AUTHORIZATION.toString()).toLowerCase(US).startsWith("basic ")
|| !request.getFirstHeader(PROXY_AUTHORIZATION.toString()).substring(6).equals(
new Base64Converter().bytesToBase64String((username + ":" + password).getBytes(UTF_8))))) {
ctx.writeAndFlush(response()
代码示例来源:origin: jamesdbloom/mockserver
} else if (proxyThisRequest || (!StringUtils.isEmpty(request.getFirstHeader(HOST.toString())) && !localAddresses.contains(request.getFirstHeader(HOST.toString())))) {
代码示例来源:origin: jamesdbloom/mockserver
final String firstHeader = httpRequest.getFirstHeader(WEB_SOCKET_CORRELATION_ID_HEADER_NAME);
WebSocketRequestCallback webSocketRequestCallback = forwardCallbackRegistry.get(firstHeader);
if (webSocketRequestCallback != null) {
代码示例来源:origin: org.mock-server/mockserver-core
private String getHostAndPort(HttpRequest request, InetSocketAddress remoteAddress) {
String host = request.getFirstHeader("Host");
if (Strings.isNullOrEmpty(host)) {
host = remoteAddress.getHostName() + ":" + remoteAddress.getPort();
}
return host;
}
代码示例来源:origin: org.mock-server/mockserver-core
public InetSocketAddress socketAddressFromHostHeader() {
if (!Strings.isNullOrEmpty(getFirstHeader(HOST.toString()))) {
boolean isSsl = isSecure() != null && isSecure();
String[] hostHeaderParts = getFirstHeader(HOST.toString()).split(":");
return new InetSocketAddress(hostHeaderParts[0], hostHeaderParts.length > 1 ? Integer.parseInt(hostHeaderParts[1]) : isSsl ? 443 : 80);
} else {
throw new IllegalArgumentException("Host header must be provided to determine remote socket address, the request does not include the \"Host\" header:" + NEW_LINE + this);
}
}
代码示例来源:origin: org.mock-server/mockserver-netty
@Override
public void run() {
final String hostHeader = request.getFirstHeader(HOST.toString());
KeyAndCertificateFactory.addSubjectAlternativeName(hostHeader);
}
});
代码示例来源: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
public void add(Expectation... expectations) {
for (Expectation expectation : expectations) {
final String hostHeader = expectation.getHttpRequest().getFirstHeader(HOST.toString());
if (!Strings.isNullOrEmpty(hostHeader)) {
scheduler.submit(new Runnable() {
@Override
public void run() {
addSubjectAlternativeName(hostHeader);
}
});
}
mockServerMatcher.add(expectation);
mockServerLogger.info(CREATED_EXPECTATION, expectation.getHttpRequest(), "creating expectation:{}", expectation.clone());
}
}
内容来源于网络,如有侵权,请联系作者删除!