diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/JettyFaultInjector.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/JettyFaultInjector.java new file mode 100644 index 0000000000..ab3e2822b8 --- /dev/null +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/JettyFaultInjector.java @@ -0,0 +1,93 @@ +/* + * Copyright 2016-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.wiremock; + +import java.io.IOException; +import java.nio.channels.ByteChannel; + +import javax.servlet.http.HttpServletResponse; + +import com.github.tomakehurst.wiremock.core.FaultInjector; +import com.google.common.base.Charsets; + +import org.eclipse.jetty.io.ChannelEndPoint; +import org.eclipse.jetty.server.HttpChannel; +import org.eclipse.jetty.server.Response; +import org.eclipse.jetty.util.BufferUtil; + +import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked; +import static com.github.tomakehurst.wiremock.jetty9.JettyUtils.unwrapResponse; + +/** + * @author Dave Syer + * + */ +public class JettyFaultInjector implements FaultInjector { + + private static final byte[] GARBAGE = "lskdu018973t09sylgasjkfg1][]'./.sdlv" + .getBytes(Charsets.UTF_8); + + private final Response response; + private final ByteChannel socket; + + public JettyFaultInjector(HttpServletResponse response) { + this.response = unwrapResponse(response); + this.socket = socket(); + } + + @Override + public void emptyResponseAndCloseConnection() { + try { + this.socket.close(); + } + catch (IOException e) { + throwUnchecked(e); + } + } + + @Override + public void malformedResponseChunk() { + try { + this.response.setStatus(200); + this.response.flushBuffer(); + this.socket.write(BufferUtil.toBuffer(GARBAGE)); + this.socket.close(); + } + catch (IOException e) { + throwUnchecked(e); + } + + } + + @Override + public void randomDataAndCloseConnection() { + try { + this.socket.write(BufferUtil.toBuffer(GARBAGE)); + this.socket.close(); + } + catch (IOException e) { + throwUnchecked(e); + } + } + + private ByteChannel socket() { + HttpChannel httpChannel = this.response.getHttpOutput().getHttpChannel(); + ChannelEndPoint ep = (ChannelEndPoint) httpChannel.getEndPoint(); + return ep.getChannel(); + } + +} diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/JettyFaultInjectorFactory.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/JettyFaultInjectorFactory.java new file mode 100644 index 0000000000..00ebaa6c3b --- /dev/null +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/JettyFaultInjectorFactory.java @@ -0,0 +1,37 @@ +/* + * Copyright 2016-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.contract.wiremock; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.github.tomakehurst.wiremock.core.FaultInjector; +import com.github.tomakehurst.wiremock.servlet.FaultInjectorFactory; + +/** + * @author Dave Syer + * + */ +public class JettyFaultInjectorFactory implements FaultInjectorFactory { + + @Override + public FaultInjector buildFaultInjector(HttpServletRequest httpServletRequest, + HttpServletResponse httpServletResponse) { + return new JettyFaultInjector(httpServletResponse); + } + +} diff --git a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/SpringBootHttpServerFactory.java b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/SpringBootHttpServerFactory.java index cb68825dc5..03d655b972 100644 --- a/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/SpringBootHttpServerFactory.java +++ b/spring-cloud-contract-wiremock/src/main/java/org/springframework/cloud/contract/wiremock/SpringBootHttpServerFactory.java @@ -29,7 +29,6 @@ import com.github.tomakehurst.wiremock.http.HttpServer; import com.github.tomakehurst.wiremock.http.HttpServerFactory; import com.github.tomakehurst.wiremock.http.RequestHandler; import com.github.tomakehurst.wiremock.http.StubRequestHandler; -import com.github.tomakehurst.wiremock.jetty9.JettyFaultInjectorFactory; import com.github.tomakehurst.wiremock.servlet.FaultInjectorFactory; import com.github.tomakehurst.wiremock.servlet.NoFaultInjectorFactory; import com.github.tomakehurst.wiremock.servlet.WireMockHandlerDispatchingServlet;