1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.web.socket.sockjs.transport.session;
18
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.springframework.web.socket.CloseStatus;
25 import org.springframework.web.socket.WebSocketHandler;
26 import org.springframework.web.socket.sockjs.SockJsTransportFailureException;
27 import org.springframework.web.socket.sockjs.frame.SockJsFrame;
28 import org.springframework.web.socket.sockjs.transport.SockJsServiceConfig;
29
30
31
32
33 public class TestHttpSockJsSession extends AbstractHttpSockJsSession {
34
35 private boolean active;
36
37 private final List<SockJsFrame> sockJsFrames = new ArrayList<>();
38
39 private CloseStatus closeStatus;
40
41 private IOException exceptionOnWrite;
42
43 private int numberOfLastActiveTimeUpdates;
44
45 private boolean cancelledHeartbeat;
46
47 private String subProtocol;
48
49
50 public TestHttpSockJsSession(String sessionId, SockJsServiceConfig config,
51 WebSocketHandler wsHandler, Map<String, Object> attributes) {
52
53 super(sessionId, config, wsHandler, attributes);
54 }
55
56 @Override
57 protected boolean isStreaming() {
58 return true;
59 }
60
61 @Override
62 public String getAcceptedProtocol() {
63 return this.subProtocol;
64 }
65
66 @Override
67 public void setAcceptedProtocol(String protocol) {
68 this.subProtocol = protocol;
69 }
70
71 public CloseStatus getCloseStatus() {
72 return this.closeStatus;
73 }
74
75 @Override
76 public boolean isActive() {
77 return this.active;
78 }
79
80 public void setActive(boolean active) {
81 this.active = active;
82 }
83
84 public List<SockJsFrame> getSockJsFramesWritten() {
85 return this.sockJsFrames;
86 }
87
88 public void setExceptionOnWrite(IOException exceptionOnWrite) {
89 this.exceptionOnWrite = exceptionOnWrite;
90 }
91
92 public int getNumberOfLastActiveTimeUpdates() {
93 return this.numberOfLastActiveTimeUpdates;
94 }
95
96 public boolean didCancelHeartbeat() {
97 return this.cancelledHeartbeat;
98 }
99
100 @Override
101 protected void updateLastActiveTime() {
102 this.numberOfLastActiveTimeUpdates++;
103 super.updateLastActiveTime();
104 }
105
106 @Override
107 protected void cancelHeartbeat() {
108 this.cancelledHeartbeat = true;
109 super.cancelHeartbeat();
110 }
111
112 @Override
113 protected void writeFrameInternal(SockJsFrame frame) throws IOException {
114 this.sockJsFrames.add(frame);
115 if (this.exceptionOnWrite != null) {
116 throw this.exceptionOnWrite;
117 }
118 }
119
120 @Override
121 protected void disconnect(CloseStatus status) {
122 this.closeStatus = status;
123 }
124
125 @Override
126 protected void flushCache() throws SockJsTransportFailureException {
127 }
128
129 }