001/* 002 * Copyright 2002-2019 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.test.web.client.response; 018 019import java.io.IOException; 020import java.net.URI; 021 022import org.springframework.core.io.Resource; 023import org.springframework.http.HttpStatus; 024import org.springframework.http.MediaType; 025import org.springframework.lang.Nullable; 026import org.springframework.test.web.client.ResponseCreator; 027 028/** 029 * Static factory methods for obtaining a {@link ResponseCreator} instance. 030 * 031 * <p><strong>Eclipse users:</strong> consider adding this class as a Java editor 032 * favorite. To navigate, open the Preferences and type "favorites". 033 * 034 * @author Rossen Stoyanchev 035 * @since 3.2 036 */ 037public abstract class MockRestResponseCreators { 038 039 /** 040 * {@code ResponseCreator} for a 200 response (OK). 041 */ 042 public static DefaultResponseCreator withSuccess() { 043 return new DefaultResponseCreator(HttpStatus.OK); 044 } 045 046 /** 047 * {@code ResponseCreator} for a 200 response (OK) with String body. 048 * @param body the response body, a "UTF-8" string 049 * @param contentType the type of the content (may be {@code null}) 050 */ 051 public static DefaultResponseCreator withSuccess(String body, @Nullable MediaType contentType) { 052 DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(body); 053 return (contentType != null ? creator.contentType(contentType) : creator); 054 } 055 056 /** 057 * {@code ResponseCreator} for a 200 response (OK) with byte[] body. 058 * @param body the response body 059 * @param contentType the type of the content (may be {@code null}) 060 */ 061 public static DefaultResponseCreator withSuccess(byte[] body, @Nullable MediaType contentType) { 062 DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(body); 063 return (contentType != null ? creator.contentType(contentType) : creator); 064 } 065 066 /** 067 * {@code ResponseCreator} for a 200 response (OK) content with {@link Resource}-based body. 068 * @param body the response body 069 * @param contentType the type of the content (may be {@code null}) 070 */ 071 public static DefaultResponseCreator withSuccess(Resource body, @Nullable MediaType contentType) { 072 DefaultResponseCreator creator = new DefaultResponseCreator(HttpStatus.OK).body(body); 073 return (contentType != null ? creator.contentType(contentType) : creator); 074 } 075 076 /** 077 * {@code ResponseCreator} for a 201 response (CREATED) with a 'Location' header. 078 * @param location the value for the {@code Location} header 079 */ 080 public static DefaultResponseCreator withCreatedEntity(URI location) { 081 return new DefaultResponseCreator(HttpStatus.CREATED).location(location); 082 } 083 084 /** 085 * {@code ResponseCreator} for a 204 response (NO_CONTENT). 086 */ 087 public static DefaultResponseCreator withNoContent() { 088 return new DefaultResponseCreator(HttpStatus.NO_CONTENT); 089 } 090 091 /** 092 * {@code ResponseCreator} for a 400 response (BAD_REQUEST). 093 */ 094 public static DefaultResponseCreator withBadRequest() { 095 return new DefaultResponseCreator(HttpStatus.BAD_REQUEST); 096 } 097 098 /** 099 * {@code ResponseCreator} for a 401 response (UNAUTHORIZED). 100 */ 101 public static DefaultResponseCreator withUnauthorizedRequest() { 102 return new DefaultResponseCreator(HttpStatus.UNAUTHORIZED); 103 } 104 105 /** 106 * {@code ResponseCreator} for a 500 response (SERVER_ERROR). 107 */ 108 public static DefaultResponseCreator withServerError() { 109 return new DefaultResponseCreator(HttpStatus.INTERNAL_SERVER_ERROR); 110 } 111 112 /** 113 * {@code ResponseCreator} with a specific HTTP status. 114 * @param status the response status 115 */ 116 public static DefaultResponseCreator withStatus(HttpStatus status) { 117 return new DefaultResponseCreator(status); 118 } 119 120 /** 121 * {@code ResponseCreator} with an internal application {@code IOException}. 122 * <p>For example, one could use this to simulate a {@code SocketTimeoutException}. 123 * @param ex the {@code Exception} to be thrown at HTTP call time 124 * @since 5.2.2 125 */ 126 public static ResponseCreator withException(IOException ex) { 127 return request -> { 128 throw ex; 129 }; 130 } 131 132}