Skip to content

Commit 5c13739

Browse files
committed
Add mock implementations of Http[Input|Output]Message
1 parent 4566db8 commit 5c13739

File tree

6 files changed

+369
-0
lines changed

6 files changed

+369
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.mock.http;
17+
18+
import java.io.ByteArrayInputStream;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
22+
import org.springframework.http.HttpHeaders;
23+
import org.springframework.http.HttpInputMessage;
24+
import org.springframework.util.Assert;
25+
26+
/**
27+
* Mock implementation of {@link HttpInputMessage}.
28+
*
29+
* @author Rossen Stoyanchev
30+
* @since 3.2
31+
*/
32+
public class MockHttpInputMessage implements HttpInputMessage {
33+
34+
private final HttpHeaders headers = new HttpHeaders();
35+
36+
private final InputStream body;
37+
38+
39+
public MockHttpInputMessage(byte[] contents) {
40+
this.body = (contents != null) ? new ByteArrayInputStream(contents) : null;
41+
}
42+
43+
public MockHttpInputMessage(InputStream body) {
44+
Assert.notNull(body, "'body' must not be null");
45+
this.body = body;
46+
}
47+
48+
public HttpHeaders getHeaders() {
49+
return this.headers;
50+
}
51+
52+
public InputStream getBody() throws IOException {
53+
return this.body;
54+
}
55+
56+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.mock.http;
17+
18+
import java.io.ByteArrayOutputStream;
19+
import java.io.IOException;
20+
import java.io.OutputStream;
21+
import java.io.UnsupportedEncodingException;
22+
import java.nio.charset.Charset;
23+
24+
import org.springframework.http.HttpHeaders;
25+
import org.springframework.http.HttpOutputMessage;
26+
27+
/**
28+
* Mock implementation of {@link HttpOutputMessage}.
29+
*
30+
* @author Rossen Stoyanchev
31+
* @since 3.2
32+
*/
33+
public class MockHttpOutputMessage implements HttpOutputMessage {
34+
35+
private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
36+
37+
private final HttpHeaders headers = new HttpHeaders();
38+
39+
private final ByteArrayOutputStream body = new ByteArrayOutputStream();
40+
41+
42+
/**
43+
* Return the headers.
44+
*/
45+
public HttpHeaders getHeaders() {
46+
return this.headers;
47+
}
48+
49+
/**
50+
* Return the body content.
51+
*/
52+
public OutputStream getBody() throws IOException {
53+
return this.body;
54+
}
55+
56+
/**
57+
* Return body content as a byte array.
58+
*/
59+
public byte[] getBodyAsBytes() {
60+
return this.body.toByteArray();
61+
}
62+
63+
/**
64+
* Return the body content interpreted as a UTF-8 string.
65+
*/
66+
public String getBodyAsString() {
67+
return getBodyAsString(DEFAULT_CHARSET);
68+
}
69+
70+
/**
71+
* Return the body content as a string.
72+
* @param charset the charset to use to turn the body content to a String
73+
*/
74+
public String getBodyAsString(Charset charset) {
75+
byte[] bytes = getBodyAsBytes();
76+
try {
77+
// Use
78+
return new String(bytes, charset.name());
79+
}
80+
catch (UnsupportedEncodingException ex) {
81+
// should not occur
82+
throw new InternalError(ex.getMessage());
83+
}
84+
}
85+
86+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.mock.http.client;
17+
18+
import java.io.IOException;
19+
import java.net.URI;
20+
21+
import org.springframework.http.HttpMethod;
22+
import org.springframework.http.client.ClientHttpRequest;
23+
import org.springframework.http.client.ClientHttpResponse;
24+
import org.springframework.mock.http.MockHttpOutputMessage;
25+
26+
/**
27+
* Mock implementation of {@link ClientHttpRequest}.
28+
*
29+
* @author Rossen Stoyanchev
30+
* @since 3.2
31+
*/
32+
public class MockClientHttpRequest extends MockHttpOutputMessage implements ClientHttpRequest {
33+
34+
private URI uri;
35+
36+
private HttpMethod httpMethod;
37+
38+
private boolean executed = false;
39+
40+
private ClientHttpResponse clientHttpResponse;
41+
42+
43+
/**
44+
* Default constructor.
45+
*/
46+
public MockClientHttpRequest() {
47+
}
48+
49+
/**
50+
* Create an instance with the given HttpMethod and URI.
51+
*/
52+
public MockClientHttpRequest(HttpMethod httpMethod, URI uri) {
53+
this.httpMethod = httpMethod;
54+
this.uri = uri;
55+
}
56+
57+
public URI getURI() {
58+
return this.uri;
59+
}
60+
61+
public void setURI(URI uri) {
62+
this.uri = uri;
63+
}
64+
65+
public HttpMethod getMethod() {
66+
return this.httpMethod;
67+
}
68+
69+
public void setMethod(HttpMethod httpMethod) {
70+
this.httpMethod = httpMethod;
71+
}
72+
73+
public void setResponse(ClientHttpResponse clientHttpResponse) {
74+
this.clientHttpResponse = clientHttpResponse;
75+
}
76+
77+
/**
78+
* Whether the execute method was invoked.
79+
*/
80+
public boolean isExecuted() {
81+
return this.executed;
82+
}
83+
84+
/**
85+
* Sets the {@link #isExecuted() executed} flag to true and returns the
86+
* configured {@link #setResponse(ClientHttpResponse) response}.
87+
*/
88+
public ClientHttpResponse execute() throws IOException {
89+
this.executed = true;
90+
return this.clientHttpResponse;
91+
}
92+
93+
@Override
94+
public String toString() {
95+
StringBuilder sb = new StringBuilder();
96+
if (this.httpMethod != null) {
97+
sb.append(this.httpMethod);
98+
}
99+
if (this.uri != null) {
100+
sb.append(" ").append(this.uri);
101+
}
102+
if (!getHeaders().isEmpty()) {
103+
sb.append(", headers : ").append(getHeaders());
104+
}
105+
if (sb.length() == 0) {
106+
sb.append("Not yet initialized");
107+
}
108+
return sb.toString();
109+
}
110+
111+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.mock.http.client;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
21+
import org.springframework.http.HttpStatus;
22+
import org.springframework.http.client.ClientHttpResponse;
23+
import org.springframework.mock.http.MockHttpInputMessage;
24+
import org.springframework.util.Assert;
25+
26+
/**
27+
* Mock implementation of {@link ClientHttpResponse}.
28+
*
29+
* @author Rossen Stoyanchev
30+
* @since 3.2
31+
*/
32+
public class MockClientHttpResponse extends MockHttpInputMessage implements ClientHttpResponse {
33+
34+
private final HttpStatus status;
35+
36+
37+
/**
38+
* Constructor with response body as a byte array.
39+
*/
40+
public MockClientHttpResponse(byte[] body, HttpStatus statusCode) {
41+
super(body);
42+
Assert.notNull(statusCode, "statisCode is required");
43+
this.status = statusCode;
44+
}
45+
46+
/**
47+
* Constructor with response body as InputStream.
48+
*/
49+
public MockClientHttpResponse(InputStream body, HttpStatus statusCode) {
50+
super(body);
51+
Assert.notNull(statusCode, "statisCode is required");
52+
this.status = statusCode;
53+
}
54+
55+
public HttpStatus getStatusCode() throws IOException {
56+
return this.status;
57+
}
58+
59+
public int getRawStatusCode() throws IOException {
60+
return this.status.value();
61+
}
62+
63+
public String getStatusText() throws IOException {
64+
return this.status.getReasonPhrase();
65+
}
66+
67+
public void close() {
68+
}
69+
70+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Mock implementations of client-side HTTP abstractions.
19+
* This package contains the <code>MockClientHttpRequest</code> and
20+
* <code>MockClientHttpResponse</code>.
21+
*/
22+
package org.springframework.mock.http.client;
23+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2002-2012 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Mock implementations of client/server-side HTTP abstractions.
19+
* This package contains <code>MockHttpInputMessage</code> and
20+
* <code>MockHttpOutputMessage</code>.
21+
*/
22+
package org.springframework.mock.http;
23+

0 commit comments

Comments
 (0)