2525import com .google .common .collect .ImmutableMap ;
2626import com .google .gson .Gson ;
2727import com .google .gson .reflect .TypeToken ;
28-
28+ import java .io .IOException ;
29+ import java .util .Map ;
2930import org .junit .Test ;
31+ import org .junit .runner .RunWith ;
32+ import org .junit .runners .JUnit4 ;
3033import org .openqa .selenium .remote .http .HttpClient ;
3134import org .openqa .selenium .remote .http .HttpRequest ;
3235import org .openqa .selenium .remote .http .HttpResponse ;
3336
34- import java .io .IOException ;
35- import java .util .Map ;
36-
3737@ SuppressWarnings ("unchecked" )
38+ @ RunWith (JUnit4 .class )
3839public class ProtocolHandshakeTest {
3940
4041 @ Test
41- public void shouldIncludeJsonWireProtocolCapabilities () throws IOException {
42+ public void requestShouldIncludeJsonWireProtocolCapabilities () throws IOException {
4243 Map <String , Object > params = ImmutableMap .of ("desiredCapabilities" , new DesiredCapabilities ());
4344 Command command = new Command (null , DriverCommand .NEW_SESSION , params );
4445
4546 HttpResponse response = new HttpResponse ();
4647 response .setStatus (HTTP_OK );
47- response .setContent ("{\" value\" : {\" sessionId\" : \" 23456789\" , \" value\" : {}}}" .getBytes (UTF_8 ));
48+ response .setContent (
49+ "{\" value\" : {\" sessionId\" : \" 23456789\" , \" capabilities\" : {}}}" .getBytes (UTF_8 ));
4850 RecordingHttpClient client = new RecordingHttpClient (response );
4951
5052 new ProtocolHandshake ().createSession (client , command );
@@ -58,13 +60,14 @@ public void shouldIncludeJsonWireProtocolCapabilities() throws IOException {
5860 }
5961
6062 @ Test
61- public void shouldIncludeOlderGeckoDriverCapabilities () throws IOException {
63+ public void requestShouldIncludeOlderGeckoDriverCapabilities () throws IOException {
6264 Map <String , Object > params = ImmutableMap .of ("desiredCapabilities" , new DesiredCapabilities ());
6365 Command command = new Command (null , DriverCommand .NEW_SESSION , params );
6466
6567 HttpResponse response = new HttpResponse ();
6668 response .setStatus (HTTP_OK );
67- response .setContent ("{\" value\" : {\" sessionId\" : \" 23456789\" , \" value\" : {}}}" .getBytes (UTF_8 ));
69+ response .setContent (
70+ "{\" value\" : {\" sessionId\" : \" 23456789\" , \" capabilities\" : {}}}" .getBytes (UTF_8 ));
6871 RecordingHttpClient client = new RecordingHttpClient (response );
6972
7073 new ProtocolHandshake ().createSession (client , command );
@@ -79,13 +82,14 @@ public void shouldIncludeOlderGeckoDriverCapabilities() throws IOException {
7982 }
8083
8184 @ Test
82- public void shouldIncludeSpecCompliantW3CCapabilities () throws IOException {
85+ public void requestShouldIncludeSpecCompliantW3CCapabilities () throws IOException {
8386 Map <String , Object > params = ImmutableMap .of ("desiredCapabilities" , new DesiredCapabilities ());
8487 Command command = new Command (null , DriverCommand .NEW_SESSION , params );
8588
8689 HttpResponse response = new HttpResponse ();
8790 response .setStatus (HTTP_OK );
88- response .setContent ("{\" value\" : {\" sessionId\" : \" 23456789\" , \" value\" : {}}}" .getBytes (UTF_8 ));
91+ response .setContent (
92+ "{\" value\" : {\" sessionId\" : \" 23456789\" , \" capabilities\" : {}}}" .getBytes (UTF_8 ));
8993 RecordingHttpClient client = new RecordingHttpClient (response );
9094
9195 new ProtocolHandshake ().createSession (client , command );
@@ -98,6 +102,54 @@ public void shouldIncludeSpecCompliantW3CCapabilities() throws IOException {
98102 assertEquals (ImmutableList .of (), json .get ("firstMatch" ));
99103 }
100104
105+ @ Test
106+ public void shouldParseW3CNewSessionResponse () throws IOException {
107+ Map <String , Object > params = ImmutableMap .of ("desiredCapabilities" , new DesiredCapabilities ());
108+ Command command = new Command (null , DriverCommand .NEW_SESSION , params );
109+
110+ HttpResponse response = new HttpResponse ();
111+ response .setStatus (HTTP_OK );
112+ response .setContent (
113+ "{\" value\" : {\" sessionId\" : \" 23456789\" , \" capabilities\" : {}}}" .getBytes (UTF_8 ));
114+ RecordingHttpClient client = new RecordingHttpClient (response );
115+
116+ ProtocolHandshake .Result result = new ProtocolHandshake ().createSession (client , command );
117+ assertEquals (result .getDialect (), Dialect .W3C );
118+ }
119+
120+ @ Test
121+ public void shouldParseOlderW3CNewSessionResponse () throws IOException {
122+ Map <String , Object > params = ImmutableMap .of ("desiredCapabilities" , new DesiredCapabilities ());
123+ Command command = new Command (null , DriverCommand .NEW_SESSION , params );
124+
125+ HttpResponse response = new HttpResponse ();
126+ response .setStatus (HTTP_OK );
127+ // Some drivers (e.g., GeckoDriver 0.15.0) return the capabilities in a key named "value",
128+ // rather than "capabilities"; essentially this is the old Wire Protocol format, wrapped in a
129+ // "value" key.
130+ response .setContent (
131+ "{\" value\" : {\" sessionId\" : \" 23456789\" , \" value\" : {}}}" .getBytes (UTF_8 ));
132+ RecordingHttpClient client = new RecordingHttpClient (response );
133+
134+ ProtocolHandshake .Result result = new ProtocolHandshake ().createSession (client , command );
135+ assertEquals (result .getDialect (), Dialect .W3C );
136+ }
137+
138+ @ Test
139+ public void shouldParseWireProtocolNewSessionResponse () throws IOException {
140+ Map <String , Object > params = ImmutableMap .of ("desiredCapabilities" , new DesiredCapabilities ());
141+ Command command = new Command (null , DriverCommand .NEW_SESSION , params );
142+
143+ HttpResponse response = new HttpResponse ();
144+ response .setStatus (HTTP_OK );
145+ response .setContent (
146+ "{\" sessionId\" : \" 23456789\" , \" status\" : 0, \" value\" : {}}" .getBytes (UTF_8 ));
147+ RecordingHttpClient client = new RecordingHttpClient (response );
148+
149+ ProtocolHandshake .Result result = new ProtocolHandshake ().createSession (client , command );
150+ assertEquals (result .getDialect (), Dialect .OSS );
151+ }
152+
101153 class RecordingHttpClient implements HttpClient {
102154
103155 private final HttpResponse response ;
@@ -123,4 +175,4 @@ public HttpRequest getRequest() {
123175 return request ;
124176 }
125177 }
126- }
178+ }
0 commit comments