Add mock implementations of Http[Input|Output]Message
This commit is contained in:
		
							parent
							
								
									4566db82f5
								
							
						
					
					
						commit
						5c13739afa
					
				|  | @ -0,0 +1,56 @@ | |||
| /* | ||||
|  * Copyright 2002-2012 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *      http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| package org.springframework.mock.http; | ||||
| 
 | ||||
| import java.io.ByteArrayInputStream; | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| 
 | ||||
| import org.springframework.http.HttpHeaders; | ||||
| import org.springframework.http.HttpInputMessage; | ||||
| import org.springframework.util.Assert; | ||||
| 
 | ||||
| /** | ||||
|  * Mock implementation of {@link HttpInputMessage}. | ||||
|  * | ||||
|  * @author Rossen Stoyanchev | ||||
|  * @since 3.2 | ||||
|  */ | ||||
| public class MockHttpInputMessage implements HttpInputMessage { | ||||
| 
 | ||||
| 	private final HttpHeaders headers = new HttpHeaders(); | ||||
| 
 | ||||
| 	private final InputStream body; | ||||
| 
 | ||||
| 
 | ||||
| 	public MockHttpInputMessage(byte[] contents) { | ||||
| 		this.body = (contents != null) ? new ByteArrayInputStream(contents) : null; | ||||
| 	} | ||||
| 
 | ||||
| 	public MockHttpInputMessage(InputStream body) { | ||||
| 		Assert.notNull(body, "'body' must not be null"); | ||||
| 		this.body = body; | ||||
| 	} | ||||
| 
 | ||||
| 	public HttpHeaders getHeaders() { | ||||
| 		return this.headers; | ||||
| 	} | ||||
| 
 | ||||
| 	public InputStream getBody() throws IOException { | ||||
| 		return this.body; | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,86 @@ | |||
| /* | ||||
|  * Copyright 2002-2012 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *      http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| package org.springframework.mock.http; | ||||
| 
 | ||||
| import java.io.ByteArrayOutputStream; | ||||
| import java.io.IOException; | ||||
| import java.io.OutputStream; | ||||
| import java.io.UnsupportedEncodingException; | ||||
| import java.nio.charset.Charset; | ||||
| 
 | ||||
| import org.springframework.http.HttpHeaders; | ||||
| import org.springframework.http.HttpOutputMessage; | ||||
| 
 | ||||
| /** | ||||
|  * Mock implementation of {@link HttpOutputMessage}. | ||||
|  * | ||||
|  * @author Rossen Stoyanchev | ||||
|  * @since 3.2 | ||||
|  */ | ||||
| public class MockHttpOutputMessage implements HttpOutputMessage { | ||||
| 
 | ||||
| 	private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); | ||||
| 
 | ||||
| 	private final HttpHeaders headers = new HttpHeaders(); | ||||
| 
 | ||||
| 	private final ByteArrayOutputStream body = new ByteArrayOutputStream(); | ||||
| 
 | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Return the headers. | ||||
| 	 */ | ||||
| 	public HttpHeaders getHeaders() { | ||||
| 		return this.headers; | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Return the body content. | ||||
| 	 */ | ||||
| 	public OutputStream getBody() throws IOException { | ||||
| 		return this.body; | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Return body content as a byte array. | ||||
| 	 */ | ||||
| 	public byte[] getBodyAsBytes() { | ||||
| 		return this.body.toByteArray(); | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Return the body content interpreted as a UTF-8 string. | ||||
| 	 */ | ||||
| 	public String getBodyAsString() { | ||||
| 		return getBodyAsString(DEFAULT_CHARSET); | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Return the body content as a string. | ||||
| 	 * @param charset the charset to use to turn the body content to a String | ||||
| 	 */ | ||||
| 	public String getBodyAsString(Charset charset) { | ||||
| 		byte[] bytes = getBodyAsBytes(); | ||||
| 		try { | ||||
| 			// Use | ||||
| 			return new String(bytes, charset.name()); | ||||
| 		} | ||||
| 		catch (UnsupportedEncodingException ex) { | ||||
| 			// should not occur | ||||
| 			throw new InternalError(ex.getMessage()); | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,111 @@ | |||
| /* | ||||
|  * Copyright 2002-2012 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *      http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| package org.springframework.mock.http.client; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| import java.net.URI; | ||||
| 
 | ||||
| import org.springframework.http.HttpMethod; | ||||
| import org.springframework.http.client.ClientHttpRequest; | ||||
| import org.springframework.http.client.ClientHttpResponse; | ||||
| import org.springframework.mock.http.MockHttpOutputMessage; | ||||
| 
 | ||||
| /** | ||||
|  * Mock implementation of {@link ClientHttpRequest}. | ||||
|  * | ||||
|  * @author Rossen Stoyanchev | ||||
|  * @since 3.2 | ||||
|  */ | ||||
| public class MockClientHttpRequest extends MockHttpOutputMessage implements ClientHttpRequest { | ||||
| 
 | ||||
| 	private URI uri; | ||||
| 
 | ||||
| 	private HttpMethod httpMethod; | ||||
| 
 | ||||
| 	private boolean executed = false; | ||||
| 
 | ||||
| 	private ClientHttpResponse clientHttpResponse; | ||||
| 
 | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Default constructor. | ||||
| 	 */ | ||||
| 	public MockClientHttpRequest() { | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Create an instance with the given HttpMethod and URI. | ||||
| 	 */ | ||||
| 	public MockClientHttpRequest(HttpMethod httpMethod, URI uri) { | ||||
| 		this.httpMethod = httpMethod; | ||||
| 		this.uri = uri; | ||||
| 	} | ||||
| 
 | ||||
| 	public URI getURI() { | ||||
| 		return this.uri; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setURI(URI uri) { | ||||
| 		this.uri = uri; | ||||
| 	} | ||||
| 
 | ||||
| 	public HttpMethod getMethod() { | ||||
| 		return this.httpMethod; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setMethod(HttpMethod httpMethod) { | ||||
| 		this.httpMethod = httpMethod; | ||||
| 	} | ||||
| 
 | ||||
| 	public void setResponse(ClientHttpResponse clientHttpResponse) { | ||||
| 		this.clientHttpResponse = clientHttpResponse; | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Whether the execute method was invoked. | ||||
| 	 */ | ||||
| 	public boolean isExecuted() { | ||||
| 		return this.executed; | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Sets the {@link #isExecuted() executed} flag to true and returns the | ||||
| 	 * configured {@link #setResponse(ClientHttpResponse) response}. | ||||
| 	 */ | ||||
| 	public ClientHttpResponse execute() throws IOException { | ||||
| 		this.executed = true; | ||||
| 		return this.clientHttpResponse; | ||||
| 	} | ||||
| 
 | ||||
| 	@Override | ||||
| 	public String toString() { | ||||
| 		StringBuilder sb = new StringBuilder(); | ||||
| 		if (this.httpMethod != null) { | ||||
| 			sb.append(this.httpMethod); | ||||
| 		} | ||||
| 		if (this.uri != null) { | ||||
| 			sb.append(" ").append(this.uri); | ||||
| 		} | ||||
| 		if (!getHeaders().isEmpty()) { | ||||
| 			sb.append(", headers : ").append(getHeaders()); | ||||
| 		} | ||||
| 		if (sb.length() == 0) { | ||||
| 			sb.append("Not yet initialized"); | ||||
| 		} | ||||
| 		return sb.toString(); | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,70 @@ | |||
| /* | ||||
|  * Copyright 2002-2012 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *      http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| package org.springframework.mock.http.client; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| 
 | ||||
| import org.springframework.http.HttpStatus; | ||||
| import org.springframework.http.client.ClientHttpResponse; | ||||
| import org.springframework.mock.http.MockHttpInputMessage; | ||||
| import org.springframework.util.Assert; | ||||
| 
 | ||||
| /** | ||||
|  * Mock implementation of {@link ClientHttpResponse}. | ||||
|  * | ||||
|  * @author Rossen Stoyanchev | ||||
|  * @since 3.2 | ||||
|  */ | ||||
| public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse { | ||||
| 
 | ||||
| 	private final HttpStatus status; | ||||
| 
 | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Constructor with response body as a byte array. | ||||
| 	 */ | ||||
| 	public MockClientHttpResponse(byte[] body, HttpStatus statusCode) { | ||||
| 		super(body); | ||||
| 		Assert.notNull(statusCode, "statisCode is required"); | ||||
| 		this.status = statusCode; | ||||
| 	} | ||||
| 
 | ||||
| 	/** | ||||
| 	 * Constructor with response body as InputStream. | ||||
| 	 */ | ||||
| 	public MockClientHttpResponse(InputStream body, HttpStatus statusCode) { | ||||
| 		super(body); | ||||
| 		Assert.notNull(statusCode, "statisCode is required"); | ||||
| 		this.status = statusCode; | ||||
| 	} | ||||
| 
 | ||||
| 	public HttpStatus getStatusCode() throws IOException { | ||||
| 		return this.status; | ||||
| 	} | ||||
| 
 | ||||
| 	public int getRawStatusCode() throws IOException { | ||||
| 		return this.status.value(); | ||||
| 	} | ||||
| 
 | ||||
| 	public String getStatusText() throws IOException { | ||||
| 		return this.status.getReasonPhrase(); | ||||
| 	} | ||||
| 
 | ||||
| 	public void close() { | ||||
| 	} | ||||
| 
 | ||||
| } | ||||
|  | @ -0,0 +1,23 @@ | |||
| /* | ||||
|  * Copyright 2002-2012 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *      http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| /** | ||||
|  * Mock implementations of client-side HTTP abstractions. | ||||
|  * This package contains the <code>MockClientHttpRequest</code> and | ||||
|  * <code>MockClientHttpResponse</code>. | ||||
|  */ | ||||
| package org.springframework.mock.http.client; | ||||
| 
 | ||||
|  | @ -0,0 +1,23 @@ | |||
| /* | ||||
|  * Copyright 2002-2012 the original author or authors. | ||||
|  * | ||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||
|  * you may not use this file except in compliance with the License. | ||||
|  * You may obtain a copy of the License at | ||||
|  * | ||||
|  *      http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  * | ||||
|  * Unless required by applicable law or agreed to in writing, software | ||||
|  * distributed under the License is distributed on an "AS IS" BASIS, | ||||
|  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
|  * See the License for the specific language governing permissions and | ||||
|  * limitations under the License. | ||||
|  */ | ||||
| 
 | ||||
| /** | ||||
|  * Mock implementations of client/server-side HTTP abstractions. | ||||
|  * This package contains <code>MockHttpInputMessage</code> and | ||||
|  * <code>MockHttpOutputMessage</code>. | ||||
|  */ | ||||
| package org.springframework.mock.http; | ||||
| 
 | ||||
		Loading…
	
		Reference in New Issue