下面列出了org.springframework.http.HttpMethod#PATCH 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void wrapPutPatchAndDeleteOnly() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
MockHttpServletRequest request = new MockHttpServletRequest(method.name(), "/");
request.setContent("foo=bar".getBytes("ISO-8859-1"));
request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
this.filterChain = new MockFilterChain();
this.filter.doFilter(request, this.response, this.filterChain);
if (method == HttpMethod.PUT || method == HttpMethod.PATCH || method == HttpMethod.DELETE) {
assertNotSame(request, this.filterChain.getRequest());
}
else {
assertSame(request, this.filterChain.getRequest());
}
}
}
protected void assertHttpMethod(String path, HttpMethod method) throws Exception {
ClientHttpResponse response = null;
try {
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/methods/" + path), method);
if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) {
// requires a body
request.getBody().write(32);
}
Future<ClientHttpResponse> futureResponse = request.executeAsync();
response = futureResponse.get();
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
assertEquals("Invalid method", path.toUpperCase(Locale.ENGLISH), request.getMethod().name());
}
finally {
if (response != null) {
response.close();
}
}
}
protected void assertHttpMethod(String path, HttpMethod method) throws Exception {
ClientHttpResponse response = null;
try {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/methods/" + path), method);
if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) {
// requires a body
try {
request.getBody().write(32);
}
catch (UnsupportedOperationException ex) {
// probably a streaming request - let's simply ignore it
}
}
response = request.execute();
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
assertEquals("Invalid method", path.toUpperCase(Locale.ENGLISH), request.getMethod().name());
}
finally {
if (response != null) {
response.close();
}
}
}
@Test
public void wrapPutPatchAndDeleteOnly() throws Exception {
for (HttpMethod method : HttpMethod.values()) {
MockHttpServletRequest request = new MockHttpServletRequest(method.name(), "/");
request.setContent("foo=bar".getBytes("ISO-8859-1"));
request.setContentType("application/x-www-form-urlencoded; charset=ISO-8859-1");
this.filterChain = new MockFilterChain();
this.filter.doFilter(request, this.response, this.filterChain);
if (method == HttpMethod.PUT || method == HttpMethod.PATCH || method == HttpMethod.DELETE) {
assertNotSame(request, this.filterChain.getRequest());
}
else {
assertSame(request, this.filterChain.getRequest());
}
}
}
protected void assertHttpMethod(String path, HttpMethod method) throws Exception {
ClientHttpResponse response = null;
try {
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/methods/" + path), method);
if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) {
// requires a body
request.getBody().write(32);
}
Future<ClientHttpResponse> futureResponse = request.executeAsync();
response = futureResponse.get();
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
assertEquals("Invalid method", path.toUpperCase(Locale.ENGLISH), request.getMethod().name());
}
finally {
if (response != null) {
response.close();
}
}
}
protected void assertHttpMethod(String path, HttpMethod method) throws Exception {
ClientHttpResponse response = null;
try {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/methods/" + path), method);
if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) {
// requires a body
try {
request.getBody().write(32);
}
catch (UnsupportedOperationException ex) {
// probably a streaming request - let's simply ignore it
}
}
response = request.execute();
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
assertEquals("Invalid method", path.toUpperCase(Locale.ENGLISH), request.getMethod().name());
}
finally {
if (response != null) {
response.close();
}
}
}
protected void assertHttpMethod(String path, HttpMethod method) throws Exception {
ClientHttpResponse response = null;
try {
AsyncClientHttpRequest request = this.factory.createAsyncRequest(new URI(baseUrl + "/methods/" + path), method);
if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) {
// requires a body
request.getBody().write(32);
}
Future<ClientHttpResponse> futureResponse = request.executeAsync();
response = futureResponse.get();
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
assertEquals("Invalid method", path.toUpperCase(Locale.ENGLISH), request.getMethod().name());
}
finally {
if (response != null) {
response.close();
}
}
}
protected void assertHttpMethod(String path, HttpMethod method) throws Exception {
ClientHttpResponse response = null;
try {
ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/methods/" + path), method);
if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) {
// requires a body
try {
request.getBody().write(32);
}
catch (UnsupportedOperationException ex) {
// probably a streaming request - let's simply ignore it
}
}
response = request.execute();
assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode());
assertEquals("Invalid method", path.toUpperCase(Locale.ENGLISH), request.getMethod().name());
}
finally {
if (response != null) {
response.close();
}
}
}
/**
* Override the parent class implementation in order to intercept PATCH requests.
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
if (httpMethod == HttpMethod.PATCH || httpMethod == null) {
processRequest(request, response);
}
else {
super.service(request, response);
}
}
@Before
public void beforeMethod() {
uri = URI.create("http://localhost:4242/" + UUID.randomUUID().toString());
method = HttpMethod.PATCH;
requestMock = mock(HttpRequest.class);
doReturn(uri).when(requestMock).getURI();
doReturn(method).when(requestMock).getMethod();
}
/**
* Override the parent class implementation in order to intercept PATCH requests.
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
if (httpMethod == HttpMethod.PATCH || httpMethod == null) {
processRequest(request, response);
}
else {
super.service(request, response);
}
}
/**
* Override the parent class implementation in order to intercept PATCH requests.
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
if (HttpMethod.PATCH == httpMethod || httpMethod == null) {
processRequest(request, response);
}
else {
super.service(request, response);
}
}
@Before
public void beforeMethod() throws IOException {
resetTracing();
initialSpanNameFromStrategy = new AtomicReference<>("span-name-from-strategy-" + UUID.randomUUID().toString());
strategyInitialSpanNameMethodCalled = new AtomicBoolean(false);
strategyRequestTaggingMethodCalled = new AtomicBoolean(false);
strategyResponseTaggingAndFinalSpanNameMethodCalled = new AtomicBoolean(false);
strategyInitialSpanNameArgs = new AtomicReference<>(null);
strategyRequestTaggingArgs = new AtomicReference<>(null);
strategyResponseTaggingArgs = new AtomicReference<>(null);
tagAndNamingStrategy = new ArgCapturingHttpTagAndSpanNamingStrategy(
initialSpanNameFromStrategy, strategyInitialSpanNameMethodCalled, strategyRequestTaggingMethodCalled,
strategyResponseTaggingAndFinalSpanNameMethodCalled, strategyInitialSpanNameArgs,
strategyRequestTaggingArgs, strategyResponseTaggingArgs
);
tagAndNamingAdapterMock = mock(HttpTagAndSpanNamingAdapter.class);
spanRecorder = new SpanRecorder();
Tracer.getInstance().addSpanLifecycleListener(spanRecorder);
method = HttpMethod.PATCH;
uri = URI.create("http://localhost:4242/" + UUID.randomUUID().toString());
headersMock = mock(HttpHeaders.class);
requestMock = mock(HttpRequest.class);
doReturn(headersMock).when(requestMock).getHeaders();
doReturn(method).when(requestMock).getMethod();
doReturn(uri).when(requestMock).getURI();
body = UUID.randomUUID().toString().getBytes();
responseMock = mock(ClientHttpResponse.class);
executionMock = mock(ClientHttpRequestExecution.class);
doAnswer(invocation -> {
tracingStateAtTimeOfExecution = TracingState.getCurrentThreadTracingState();
return responseMock;
}).when(executionMock).execute(any(HttpRequest.class), any(byte[].class));
}
private boolean requireHttpBody(HttpMethod method)
{
return HttpMethod.POST == method || HttpMethod.PUT == method ||
HttpMethod.PATCH == method || HttpMethod.TRACE == method ;
}
@Before
public void beforeMethod() throws IOException {
resetTracing();
initialSpanNameFromStrategy = new AtomicReference<>("span-name-from-strategy-" + UUID.randomUUID().toString());
strategyInitialSpanNameMethodCalled = new AtomicBoolean(false);
strategyRequestTaggingMethodCalled = new AtomicBoolean(false);
strategyResponseTaggingAndFinalSpanNameMethodCalled = new AtomicBoolean(false);
strategyInitialSpanNameArgs = new AtomicReference<>(null);
strategyRequestTaggingArgs = new AtomicReference<>(null);
strategyResponseTaggingArgs = new AtomicReference<>(null);
tagAndNamingStrategy = new ArgCapturingHttpTagAndSpanNamingStrategy(
initialSpanNameFromStrategy, strategyInitialSpanNameMethodCalled, strategyRequestTaggingMethodCalled,
strategyResponseTaggingAndFinalSpanNameMethodCalled, strategyInitialSpanNameArgs,
strategyRequestTaggingArgs, strategyResponseTaggingArgs
);
tagAndNamingAdapterMock = mock(HttpTagAndSpanNamingAdapter.class);
spanRecorder = new SpanRecorder();
Tracer.getInstance().addSpanLifecycleListener(spanRecorder);
method = HttpMethod.PATCH;
uri = URI.create("http://localhost:4242/" + UUID.randomUUID().toString());
headersMock = mock(HttpHeaders.class);
requestMock = mock(HttpRequest.class);
doReturn(headersMock).when(requestMock).getHeaders();
doReturn(method).when(requestMock).getMethod();
doReturn(uri).when(requestMock).getURI();
body = UUID.randomUUID().toString().getBytes();
executionMock = mock(AsyncClientHttpRequestExecution.class);
doAnswer(invocation -> {
tracingStateAtTimeOfExecution = TracingState.getCurrentThreadTracingState();
executionResponseFuture = new SettableListenableFuture<>();
return executionResponseFuture;
}).when(executionMock).executeAsync(any(HttpRequest.class), any(byte[].class));
normalCompletionResponse = mock(ClientHttpResponse.class);
normalResponseCode = 200; //Normal
doReturn(normalResponseCode).when(normalCompletionResponse).getRawStatusCode();
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
* @param uri the URL
* @since 4.0.3
*/
public static MockHttpServletRequestBuilder patch(URI uri) {
return new MockHttpServletRequestBuilder(HttpMethod.PATCH, uri);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param uriVars zero or more URI variables
*/
public static MockHttpServletRequestBuilder patch(String urlTemplate, Object... uriVars) {
return new MockHttpServletRequestBuilder(HttpMethod.PATCH, urlTemplate, uriVars);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
* @param uri the URL
* @since 4.0.3
*/
public static MockHttpServletRequestBuilder patch(URI uri) {
return new MockHttpServletRequestBuilder(HttpMethod.PATCH, uri);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
* @param urlTemplate a URL template; the resulting URL will be encoded
* @param urlVariables zero or more URL variables
*/
public static MockHttpServletRequestBuilder patch(String urlTemplate, Object... urlVariables) {
return new MockHttpServletRequestBuilder(HttpMethod.PATCH, urlTemplate, urlVariables);
}
/**
* Create a {@link MockHttpServletRequestBuilder} for a PATCH request.
* @param uri the URL
* @since 4.0.3
*/
public static MockHttpServletRequestBuilder patch(URI uri) {
return new MockHttpServletRequestBuilder(HttpMethod.PATCH, uri);
}