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[] contents) { 041 this.body = new ByteArrayInputStream(contents != null ? contents : new byte[0]); 042 } 043 044 public MockHttpInputMessage(InputStream body) { 045 Assert.notNull(body, "InputStream must not be null"); 046 this.body = body; 047 } 048 049 050 @Override 051 public HttpHeaders getHeaders() { 052 return this.headers; 053 } 054 055 @Override 056 public InputStream getBody() throws IOException { 057 return this.body; 058 } 059 060}