001/*
002 * Copyright 2002-2017 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.mock.http;
018
019import java.io.ByteArrayInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022
023import org.springframework.http.HttpHeaders;
024import org.springframework.http.HttpInputMessage;
025import org.springframework.util.Assert;
026
027/**
028 * Mock implementation of {@link HttpInputMessage}.
029 *
030 * @author Rossen Stoyanchev
031 * @since 3.2
032 */
033public class MockHttpInputMessage implements HttpInputMessage {
034
035        private final HttpHeaders headers = new HttpHeaders();
036
037        private final InputStream body;
038
039
040        public MockHttpInputMessage(byte[] content) {
041                Assert.notNull(content, "Byte array must not be null");
042                this.body = new ByteArrayInputStream(content);
043        }
044
045        public MockHttpInputMessage(InputStream body) {
046                Assert.notNull(body, "InputStream must not be null");
047                this.body = body;
048        }
049
050
051        @Override
052        public HttpHeaders getHeaders() {
053                return this.headers;
054        }
055
056        @Override
057        public InputStream getBody() throws IOException {
058                return this.body;
059        }
060
061}