Skip to content

Commit 52eccc8

Browse files
committed
fix bad tests
update some URLs but mostly this means @Ignore-ing the dead external websites (...this codes needs to run its own http(s) server for testing...) also restored the single-arg EncoderRegistry.encodeForm(Map) which was removed by jgritman#32 rkaw:PUT_POST_ContentType when they said they were just adding the 2-arg version
1 parent 77a526c commit 52eccc8

7 files changed

Lines changed: 80 additions & 55 deletions

File tree

src/main/java/groovyx/net/http/EncoderRegistry.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ else if ( data instanceof Reader && ! (data instanceof BufferedReader) )
204204
* @return an {@link HttpEntity} encapsulating this request data
205205
* @throws UnsupportedEncodingException
206206
*/
207+
public UrlEncodedFormEntity encodeForm( Map<?,?> params )
208+
throws UnsupportedEncodingException {
209+
return encodeForm( params, null );
210+
}
207211

208212
public UrlEncodedFormEntity encodeForm( Map<?,?> params, Object contentType )
209213
throws UnsupportedEncodingException {

src/test/groovy/groovyx/net/http/AsyncHTTPBuilderTest.groovy

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public class AsyncHTTPBuilderTest {
7878
@Ignore
7979
@Test public void testDefaultConstructor() {
8080
def http = new AsyncHTTPBuilder()
81-
def resp = http.get( uri:'http://ajax.googleapis.com',
81+
def resp = http.get( uri:'https://ajax.googleapis.com',
8282
path : '/ajax/services/search/web',
8383
query : [ v:'1.0', q: 'Calvin and Hobbes' ],
8484
contentType: JSON )
@@ -89,6 +89,7 @@ public class AsyncHTTPBuilderTest {
8989
http.shutdown()
9090
}
9191

92+
@Ignore // requires auth; 404 entire site
9293
@Test public void testPostAndDelete() {
9394
def http = new AsyncHTTPBuilder(uri:'https://api.twitter.com/1.1/statuses/')
9495

@@ -126,6 +127,7 @@ public class AsyncHTTPBuilderTest {
126127
}
127128

128129

130+
@Ignore // 404
129131
@Test public void testTimeout() {
130132
def http = new AsyncHTTPBuilder( uri:'http://netflix.com',
131133
contentType: HTML, timeout:2 ) // 2ms to force timeout
@@ -145,9 +147,10 @@ public class AsyncHTTPBuilderTest {
145147
}
146148
}
147149

150+
@Ignore // 404 dead service
148151
@Test public void testPoolsizeAndQueueing() {
149152
def http = new AsyncHTTPBuilder( poolSize : 1 ,
150-
uri : 'http://ajax.googleapis.com/ajax/services/search/web' )
153+
uri : 'https://ajax.googleapis.com/ajax/services/search/web' )
151154

152155
def responses = []
153156
/* With one thread in the pool, responses will be sequential but should
@@ -172,10 +175,10 @@ public class AsyncHTTPBuilderTest {
172175
@Test public void testInvalidNamedArg() {
173176
try {
174177
def http = new AsyncHTTPBuilder( poolsize : 1 ,
175-
uri : 'http://ajax.googleapis.com/ajax/services/search/web' )
178+
uri : 'https://ajax.googleapis.com/ajax/services/search/web' )
176179
throw new AssertionError("request should have failed due to invalid kwarg.")
177180
}
178181
catch ( IllegalArgumentException ex ) { /* Expected result */ }
179182
}
180183

181-
}
184+
}

src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ class HTTPBuilderTest {
121121
* based on the given content-type, i.e. TEXT (text/plain).
122122
*/
123123
@Test public void testReader() {
124-
def http = new HTTPBuilder('http://examples.oreilly.com')
125-
http.get( uri: 'http://examples.oreilly.com/9780596002527/examples/first.xml',
124+
def http = new HTTPBuilder('https://resources.oreilly.com')
125+
http.get( uri:'https://resources.oreilly.com/examples/9780596002527/raw/master/examples/first.xml',
126126
contentType: TEXT, headers: [Accept:'*/*'] ) { resp, reader ->
127127
println "response status: ${resp.statusLine}"
128128
println 'Headers:'
@@ -145,7 +145,7 @@ class HTTPBuilderTest {
145145
/* REST testing with Twitter!
146146
* Tests POST with JSON response, and DELETE with a JSON response.
147147
*/
148-
148+
@Ignore // requires auth; 404 entire site
149149
@Test public void testPOST() {
150150
def http = new HTTPBuilder('https://api.twitter.com/1.1/statuses/')
151151

@@ -204,8 +204,8 @@ class HTTPBuilderTest {
204204
}
205205
}
206206

207-
// @Test
208-
public void testHeadMethod() {
207+
@Ignore // requires auth; 404 entire site
208+
@Test public void testHeadMethod() {
209209
def http = new HTTPBuilder('https://api.twitter.com/1.1/statuses/')
210210

211211
http.auth.oauth twitter.consumerKey, twitter.consumerSecret,
@@ -307,6 +307,8 @@ class HTTPBuilderTest {
307307
http.auth.basic( 'user2', 'user2' )
308308

309309
http.request( GET, HTML ) {
310+
response.'403' = { "expected bad auth" }
311+
response.success = { throw new AssertionError("request should have failed.") }
310312
uri.path = '/auth-digest/'
311313
}
312314

@@ -315,32 +317,33 @@ class HTTPBuilderTest {
315317
}
316318
}
317319

320+
@Ignore // requires auth
318321
@Test public void testCatalog() {
319-
def http = new HTTPBuilder( 'http://weather.yahooapis.com/forecastrss' )
320-
322+
def http = new HTTPBuilder( 'https://weather-ydn-yql.media.yahoo.com/forecastrss' )
321323
http.parser.addCatalog getClass().getResource( '/rss-catalog.xml')
322324
def xml = http.get( query : [p:'02110',u:'f'] )
323-
324-
325325
}
326326

327+
@Ignore // requires auth
327328
@Test public void testInvalidNamedArg() {
328-
def http = new HTTPBuilder( 'http://weather.yahooapis.com/forecastrss' )
329+
def http = new HTTPBuilder( 'https://weather-ydn-yql.media.yahoo.com/forecastrss' )
329330
try {
330331
def xml = http.get( query : [p:'02110',u:'f'], blah : 'asdf' )
331332
throw new AssertionError("request should have failed due to invalid kwarg.")
332333
}
333334
catch ( IllegalArgumentException ex ) { /* Expected result */ }
334335
}
335336

337+
@Ignore // requires auth
336338
@Test(expected = IllegalArgumentException)
337339
public void testShouldThrowExceptionIfContentTypeIsNotSet() {
338-
new HTTPBuilder( 'http://weather.yahooapis.com/forecastrss' ).request(POST) { request ->
340+
new HTTPBuilder( 'https://weather-ydn-yql.media.yahoo.com/forecastrss' ).request(POST) { request ->
339341
body = [p:'02110',u:'f']
340342
}
341343
fail("request should have failed due to unset content type.")
342344
}
343345

346+
@Ignore // 500
344347
@Test
345348
public void testUrlencRequestContentType() {
346349
def http = new HTTPBuilder('http://restmirror.appspot.com/')
@@ -354,10 +357,11 @@ class HTTPBuilderTest {
354357
assert resp.statusLine.statusCode == 201
355358
}
356359
}
357-
}
360+
}
358361

362+
@Ignore // 500
359363
@Test public void testJSONPost() {
360-
def builder = new HTTPBuilder("http://restmirror.appspot.com/")
364+
def builder = new HTTPBuilder("http://restmirror.appspot.com/")
361365
def result = builder.request(POST, JSON) { req ->
362366
body = [name: 'bob', title: 'construction worker']
363367

@@ -374,4 +378,5 @@ class HTTPBuilderTest {
374378
}
375379
assert result == 'bob'
376380
}
381+
377382
}

src/test/groovy/groovyx/net/http/HttpURLClientTest.groovy

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class HttpURLClientTest {
1919
* This method will parse the content based on the response content-type
2020
*/
2121
@Test public void testGET() {
22-
def http = new HttpURLClient(url:'http://www.google.com')
22+
def http = new HttpURLClient(url:'https://www.google.com')
2323
def resp = http.request( path:'/search', query:[q:'HTTPBuilder'],
2424
headers:['User-Agent':'Firefox'] )
2525

@@ -52,7 +52,7 @@ class HttpURLClientTest {
5252
}
5353

5454
@Test public void testSetHeaders() {
55-
def http = new HttpURLClient(url:'http://www.google.com')
55+
def http = new HttpURLClient(url:'https://www.google.com')
5656
def val = '1'
5757
def v2 = 'two'
5858
def h3 = 'three'
@@ -73,7 +73,7 @@ class HttpURLClientTest {
7373

7474

7575
@Test public void testFailure() {
76-
def http = new HttpURLClient(url:'http://www.google.com')
76+
def http = new HttpURLClient(url:'https://www.google.com')
7777

7878
try {
7979
def resp = http.request( path:'/adsasf/kjsslkd' )
@@ -83,7 +83,7 @@ class HttpURLClientTest {
8383
assert ! ex.response.success
8484
assert ex.response.headers.every { it.name && it.value }
8585
}
86-
assert http.url.toString() == 'http://www.google.com'
86+
assert http.url.toString() == 'https://www.google.com'
8787
}
8888

8989
/**
@@ -92,8 +92,13 @@ class HttpURLClientTest {
9292
*/
9393
@Test public void testReader() {
9494
def http = new HttpURLClient()
95-
def resp = http.request( url:'http://validator.w3.org/about.html',
96-
contentType: TEXT, headers: [Accept:'*/*'] )
95+
def resp = http.request( url:
96+
//'http://validator.w3.org/about.html' // fails validation by twice using "&ouml;": The entity "ouml" was referenced, but not declared.
97+
//'http://validator.w3.org/docs/help.html' // fails &ouml; &mdash;
98+
//'https://validator.w3.org/nu/about.html'
99+
'https://www.w3schools.com/xml/cd_catalog.xml'
100+
,
101+
contentType: TEXT, headers: [Accept:'*/*'] )
97102

98103
println "response status: ${resp.statusLine}"
99104

@@ -108,20 +113,21 @@ class HttpURLClientTest {
108113

109114
/** W3C pages will have a doctype, but will return a 503 if you do a GET
110115
* for them with the Java User-Agent.
116+
* ...and they all fail validation (see above)
111117
*/
112118
@Test public void testCatalog() {
113119
def http = new HttpURLClient(
114-
url:'http://validator.w3.org/',
120+
url:'https://www.w3schools.com',
115121
contentType: XML )
116122

117-
def resp = http.request( path : 'about.html' )
123+
def resp = http.request( path : '/xml/note.xml' )
118124
assert resp.data
119125
}
120126

121127
/* REST testing with Twitter!
122128
* Tests POST with XML response, and DELETE with a JSON response.
123129
*/
124-
130+
@Ignore // requires auth; 404 entire site
125131
@Test public void testPOST() {
126132
def http = new HttpURLClient(url:'https://api.twitter.com/1.1/statuses/')
127133

@@ -153,8 +159,8 @@ class HttpURLClientTest {
153159
println "Test tweet ID ${json.id} was deleted."
154160
}
155161

156-
// @Test
157-
public void testHeadMethod() {
162+
@Ignore // requires auth; 404 entire site
163+
@Test public void testHeadMethod() {
158164
def http = new HttpURLClient(url:'http://api.twitter.com/1/statuses/')
159165

160166
assert http.url.toString() == "http://api.twitter.com/1/statuses/"
@@ -168,6 +174,7 @@ class HttpURLClientTest {
168174
assert resp.headers.Status == "200 OK"
169175
}
170176

177+
@Ignore // requires auth; 404 entire site
171178
@Test public void testParsers() {
172179
def parsers = new ParserRegistry()
173180
def done = false
@@ -193,15 +200,15 @@ class HttpURLClientTest {
193200
assert resp.data
194201
}
195202

196-
/* http://googlesystem.blogspot.com/2008/04/google-search-rest-api.html
197-
* http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Earth%20Day
203+
/* https://googlesystem.blogspot.com/2008/04/google-search-rest-api.html
204+
* https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Earth%20Day
198205
*/
199206
@Ignore
200207
@Test public void testJSON() {
201208

202209
def http = new HttpURLClient()
203210

204-
def resp = http.request( url:'http://ajax.googleapis.com',
211+
def resp = http.request( url:'https://ajax.googleapis.com',
205212
method:GET, contentType:JSON ,
206213
path : '/ajax/services/search/web',
207214
query : [ v:'1.0', q: 'Calvin and Hobbes' ],
@@ -222,7 +229,7 @@ class HttpURLClientTest {
222229
def http = new HttpURLClient()
223230

224231
try {
225-
def resp = http.request( url:'http://ajax.googleapis.com',
232+
def resp = http.request( url:'https://ajax.googleapis.com',
226233
method:GET, contentType:JSON ,
227234
Path : '/ajax/services/search/web',
228235
query : [ v:'1.0', q: 'Calvin and Hobbes' ] )
@@ -233,6 +240,6 @@ class HttpURLClientTest {
233240

234241
@Test(expected = SocketTimeoutException)
235242
void testTimeout() {
236-
new HttpURLClient(url: 'https://www.google.com/').request(timeout: 1)
243+
new HttpURLClient(url: 'https://groovy-lang.org/').request(timeout: 1)
237244
}
238245
}

src/test/groovy/groovyx/net/http/RESTClientTest.groovy renamed to src/test/groovy/groovyx/net/http/RESTClientTest.groovy.dead-sites

File renamed without changes.

src/test/groovy/groovyx/net/http/RegistryTest.groovy

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ package groovyx.net.http
33
import org.apache.http.ProtocolVersion
44
import org.apache.http.entity.StringEntity
55
import org.apache.http.message.BasicHttpResponse
6-
import org.junit.Testimport java.io.StringReaderimport java.io.ByteArrayInputStream
7-
import static groovyx.net.http.ContentType.*
6+
import org.junit.Test
7+
import java.io.StringReader
8+
import java.io.ByteArrayInputStream
9+
import static groovyx.net.http.ContentType.*
10+
811
/**
912
* @author tnichols
1013
*/

0 commit comments

Comments
 (0)