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.client; 018 019import java.io.IOException; 020import java.io.InputStream; 021 022import org.springframework.http.HttpStatus; 023import org.springframework.http.client.ClientHttpResponse; 024import org.springframework.mock.http.MockHttpInputMessage; 025import org.springframework.util.Assert; 026 027/** 028 * Mock implementation of {@link ClientHttpResponse}. 029 * 030 * @author Rossen Stoyanchev 031 * @since 3.2 032 */ 033public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse { 034 035 private final HttpStatus status; 036 037 038 /** 039 * Constructor with response body as a byte array. 040 */ 041 public MockClientHttpResponse(byte[] body, HttpStatus statusCode) { 042 super(body); 043 Assert.notNull(statusCode, "HttpStatus is required"); 044 this.status = statusCode; 045 } 046 047 /** 048 * Constructor with response body as InputStream. 049 */ 050 public MockClientHttpResponse(InputStream body, HttpStatus statusCode) { 051 super(body); 052 Assert.notNull(statusCode, "HttpStatus is required"); 053 this.status = statusCode; 054 } 055 056 057 @Override 058 public HttpStatus getStatusCode() throws IOException { 059 return this.status; 060 } 061 062 @Override 063 public int getRawStatusCode() throws IOException { 064 return this.status.value(); 065 } 066 067 @Override 068 public String getStatusText() throws IOException { 069 return this.status.getReasonPhrase(); 070 } 071 072 @Override 073 public void close() { 074 try { 075 getBody().close(); 076 } 077 catch (IOException ex) { 078 // ignore 079 } 080 } 081 082}