|
| 1 | +package org.springframework.http.client; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | + |
| 5 | +import okhttp3.mockwebserver.Dispatcher; |
| 6 | +import okhttp3.mockwebserver.MockResponse; |
| 7 | +import okhttp3.mockwebserver.MockWebServer; |
| 8 | +import okhttp3.mockwebserver.RecordedRequest; |
| 9 | +import org.hamcrest.Matchers; |
| 10 | +import org.junit.After; |
| 11 | +import org.junit.Before; |
| 12 | + |
| 13 | +import org.springframework.http.MediaType; |
| 14 | +import org.springframework.util.StringUtils; |
| 15 | + |
| 16 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 17 | + |
| 18 | +/** |
| 19 | + * @author Brian Clozel |
| 20 | + */ |
| 21 | +public class AbstractMockWebServerTestCase { |
| 22 | + |
| 23 | + private MockWebServer server; |
| 24 | + |
| 25 | + protected int port; |
| 26 | + |
| 27 | + protected String baseUrl; |
| 28 | + |
| 29 | + protected static final MediaType textContentType = |
| 30 | + new MediaType("text", "plain", Collections.singletonMap("charset", "UTF-8")); |
| 31 | + |
| 32 | + @Before |
| 33 | + public void setUp() throws Exception { |
| 34 | + this.server = new MockWebServer(); |
| 35 | + this.server.setDispatcher(new TestDispatcher()); |
| 36 | + this.server.start(); |
| 37 | + this.port = this.server.getPort(); |
| 38 | + this.baseUrl = "http://localhost:" + this.port; |
| 39 | + } |
| 40 | + |
| 41 | + @After |
| 42 | + public void tearDown() throws Exception { |
| 43 | + this.server.shutdown(); |
| 44 | + } |
| 45 | + |
| 46 | + protected class TestDispatcher extends Dispatcher { |
| 47 | + @Override |
| 48 | + public MockResponse dispatch(RecordedRequest request) throws InterruptedException { |
| 49 | + try { |
| 50 | + if (request.getPath().equals("/echo")) { |
| 51 | + MockResponse response = new MockResponse() |
| 52 | + .setHeaders(request.getHeaders()) |
| 53 | + .setHeader("Content-Length", request.getBody().size()) |
| 54 | + .setResponseCode(200) |
| 55 | + .setBody(request.getBody()); |
| 56 | + request.getBody().flush(); |
| 57 | + return response; |
| 58 | + } |
| 59 | + else if(request.getPath().equals("/status/ok")) { |
| 60 | + return new MockResponse(); |
| 61 | + } |
| 62 | + else if(request.getPath().equals("/status/notfound")) { |
| 63 | + return new MockResponse().setResponseCode(404); |
| 64 | + } |
| 65 | + else if(request.getPath().startsWith("/params")) { |
| 66 | + assertThat(request.getPath(), Matchers.containsString("param1=value")); |
| 67 | + assertThat(request.getPath(), Matchers.containsString("param2=value1¶m2=value2")); |
| 68 | + return new MockResponse(); |
| 69 | + } |
| 70 | + else if(request.getPath().equals("/methods/post")) { |
| 71 | + assertThat(request.getMethod(), Matchers.is("POST")); |
| 72 | + String transferEncoding = request.getHeader("Transfer-Encoding"); |
| 73 | + if(StringUtils.hasLength(transferEncoding)) { |
| 74 | + assertThat(transferEncoding, Matchers.is("chunked")); |
| 75 | + } |
| 76 | + else { |
| 77 | + long contentLength = Long.parseLong(request.getHeader("Content-Length")); |
| 78 | + assertThat("Invalid content-length", |
| 79 | + request.getBody().size(), Matchers.is(contentLength)); |
| 80 | + } |
| 81 | + return new MockResponse().setResponseCode(200); |
| 82 | + } |
| 83 | + else if(request.getPath().startsWith("/methods/")) { |
| 84 | + String expectedMethod = request.getPath().replace("/methods/","").toUpperCase(); |
| 85 | + assertThat(request.getMethod(), Matchers.is(expectedMethod)); |
| 86 | + return new MockResponse(); |
| 87 | + } |
| 88 | + return new MockResponse().setResponseCode(404); |
| 89 | + } |
| 90 | + catch (Throwable exc) { |
| 91 | + return new MockResponse().setResponseCode(500).setBody(exc.toString()); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments