/** * 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) * * @param name the header name * @param values the header values which can be a varags of strings or regular expressions */ public HttpRequest withHeader(String name, String... values) { this.headers.withEntry(header(name, values)); return this; }
/** * 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 * * @param name the header name as a NottableString * @param values the header values which can be a varags of NottableStrings */ public HttpResponse withHeader(NottableString name, NottableString... values) { this.headers.withEntry(header(name, values)); return this; }
/** * Adds one header to match on or to not match on using the NottableString, each NottableString can either be a positive matching value, * such as string("match"), or a value to not match on, such as not("do not match"), the string values passed to the NottableString * can also be a plain string or a regex (for more details of the supported regex syntax * see http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html) * * @param name the header name as a NottableString * @param values the header values which can be a varags of NottableStrings */ public HttpRequest withHeader(NottableString name, NottableString... values) { this.headers.withEntry(header(name, values)); return this; }
@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); }
@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); }
protected void addConnectionHeader(HttpRequest request, HttpResponse response) { ConnectionOptions connectionOptions = response.getConnectionOptions(); if (connectionOptions != null && connectionOptions.getKeepAliveOverride() != null) { if (connectionOptions.getKeepAliveOverride()) { response.replaceHeader(header(CONNECTION.toString(), KEEP_ALIVE.toString())); } else { response.replaceHeader(header(CONNECTION.toString(), CLOSE.toString())); } } else if (connectionOptions == null || isFalseOrNull(connectionOptions.getSuppressConnectionHeader())) { if (request.isKeepAlive() != null && request.isKeepAlive() && (connectionOptions == null || isFalseOrNull(connectionOptions.getCloseSocket()))) { response.replaceHeader(header(CONNECTION.toString(), KEEP_ALIVE.toString())); } else { response.replaceHeader(header(CONNECTION.toString(), CLOSE.toString())); } } } }
@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())); }
@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())); }
@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))); }
@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())); }
@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))); }
@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())); }
@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))); }
@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))); }
@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))); }
@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))); }
@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))); }
@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))); }
@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))); }
@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))); }