|
|
|
@ -1,3 +1,19 @@ |
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
* Copyright 2002-2016 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.web.client; |
|
|
|
package org.springframework.web.client; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.EOFException; |
|
|
|
import java.io.EOFException; |
|
|
|
@ -16,16 +32,16 @@ import org.junit.Before; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
import static org.junit.Assert.*; |
|
|
|
import static org.junit.Assert.assertNotNull; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertThat; |
|
|
|
|
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author Brian Clozel |
|
|
|
* @author Brian Clozel |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class AbstractMockWebServerTestCase { |
|
|
|
public class AbstractMockWebServerTestCase { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected static final MediaType textContentType = |
|
|
|
|
|
|
|
new MediaType("text", "plain", Collections.singletonMap("charset", "UTF-8")); |
|
|
|
|
|
|
|
|
|
|
|
protected static final String helloWorld = "H\u00e9llo W\u00f6rld"; |
|
|
|
protected static final String helloWorld = "H\u00e9llo W\u00f6rld"; |
|
|
|
|
|
|
|
|
|
|
|
private MockWebServer server; |
|
|
|
private MockWebServer server; |
|
|
|
@ -34,8 +50,6 @@ public class AbstractMockWebServerTestCase { |
|
|
|
|
|
|
|
|
|
|
|
protected String baseUrl; |
|
|
|
protected String baseUrl; |
|
|
|
|
|
|
|
|
|
|
|
protected static final MediaType textContentType = |
|
|
|
|
|
|
|
new MediaType("text", "plain", Collections.singletonMap("charset", "UTF-8")); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Before |
|
|
|
@Before |
|
|
|
public void setUp() throws Exception { |
|
|
|
public void setUp() throws Exception { |
|
|
|
@ -51,68 +65,9 @@ public class AbstractMockWebServerTestCase { |
|
|
|
this.server.shutdown(); |
|
|
|
this.server.shutdown(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected class TestDispatcher extends Dispatcher { |
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public MockResponse dispatch(RecordedRequest request) throws InterruptedException { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
byte[] helloWorldBytes = helloWorld.getBytes(StandardCharsets.UTF_8); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (request.getPath().equals("/get")) { |
|
|
|
|
|
|
|
return getRequest(request, helloWorldBytes, textContentType.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/get/nothing")) { |
|
|
|
|
|
|
|
return getRequest(request, new byte[0], textContentType.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/get/nocontenttype")) { |
|
|
|
|
|
|
|
return getRequest(request, helloWorldBytes, null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/post")) { |
|
|
|
|
|
|
|
return postRequest(request, helloWorld, "/post/1", textContentType.toString(), helloWorldBytes); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/jsonpost")) { |
|
|
|
|
|
|
|
return jsonPostRequest(request, "/jsonpost/1", "application/json; charset=utf-8"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/status/nocontent")) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(204); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/status/notmodified")) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(304); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/status/notfound")) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(404); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/status/server")) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(500); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().contains("/uri/")) { |
|
|
|
|
|
|
|
return new MockResponse().setBody(request.getPath()).setHeader("Content-Type", "text/plain"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/multipart")) { |
|
|
|
|
|
|
|
return multipartRequest(request); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/form")) { |
|
|
|
|
|
|
|
return formRequest(request); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/delete")) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(200); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/patch")) { |
|
|
|
|
|
|
|
return patchRequest(request, helloWorld, textContentType.toString(), helloWorldBytes); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/put")) { |
|
|
|
|
|
|
|
return putRequest(request, helloWorld); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(404); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Throwable exc) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(500).setBody(exc.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private MockResponse getRequest(RecordedRequest request, byte[] body, String contentType) { |
|
|
|
private MockResponse getRequest(RecordedRequest request, byte[] body, String contentType) { |
|
|
|
if(request.getMethod().equals("OPTIONS")) { |
|
|
|
if (request.getMethod().equals("OPTIONS")) { |
|
|
|
return new MockResponse().setResponseCode(200).setHeader("Allow", "GET, OPTIONS, HEAD, TRACE"); |
|
|
|
return new MockResponse().setResponseCode(200).setHeader("Allow", "GET, OPTIONS, HEAD, TRACE"); |
|
|
|
} |
|
|
|
} |
|
|
|
Buffer buf = new Buffer(); |
|
|
|
Buffer buf = new Buffer(); |
|
|
|
@ -135,7 +90,7 @@ public class AbstractMockWebServerTestCase { |
|
|
|
String requestContentType = request.getHeader("Content-Type"); |
|
|
|
String requestContentType = request.getHeader("Content-Type"); |
|
|
|
assertNotNull("No content-type", requestContentType); |
|
|
|
assertNotNull("No content-type", requestContentType); |
|
|
|
Charset charset = StandardCharsets.ISO_8859_1; |
|
|
|
Charset charset = StandardCharsets.ISO_8859_1; |
|
|
|
if(requestContentType.indexOf("charset=") > -1) { |
|
|
|
if (requestContentType.contains("charset=")) { |
|
|
|
String charsetName = requestContentType.split("charset=")[1]; |
|
|
|
String charsetName = requestContentType.split("charset=")[1]; |
|
|
|
charset = Charset.forName(charsetName); |
|
|
|
charset = Charset.forName(charsetName); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -151,10 +106,11 @@ public class AbstractMockWebServerTestCase { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private MockResponse jsonPostRequest(RecordedRequest request, String location, String contentType) { |
|
|
|
private MockResponse jsonPostRequest(RecordedRequest request, String location, String contentType) { |
|
|
|
|
|
|
|
if (request.getBodySize() > 0) { |
|
|
|
assertTrue("Invalid request content-length", |
|
|
|
assertTrue("Invalid request content-length", |
|
|
|
Integer.parseInt(request.getHeader("Content-Length")) > 0); |
|
|
|
Integer.parseInt(request.getHeader("Content-Length")) > 0); |
|
|
|
assertNotNull("No content-type", request.getHeader("Content-Type")); |
|
|
|
assertNotNull("No content-type", request.getHeader("Content-Type")); |
|
|
|
|
|
|
|
} |
|
|
|
return new MockResponse() |
|
|
|
return new MockResponse() |
|
|
|
.setHeader("Location", baseUrl + location) |
|
|
|
.setHeader("Location", baseUrl + location) |
|
|
|
.setHeader("Content-Type", contentType) |
|
|
|
.setHeader("Content-Type", contentType) |
|
|
|
@ -174,8 +130,8 @@ public class AbstractMockWebServerTestCase { |
|
|
|
assertPart(body, "form-data", boundary, "name 2", "text/plain", "value 2+2"); |
|
|
|
assertPart(body, "form-data", boundary, "name 2", "text/plain", "value 2+2"); |
|
|
|
assertFilePart(body, "form-data", boundary, "logo", "logo.jpg", "image/jpeg"); |
|
|
|
assertFilePart(body, "form-data", boundary, "logo", "logo.jpg", "image/jpeg"); |
|
|
|
} |
|
|
|
} |
|
|
|
catch (EOFException e) { |
|
|
|
catch (EOFException ex) { |
|
|
|
throw new RuntimeException(e); |
|
|
|
throw new IllegalStateException(ex); |
|
|
|
} |
|
|
|
} |
|
|
|
return new MockResponse().setResponseCode(200); |
|
|
|
return new MockResponse().setResponseCode(200); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -218,13 +174,14 @@ public class AbstractMockWebServerTestCase { |
|
|
|
|
|
|
|
|
|
|
|
private MockResponse patchRequest(RecordedRequest request, String expectedRequestContent, |
|
|
|
private MockResponse patchRequest(RecordedRequest request, String expectedRequestContent, |
|
|
|
String contentType, byte[] responseBody) { |
|
|
|
String contentType, byte[] responseBody) { |
|
|
|
|
|
|
|
|
|
|
|
assertEquals("PATCH", request.getMethod()); |
|
|
|
assertEquals("PATCH", request.getMethod()); |
|
|
|
assertTrue("Invalid request content-length", |
|
|
|
assertTrue("Invalid request content-length", |
|
|
|
Integer.parseInt(request.getHeader("Content-Length")) > 0); |
|
|
|
Integer.parseInt(request.getHeader("Content-Length")) > 0); |
|
|
|
String requestContentType = request.getHeader("Content-Type"); |
|
|
|
String requestContentType = request.getHeader("Content-Type"); |
|
|
|
assertNotNull("No content-type", requestContentType); |
|
|
|
assertNotNull("No content-type", requestContentType); |
|
|
|
Charset charset = StandardCharsets.ISO_8859_1; |
|
|
|
Charset charset = StandardCharsets.ISO_8859_1; |
|
|
|
if(requestContentType.indexOf("charset=") > -1) { |
|
|
|
if (requestContentType.contains("charset=")) { |
|
|
|
String charsetName = requestContentType.split("charset=")[1]; |
|
|
|
String charsetName = requestContentType.split("charset=")[1]; |
|
|
|
charset = Charset.forName(charsetName); |
|
|
|
charset = Charset.forName(charsetName); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -243,7 +200,7 @@ public class AbstractMockWebServerTestCase { |
|
|
|
String requestContentType = request.getHeader("Content-Type"); |
|
|
|
String requestContentType = request.getHeader("Content-Type"); |
|
|
|
assertNotNull("No content-type", requestContentType); |
|
|
|
assertNotNull("No content-type", requestContentType); |
|
|
|
Charset charset = StandardCharsets.ISO_8859_1; |
|
|
|
Charset charset = StandardCharsets.ISO_8859_1; |
|
|
|
if(requestContentType.indexOf("charset=") > -1) { |
|
|
|
if (requestContentType.contains("charset=")) { |
|
|
|
String charsetName = requestContentType.split("charset=")[1]; |
|
|
|
String charsetName = requestContentType.split("charset=")[1]; |
|
|
|
charset = Charset.forName(charsetName); |
|
|
|
charset = Charset.forName(charsetName); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -251,4 +208,65 @@ public class AbstractMockWebServerTestCase { |
|
|
|
return new MockResponse().setResponseCode(202); |
|
|
|
return new MockResponse().setResponseCode(202); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected class TestDispatcher extends Dispatcher { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public MockResponse dispatch(RecordedRequest request) throws InterruptedException { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
byte[] helloWorldBytes = helloWorld.getBytes(StandardCharsets.UTF_8); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (request.getPath().equals("/get")) { |
|
|
|
|
|
|
|
return getRequest(request, helloWorldBytes, textContentType.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/get/nothing")) { |
|
|
|
|
|
|
|
return getRequest(request, new byte[0], textContentType.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/get/nocontenttype")) { |
|
|
|
|
|
|
|
return getRequest(request, helloWorldBytes, null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/post")) { |
|
|
|
|
|
|
|
return postRequest(request, helloWorld, "/post/1", textContentType.toString(), helloWorldBytes); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/jsonpost")) { |
|
|
|
|
|
|
|
return jsonPostRequest(request, "/jsonpost/1", "application/json; charset=utf-8"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/status/nocontent")) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(204); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/status/notmodified")) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(304); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/status/notfound")) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(404); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/status/server")) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(500); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().contains("/uri/")) { |
|
|
|
|
|
|
|
return new MockResponse().setBody(request.getPath()).setHeader("Content-Type", "text/plain"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/multipart")) { |
|
|
|
|
|
|
|
return multipartRequest(request); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/form")) { |
|
|
|
|
|
|
|
return formRequest(request); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/delete")) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(200); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/patch")) { |
|
|
|
|
|
|
|
return patchRequest(request, helloWorld, textContentType.toString(), helloWorldBytes); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (request.getPath().equals("/put")) { |
|
|
|
|
|
|
|
return putRequest(request, helloWorld); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(404); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Throwable ex) { |
|
|
|
|
|
|
|
return new MockResponse().setResponseCode(500).setBody(ex.toString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|