View Javadoc
1   /*
2    * Copyright 2002-2013 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  package org.springframework.web.socket.handler;
18  
19  import java.io.IOException;
20  import java.net.InetSocketAddress;
21  import java.net.URI;
22  import java.security.Principal;
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.springframework.http.HttpHeaders;
29  import org.springframework.web.socket.CloseStatus;
30  import org.springframework.web.socket.WebSocketExtension;
31  import org.springframework.web.socket.WebSocketMessage;
32  import org.springframework.web.socket.WebSocketSession;
33  
34  /**
35   * A {@link WebSocketSession} for use in tests.
36   *
37   * @author Rossen Stoyanchev
38   */
39  public class TestWebSocketSession implements WebSocketSession {
40  
41  	private String id;
42  
43  	private URI uri;
44  
45  	private Map<String, Object> attributes = new HashMap<String, Object>();
46  
47  	private Principal principal;
48  
49  	private InetSocketAddress localAddress;
50  
51  	private InetSocketAddress remoteAddress;
52  
53  	private String protocol;
54  
55  	private List<WebSocketExtension> extensions = new ArrayList<WebSocketExtension>();
56  
57  	private boolean open;
58  
59  	private final List<WebSocketMessage<?>> messages = new ArrayList<>();
60  
61  	private CloseStatus status;
62  
63  	private HttpHeaders headers;
64  
65  	public TestWebSocketSession() {
66  	}
67  
68  	public TestWebSocketSession(String id) {
69  		this.id = id;
70  	}
71  
72  	@Override
73  	public String getId() {
74  		return this.id;
75  	}
76  
77  	public void setId(String id) {
78  		this.id = id;
79  	}
80  
81  	@Override
82  	public URI getUri() {
83  		return this.uri;
84  	}
85  
86  	public void setUri(URI uri) {
87  		this.uri = uri;
88  	}
89  
90  
91  	@Override
92  	public HttpHeaders getHandshakeHeaders() {
93  		return this.headers;
94  	}
95  
96  	public HttpHeaders getHeaders() {
97  		return this.headers;
98  	}
99  
100 	public void setHeaders(HttpHeaders headers) {
101 		this.headers = headers;
102 	}
103 
104 	public void setAttributes(Map<String, Object> attributes) {
105 		this.attributes = attributes;
106 	}
107 
108 	@Override
109 	public Map<String, Object> getAttributes() {
110 		return this.attributes;
111 	}
112 
113 	@Override
114 	public Principal getPrincipal() {
115 		return this.principal;
116 	}
117 
118 	public void setPrincipal(Principal principal) {
119 		this.principal = principal;
120 	}
121 
122 	@Override
123 	public InetSocketAddress getLocalAddress() {
124 		return this.localAddress;
125 	}
126 
127 	public void setLocalAddress(InetSocketAddress localAddress) {
128 		this.localAddress = localAddress;
129 	}
130 
131 	@Override
132 	public InetSocketAddress getRemoteAddress() {
133 		return this.remoteAddress;
134 	}
135 
136 	public void setRemoteAddress(InetSocketAddress remoteAddress) {
137 		this.remoteAddress = remoteAddress;
138 	}
139 
140 	public String getAcceptedProtocol() {
141 		return this.protocol;
142 	}
143 
144 	public void setAcceptedProtocol(String protocol) {
145 		this.protocol = protocol;
146 	}
147 
148 	@Override
149 	public void setTextMessageSizeLimit(int messageSizeLimit) {
150 	}
151 
152 	@Override
153 	public int getTextMessageSizeLimit() {
154 		return 0;
155 	}
156 
157 	@Override
158 	public void setBinaryMessageSizeLimit(int messageSizeLimit) {
159 	}
160 
161 	@Override
162 	public int getBinaryMessageSizeLimit() {
163 		return 0;
164 	}
165 
166 	@Override
167 	public List<WebSocketExtension> getExtensions() {
168 		return this.extensions;
169 	}
170 
171 	public void setExtensions(List<WebSocketExtension> extensions) {
172 		this.extensions = extensions;
173 	}
174 
175 	@Override
176 	public boolean isOpen() {
177 		return this.open;
178 	}
179 
180 	public void setOpen(boolean open) {
181 		this.open = open;
182 	}
183 
184 	public List<WebSocketMessage<?>> getSentMessages() {
185 		return this.messages;
186 	}
187 
188 	public CloseStatus getCloseStatus() {
189 		return this.status;
190 	}
191 
192 	@Override
193 	public void sendMessage(WebSocketMessage<?> message) throws IOException {
194 		this.messages.add(message);
195 	}
196 
197 	@Override
198 	public void close() throws IOException {
199 		this.open = false;
200 	}
201 
202 	@Override
203 	public void close(CloseStatus status) throws IOException {
204 		this.open = false;
205 		this.status = status;
206 	}
207 
208 }