1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
26
26
import org .mockito .Mock ;
27
27
import org .mockito .MockitoAnnotations ;
28
28
29
+ import org .springframework .http .HttpHeaders ;
29
30
import org .springframework .web .socket .AbstractHttpRequestTests ;
30
31
import org .springframework .web .socket .SubProtocolCapable ;
31
32
import org .springframework .web .socket .WebSocketExtension ;
@@ -62,14 +63,9 @@ public void setup() {
62
63
public void supportedSubProtocols () {
63
64
this .handshakeHandler .setSupportedProtocols ("stomp" , "mqtt" );
64
65
given (this .upgradeStrategy .getSupportedVersions ()).willReturn (new String [] {"13" });
65
- this .servletRequest .setMethod ("GET" );
66
66
67
- WebSocketHttpHeaders headers = new WebSocketHttpHeaders (this .request .getHeaders ());
68
- headers .setUpgrade ("WebSocket" );
69
- headers .setConnection ("Upgrade" );
70
- headers .setSecWebSocketVersion ("13" );
71
- headers .setSecWebSocketKey ("82/ZS2YHjEnUN97HLL8tbw==" );
72
- headers .setSecWebSocketProtocol ("STOMP" );
67
+ this .servletRequest .setMethod ("GET" );
68
+ initHeaders (this .request .getHeaders ()).setSecWebSocketProtocol ("STOMP" );
73
69
74
70
WebSocketHandler handler = new TextWebSocketHandler ();
75
71
Map <String , Object > attributes = Collections .emptyMap ();
@@ -88,16 +84,10 @@ public void supportedExtensions() {
88
84
given (this .upgradeStrategy .getSupportedExtensions (this .request )).willReturn (Collections .singletonList (extension1 ));
89
85
90
86
this .servletRequest .setMethod ("GET" );
91
-
92
- WebSocketHttpHeaders headers = new WebSocketHttpHeaders (this .request .getHeaders ());
93
- headers .setUpgrade ("WebSocket" );
94
- headers .setConnection ("Upgrade" );
95
- headers .setSecWebSocketVersion ("13" );
96
- headers .setSecWebSocketKey ("82/ZS2YHjEnUN97HLL8tbw==" );
97
- headers .setSecWebSocketExtensions (Arrays .asList (extension1 , extension2 ));
87
+ initHeaders (this .request .getHeaders ()).setSecWebSocketExtensions (Arrays .asList (extension1 , extension2 ));
98
88
99
89
WebSocketHandler handler = new TextWebSocketHandler ();
100
- Map <String , Object > attributes = Collections .< String , Object > emptyMap ();
90
+ Map <String , Object > attributes = Collections .emptyMap ();
101
91
this .handshakeHandler .doHandshake (this .request , this .response , handler , attributes );
102
92
103
93
verify (this .upgradeStrategy ).upgrade (this .request , this .response , null ,
@@ -109,16 +99,10 @@ public void subProtocolCapableHandler() {
109
99
given (this .upgradeStrategy .getSupportedVersions ()).willReturn (new String [] {"13" });
110
100
111
101
this .servletRequest .setMethod ("GET" );
112
-
113
- WebSocketHttpHeaders headers = new WebSocketHttpHeaders (this .request .getHeaders ());
114
- headers .setUpgrade ("WebSocket" );
115
- headers .setConnection ("Upgrade" );
116
- headers .setSecWebSocketVersion ("13" );
117
- headers .setSecWebSocketKey ("82/ZS2YHjEnUN97HLL8tbw==" );
118
- headers .setSecWebSocketProtocol ("v11.stomp" );
102
+ initHeaders (this .request .getHeaders ()).setSecWebSocketProtocol ("v11.stomp" );
119
103
120
104
WebSocketHandler handler = new SubProtocolCapableHandler ("v12.stomp" , "v11.stomp" );
121
- Map <String , Object > attributes = Collections .< String , Object > emptyMap ();
105
+ Map <String , Object > attributes = Collections .emptyMap ();
122
106
this .handshakeHandler .doHandshake (this .request , this .response , handler , attributes );
123
107
124
108
verify (this .upgradeStrategy ).upgrade (this .request , this .response , "v11.stomp" ,
@@ -130,22 +114,25 @@ public void subProtocolCapableHandlerNoMatch() {
130
114
given (this .upgradeStrategy .getSupportedVersions ()).willReturn (new String [] {"13" });
131
115
132
116
this .servletRequest .setMethod ("GET" );
133
-
134
- WebSocketHttpHeaders headers = new WebSocketHttpHeaders (this .request .getHeaders ());
135
- headers .setUpgrade ("WebSocket" );
136
- headers .setConnection ("Upgrade" );
137
- headers .setSecWebSocketVersion ("13" );
138
- headers .setSecWebSocketKey ("82/ZS2YHjEnUN97HLL8tbw==" );
139
- headers .setSecWebSocketProtocol ("v10.stomp" );
117
+ initHeaders (this .request .getHeaders ()).setSecWebSocketProtocol ("v10.stomp" );
140
118
141
119
WebSocketHandler handler = new SubProtocolCapableHandler ("v12.stomp" , "v11.stomp" );
142
- Map <String , Object > attributes = Collections .< String , Object > emptyMap ();
120
+ Map <String , Object > attributes = Collections .emptyMap ();
143
121
this .handshakeHandler .doHandshake (this .request , this .response , handler , attributes );
144
122
145
123
verify (this .upgradeStrategy ).upgrade (this .request , this .response , null ,
146
124
Collections .emptyList (), null , handler , attributes );
147
125
}
148
126
127
+ private WebSocketHttpHeaders initHeaders (HttpHeaders httpHeaders ) {
128
+ WebSocketHttpHeaders headers = new WebSocketHttpHeaders (httpHeaders );
129
+ headers .setUpgrade ("WebSocket" );
130
+ headers .setConnection ("Upgrade" );
131
+ headers .setSecWebSocketVersion ("13" );
132
+ headers .setSecWebSocketKey ("82/ZS2YHjEnUN97HLL8tbw==" );
133
+ return headers ;
134
+ }
135
+
149
136
150
137
private static class SubProtocolCapableHandler extends TextWebSocketHandler implements SubProtocolCapable {
151
138
0 commit comments