001/*
002 * Copyright 2002-2016 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 */
016package org.springframework.test.web.client.response;
017
018import java.net.URI;
019
020import org.springframework.core.io.Resource;
021import org.springframework.http.HttpStatus;
022import org.springframework.http.MediaType;
023import org.springframework.test.web.client.ResponseCreator;
024
025/**
026 * Static factory methods for obtaining a {@link ResponseCreator} instance.
027 *
028 * <p><strong>Eclipse users:</strong> consider adding this class as a Java editor
029 * favorite. To navigate, open the Preferences and type "favorites".
030 *
031 * @author Rossen Stoyanchev
032 * @since 3.2
033 */
034public abstract class MockRestResponseCreators {
035
036        /**
037         * {@code ResponseCreator} for a 200 response (OK).
038         */
039        public static DefaultResponseCreator withSuccess() {
040                return new DefaultResponseCreator(HttpStatus.OK);
041        }
042
043        /**
044         * {@code ResponseCreator} for a 200 response (OK) with String body.
045         * @param body the response body, a "UTF-8" string
046         * @param mediaType the type of the content, may be {@code null}
047         */
048        public static DefaultResponseCreator withSuccess(String body, MediaType mediaType) {
049                return new DefaultResponseCreator(HttpStatus.OK).body(body).contentType(mediaType);
050        }
051
052        /**
053         * {@code ResponseCreator} for a 200 response (OK) with byte[] body.
054         * @param body the response body
055         * @param contentType the type of the content, may be {@code null}
056         */
057        public static DefaultResponseCreator withSuccess(byte[] body, MediaType contentType) {
058                return new DefaultResponseCreator(HttpStatus.OK).body(body).contentType(contentType);
059        }
060
061        /**
062         * {@code ResponseCreator} for a 200 response (OK) content with {@link Resource}-based body.
063         * @param body the response body
064         * @param contentType the type of the content, may be {@code null}
065         */
066        public static DefaultResponseCreator withSuccess(Resource body, MediaType contentType) {
067                return new DefaultResponseCreator(HttpStatus.OK).body(body).contentType(contentType);
068        }
069
070        /**
071         * {@code ResponseCreator} for a 201 response (CREATED) with a 'Location' header.
072         * @param location the value for the {@code Location} header
073         */
074        public static DefaultResponseCreator withCreatedEntity(URI location) {
075                return new DefaultResponseCreator(HttpStatus.CREATED).location(location);
076        }
077
078        /**
079         * {@code ResponseCreator} for a 204 response (NO_CONTENT).
080         */
081        public static DefaultResponseCreator withNoContent() {
082                return new DefaultResponseCreator(HttpStatus.NO_CONTENT);
083        }
084
085        /**
086         * {@code ResponseCreator} for a 400 response (BAD_REQUEST).
087         */
088        public static DefaultResponseCreator withBadRequest() {
089                return new DefaultResponseCreator(HttpStatus.BAD_REQUEST);
090        }
091
092        /**
093         * {@code ResponseCreator} for a 401 response (UNAUTHORIZED).
094         */
095        public static DefaultResponseCreator withUnauthorizedRequest() {
096                return new DefaultResponseCreator(HttpStatus.UNAUTHORIZED);
097        }
098
099        /**
100         * {@code ResponseCreator} for a 500 response (SERVER_ERROR).
101         */
102        public static DefaultResponseCreator withServerError() {
103                return new DefaultResponseCreator(HttpStatus.INTERNAL_SERVER_ERROR);
104        }
105
106        /**
107         * {@code ResponseCreator} with a specific HTTP status.
108         * @param status the response status
109         */
110        public static DefaultResponseCreator withStatus(HttpStatus status) {
111                return new DefaultResponseCreator(status);
112        }
113
114}