9
9
import java .io .IOException ;
10
10
import java .io .InputStream ;
11
11
import java .net .HttpURLConnection ;
12
+ import java .net .ProtocolException ;
12
13
import java .nio .charset .StandardCharsets ;
13
14
import java .util .Date ;
14
15
import java .util .Map ;
@@ -102,13 +103,25 @@ public void onError(IOException e, HttpURLConnection uc) throws IOException {
102
103
103
104
Assert .assertThrows (IllegalStateException .class , () -> uc .getRequestProperties ());
104
105
105
- // disconnect does nothing, never throws
106
- uc .disconnect ();
107
- uc .disconnect ();
108
-
109
- if (uc instanceof GitHubConnectorResponseHttpUrlConnectionAdapter ) {
110
- Assert .assertThrows (UnsupportedOperationException .class , () -> uc .connect ());
106
+ // Actions that are not allowed because connection already opened.
107
+ Assert .assertThrows (IllegalStateException .class , () -> uc .addRequestProperty ("bogus" , "item" ));
108
+
109
+ Assert .assertThrows (IllegalStateException .class , () -> uc .setAllowUserInteraction (true ));
110
+ Assert .assertThrows (IllegalStateException .class , () -> uc .setChunkedStreamingMode (1 ));
111
+ Assert .assertThrows (IllegalStateException .class , () -> uc .setDoInput (true ));
112
+ Assert .assertThrows (IllegalStateException .class , () -> uc .setDoOutput (true ));
113
+ Assert .assertThrows (IllegalStateException .class , () -> uc .setFixedLengthStreamingMode (1 ));
114
+ Assert .assertThrows (IllegalStateException .class , () -> uc .setFixedLengthStreamingMode (1L ));
115
+ Assert .assertThrows (IllegalStateException .class , () -> uc .setIfModifiedSince (1L ));
116
+ Assert .assertThrows (IllegalStateException .class , () -> uc .setRequestProperty ("bogus" , "thing" ));
117
+ Assert .assertThrows (IllegalStateException .class , () -> uc .setUseCaches (true ));
118
+
119
+ boolean isAdapter = false ;
120
+ if (uc .toString ().contains ("GitHubConnectorResponseHttpUrlConnectionAdapter" )) {
121
+ isAdapter = true ;
122
+ }
111
123
124
+ if (isAdapter ) {
112
125
Assert .assertThrows (UnsupportedOperationException .class ,
113
126
() -> uc .getAllowUserInteraction ());
114
127
Assert .assertThrows (UnsupportedOperationException .class , () -> uc .getConnectTimeout ());
@@ -125,32 +138,33 @@ public void onError(IOException e, HttpURLConnection uc) throws IOException {
125
138
Assert .assertThrows (UnsupportedOperationException .class , () -> uc .getUseCaches ());
126
139
Assert .assertThrows (UnsupportedOperationException .class , () -> uc .usingProxy ());
127
140
128
- Assert .assertThrows (UnsupportedOperationException .class ,
129
- () -> uc .addRequestProperty ("bogus" , "item" ));
130
- Assert .assertThrows (UnsupportedOperationException .class ,
131
- () -> uc .setAllowUserInteraction (true ));
132
- Assert .assertThrows (UnsupportedOperationException .class ,
133
- () -> uc .setChunkedStreamingMode (1 ));
134
141
Assert .assertThrows (UnsupportedOperationException .class , () -> uc .setConnectTimeout (10 ));
135
142
Assert .assertThrows (UnsupportedOperationException .class ,
136
143
() -> uc .setDefaultUseCaches (true ));
137
- Assert .assertThrows (UnsupportedOperationException .class , () -> uc .setDoInput (true ));
138
- Assert .assertThrows (UnsupportedOperationException .class , () -> uc .setDoOutput (true ));
139
- Assert .assertThrows (UnsupportedOperationException .class ,
140
- () -> uc .setFixedLengthStreamingMode (1 ));
141
- Assert .assertThrows (UnsupportedOperationException .class ,
142
- () -> uc .setFixedLengthStreamingMode (1L ));
143
- Assert .assertThrows (UnsupportedOperationException .class , () -> uc .setIfModifiedSince (1L ));
144
+
144
145
Assert .assertThrows (UnsupportedOperationException .class ,
145
146
() -> uc .setInstanceFollowRedirects (true ));
146
147
Assert .assertThrows (UnsupportedOperationException .class , () -> uc .setReadTimeout (10 ));
147
- Assert .assertThrows (UnsupportedOperationException .class , () -> uc .setRequestMethod ("GET" ));
148
- Assert .assertThrows (UnsupportedOperationException .class ,
149
- () -> uc .setRequestProperty ("bogus" , "thing" ));
150
- Assert .assertThrows (UnsupportedOperationException .class , () -> uc .setUseCaches (true ));
148
+ Assert .assertThrows (ProtocolException .class , () -> uc .setRequestMethod ("GET" ));
149
+ } else {
150
+ uc .getDefaultUseCaches ();
151
+ assertThat (uc .getDoInput (), is (true ));
152
+
153
+ // Depending on the underlying implementation, this may throw or not
154
+ // Assert.assertThrows(IllegalStateException.class, () -> uc.setRequestMethod("GET"));
151
155
}
152
156
153
- RateLimitHandler .FAIL .onError (e , uc );
157
+ // ignored
158
+ uc .connect ();
159
+
160
+ // disconnect does nothing, never throws
161
+ uc .disconnect ();
162
+ uc .disconnect ();
163
+
164
+ // ignored
165
+ uc .connect ();
166
+
167
+ AbuseLimitHandler .FAIL .onError (e , uc );
154
168
}
155
169
})
156
170
.build ();
0 commit comments