1
+ // Licensed to the Software Freedom Conservancy (SFC) under one
2
+ // or more contributor license agreements. See the NOTICE file
3
+ // distributed with this work for additional information
4
+ // regarding copyright ownership. The SFC licenses this file
5
+ // to you under the Apache License, Version 2.0 (the
6
+ // "License"); you may not use this file except in compliance
7
+ // with the License. You may obtain a copy of the License at
8
+ //
9
+ // http://www.apache.org/licenses/LICENSE-2.0
10
+ //
11
+ // Unless required by applicable law or agreed to in writing,
12
+ // software distributed under the License is distributed on an
13
+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
+ // KIND, either express or implied. See the License for the
15
+ // specific language governing permissions and limitations
16
+ // under the License.
17
+
18
+ package org .openqa .selenium .remote .http ;
19
+
20
+ import static java .net .HttpURLConnection .HTTP_INTERNAL_ERROR ;
21
+ import static java .net .HttpURLConnection .HTTP_OK ;
22
+ import static java .nio .charset .StandardCharsets .UTF_8 ;
23
+ import static org .junit .Assert .assertEquals ;
24
+ import static org .junit .Assert .assertTrue ;
25
+ import static org .openqa .selenium .remote .ErrorCodes .METHOD_NOT_ALLOWED ;
26
+
27
+ import com .google .gson .Gson ;
28
+
29
+ import org .junit .Test ;
30
+ import org .openqa .selenium .UnsupportedCommandException ;
31
+ import org .openqa .selenium .WebDriverException ;
32
+ import org .openqa .selenium .remote .ErrorCodes ;
33
+ import org .openqa .selenium .remote .Response ;
34
+
35
+ import java .util .HashMap ;
36
+ import java .util .Map ;
37
+
38
+ public class W3CHttpResponseCodecTest {
39
+
40
+ @ Test
41
+ public void noErrorNoCry () {
42
+ Map <String , Object > data = new HashMap <>();
43
+ data .put ("value" , "cheese" );
44
+
45
+ HttpResponse response = createValidResponse (HTTP_OK , data );
46
+
47
+ Response decoded = new W3CHttpResponseCodec ().decode (response );
48
+
49
+ assertEquals (ErrorCodes .SUCCESS , decoded .getStatus ().intValue ());
50
+ assertEquals ("success" , decoded .getState ());
51
+ assertEquals ("cheese" , decoded .getValue ());
52
+ }
53
+
54
+ @ Test
55
+ public void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForNonCompliantImplementations () {
56
+ Map <String , Object > error = new HashMap <>();
57
+ error .put ("error" , "unsupported operation" ); // 500
58
+ error .put ("message" , "I like peas" );
59
+ error .put ("stacktrace" , "" );
60
+
61
+ HttpResponse response = createValidResponse (HTTP_INTERNAL_ERROR , error );
62
+
63
+ Response decoded = new W3CHttpResponseCodec ().decode (response );
64
+
65
+ assertEquals ("unsupported operation" , decoded .getState ());
66
+ assertEquals (METHOD_NOT_ALLOWED , decoded .getStatus ().intValue ());
67
+
68
+ assertTrue (decoded .getValue () instanceof UnsupportedCommandException );
69
+ assertTrue (((WebDriverException ) decoded .getValue ()).getMessage ().contains ("I like peas" ));
70
+ }
71
+
72
+ @ Test
73
+ public void decodingAnErrorWithoutAStacktraceIsDecodedProperlyForConformingImplementations () {
74
+ Map <String , Object > error = new HashMap <>();
75
+ error .put ("error" , "unsupported operation" ); // 500
76
+ error .put ("message" , "I like peas" );
77
+ error .put ("stacktrace" , "" );
78
+ Map <String , Object > data = new HashMap <>();
79
+ data .put ("value" , error );
80
+
81
+ HttpResponse response = createValidResponse (HTTP_INTERNAL_ERROR , data );
82
+
83
+ Response decoded = new W3CHttpResponseCodec ().decode (response );
84
+
85
+ assertEquals ("unsupported operation" , decoded .getState ());
86
+ assertEquals (METHOD_NOT_ALLOWED , decoded .getStatus ().intValue ());
87
+
88
+ assertTrue (decoded .getValue () instanceof UnsupportedCommandException );
89
+ assertTrue (((WebDriverException ) decoded .getValue ()).getMessage ().contains ("I like peas" ));
90
+ }
91
+
92
+ private HttpResponse createValidResponse (int statusCode , Map <String , ?> data ) {
93
+ byte [] contents = new Gson ().toJson (data ).getBytes (UTF_8 );
94
+
95
+ HttpResponse response = new HttpResponse ();
96
+ response .setStatus (statusCode );
97
+ response .addHeader ("Content-Type" , "application/json; charset=utf-8" );
98
+ response .addHeader ("Cache-Control" , "no-cache" );
99
+ response .addHeader ("Content-Length" , String .valueOf (contents .length ));
100
+ response .setContent (contents );
101
+
102
+ return response ;
103
+ }
104
+ }
0 commit comments