From 52eccc8e2dcfaf167dfc0740e8ebf7ec50518185 Mon Sep 17 00:00:00 2001 From: "John J. Allison" Date: Mon, 10 Aug 2020 15:02:01 -0600 Subject: [PATCH] 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 #32 rkaw:PUT_POST_ContentType when they said they were just adding the 2-arg version --- .../groovyx/net/http/EncoderRegistry.java | 4 ++ .../net/http/AsyncHTTPBuilderTest.groovy | 11 +++-- .../groovyx/net/http/HTTPBuilderTest.groovy | 31 +++++++------ .../groovyx/net/http/HttpURLClientTest.groovy | 39 ++++++++++------- ...roovy => RESTClientTest.groovy.dead-sites} | 0 .../groovyx/net/http/RegistryTest.groovy | 7 ++- .../net/http/thirdparty/GAETest.groovy | 43 ++++++++++--------- 7 files changed, 80 insertions(+), 55 deletions(-) rename src/test/groovy/groovyx/net/http/{RESTClientTest.groovy => RESTClientTest.groovy.dead-sites} (100%) diff --git a/src/main/java/groovyx/net/http/EncoderRegistry.java b/src/main/java/groovyx/net/http/EncoderRegistry.java index 1dda722..46b61b4 100755 --- a/src/main/java/groovyx/net/http/EncoderRegistry.java +++ b/src/main/java/groovyx/net/http/EncoderRegistry.java @@ -204,6 +204,10 @@ else if ( data instanceof Reader && ! (data instanceof BufferedReader) ) * @return an {@link HttpEntity} encapsulating this request data * @throws UnsupportedEncodingException */ + public UrlEncodedFormEntity encodeForm( Map params ) + throws UnsupportedEncodingException { + return encodeForm( params, null ); + } public UrlEncodedFormEntity encodeForm( Map params, Object contentType ) throws UnsupportedEncodingException { diff --git a/src/test/groovy/groovyx/net/http/AsyncHTTPBuilderTest.groovy b/src/test/groovy/groovyx/net/http/AsyncHTTPBuilderTest.groovy index 86662ae..520f37a 100644 --- a/src/test/groovy/groovyx/net/http/AsyncHTTPBuilderTest.groovy +++ b/src/test/groovy/groovyx/net/http/AsyncHTTPBuilderTest.groovy @@ -78,7 +78,7 @@ public class AsyncHTTPBuilderTest { @Ignore @Test public void testDefaultConstructor() { def http = new AsyncHTTPBuilder() - def resp = http.get( uri:'http://ajax.googleapis.com', + def resp = http.get( uri:'https://ajax.googleapis.com', path : '/ajax/services/search/web', query : [ v:'1.0', q: 'Calvin and Hobbes' ], contentType: JSON ) @@ -89,6 +89,7 @@ public class AsyncHTTPBuilderTest { http.shutdown() } + @Ignore // requires auth; 404 entire site @Test public void testPostAndDelete() { def http = new AsyncHTTPBuilder(uri:'https://api.twitter.com/1.1/statuses/') @@ -126,6 +127,7 @@ public class AsyncHTTPBuilderTest { } + @Ignore // 404 @Test public void testTimeout() { def http = new AsyncHTTPBuilder( uri:'http://netflix.com', contentType: HTML, timeout:2 ) // 2ms to force timeout @@ -145,9 +147,10 @@ public class AsyncHTTPBuilderTest { } } + @Ignore // 404 dead service @Test public void testPoolsizeAndQueueing() { def http = new AsyncHTTPBuilder( poolSize : 1 , - uri : 'http://ajax.googleapis.com/ajax/services/search/web' ) + uri : 'https://ajax.googleapis.com/ajax/services/search/web' ) def responses = [] /* With one thread in the pool, responses will be sequential but should @@ -172,10 +175,10 @@ public class AsyncHTTPBuilderTest { @Test public void testInvalidNamedArg() { try { def http = new AsyncHTTPBuilder( poolsize : 1 , - uri : 'http://ajax.googleapis.com/ajax/services/search/web' ) + uri : 'https://ajax.googleapis.com/ajax/services/search/web' ) throw new AssertionError("request should have failed due to invalid kwarg.") } catch ( IllegalArgumentException ex ) { /* Expected result */ } } -} \ No newline at end of file +} diff --git a/src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy b/src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy index 9ee1e10..a478fbc 100644 --- a/src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy +++ b/src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy @@ -121,8 +121,8 @@ class HTTPBuilderTest { * based on the given content-type, i.e. TEXT (text/plain). */ @Test public void testReader() { - def http = new HTTPBuilder('http://examples.oreilly.com') - http.get( uri: 'http://examples.oreilly.com/9780596002527/examples/first.xml', + def http = new HTTPBuilder('https://resources.oreilly.com') + http.get( uri:'https://resources.oreilly.com/examples/9780596002527/raw/master/examples/first.xml', contentType: TEXT, headers: [Accept:'*/*'] ) { resp, reader -> println "response status: ${resp.statusLine}" println 'Headers:' @@ -145,7 +145,7 @@ class HTTPBuilderTest { /* REST testing with Twitter! * Tests POST with JSON response, and DELETE with a JSON response. */ - + @Ignore // requires auth; 404 entire site @Test public void testPOST() { def http = new HTTPBuilder('https://api.twitter.com/1.1/statuses/') @@ -204,8 +204,8 @@ class HTTPBuilderTest { } } -// @Test - public void testHeadMethod() { + @Ignore // requires auth; 404 entire site + @Test public void testHeadMethod() { def http = new HTTPBuilder('https://api.twitter.com/1.1/statuses/') http.auth.oauth twitter.consumerKey, twitter.consumerSecret, @@ -307,6 +307,8 @@ class HTTPBuilderTest { http.auth.basic( 'user2', 'user2' ) http.request( GET, HTML ) { + response.'403' = { "expected bad auth" } + response.success = { throw new AssertionError("request should have failed.") } uri.path = '/auth-digest/' } @@ -315,17 +317,16 @@ class HTTPBuilderTest { } } + @Ignore // requires auth @Test public void testCatalog() { - def http = new HTTPBuilder( 'http://weather.yahooapis.com/forecastrss' ) - + def http = new HTTPBuilder( 'https://weather-ydn-yql.media.yahoo.com/forecastrss' ) http.parser.addCatalog getClass().getResource( '/rss-catalog.xml') def xml = http.get( query : [p:'02110',u:'f'] ) - - } + @Ignore // requires auth @Test public void testInvalidNamedArg() { - def http = new HTTPBuilder( 'http://weather.yahooapis.com/forecastrss' ) + def http = new HTTPBuilder( 'https://weather-ydn-yql.media.yahoo.com/forecastrss' ) try { def xml = http.get( query : [p:'02110',u:'f'], blah : 'asdf' ) throw new AssertionError("request should have failed due to invalid kwarg.") @@ -333,14 +334,16 @@ class HTTPBuilderTest { catch ( IllegalArgumentException ex ) { /* Expected result */ } } + @Ignore // requires auth @Test(expected = IllegalArgumentException) public void testShouldThrowExceptionIfContentTypeIsNotSet() { - new HTTPBuilder( 'http://weather.yahooapis.com/forecastrss' ).request(POST) { request -> + new HTTPBuilder( 'https://weather-ydn-yql.media.yahoo.com/forecastrss' ).request(POST) { request -> body = [p:'02110',u:'f'] } fail("request should have failed due to unset content type.") } + @Ignore // 500 @Test public void testUrlencRequestContentType() { def http = new HTTPBuilder('http://restmirror.appspot.com/') @@ -354,10 +357,11 @@ class HTTPBuilderTest { assert resp.statusLine.statusCode == 201 } } - } + } + @Ignore // 500 @Test public void testJSONPost() { - def builder = new HTTPBuilder("http://restmirror.appspot.com/") + def builder = new HTTPBuilder("http://restmirror.appspot.com/") def result = builder.request(POST, JSON) { req -> body = [name: 'bob', title: 'construction worker'] @@ -374,4 +378,5 @@ class HTTPBuilderTest { } assert result == 'bob' } + } diff --git a/src/test/groovy/groovyx/net/http/HttpURLClientTest.groovy b/src/test/groovy/groovyx/net/http/HttpURLClientTest.groovy index 000210e..94b4ac2 100755 --- a/src/test/groovy/groovyx/net/http/HttpURLClientTest.groovy +++ b/src/test/groovy/groovyx/net/http/HttpURLClientTest.groovy @@ -19,7 +19,7 @@ class HttpURLClientTest { * This method will parse the content based on the response content-type */ @Test public void testGET() { - def http = new HttpURLClient(url:'http://www.google.com') + def http = new HttpURLClient(url:'https://www.google.com') def resp = http.request( path:'/search', query:[q:'HTTPBuilder'], headers:['User-Agent':'Firefox'] ) @@ -52,7 +52,7 @@ class HttpURLClientTest { } @Test public void testSetHeaders() { - def http = new HttpURLClient(url:'http://www.google.com') + def http = new HttpURLClient(url:'https://www.google.com') def val = '1' def v2 = 'two' def h3 = 'three' @@ -73,7 +73,7 @@ class HttpURLClientTest { @Test public void testFailure() { - def http = new HttpURLClient(url:'http://www.google.com') + def http = new HttpURLClient(url:'https://www.google.com') try { def resp = http.request( path:'/adsasf/kjsslkd' ) @@ -83,7 +83,7 @@ class HttpURLClientTest { assert ! ex.response.success assert ex.response.headers.every { it.name && it.value } } - assert http.url.toString() == 'http://www.google.com' + assert http.url.toString() == 'https://www.google.com' } /** @@ -92,8 +92,13 @@ class HttpURLClientTest { */ @Test public void testReader() { def http = new HttpURLClient() - def resp = http.request( url:'http://validator.w3.org/about.html', - contentType: TEXT, headers: [Accept:'*/*'] ) + def resp = http.request( url: + //'http://validator.w3.org/about.html' // fails validation by twice using "ö": The entity "ouml" was referenced, but not declared. + //'http://validator.w3.org/docs/help.html' // fails ö — + //'https://validator.w3.org/nu/about.html' + 'https://www.w3schools.com/xml/cd_catalog.xml' + , + contentType: TEXT, headers: [Accept:'*/*'] ) println "response status: ${resp.statusLine}" @@ -108,20 +113,21 @@ class HttpURLClientTest { /** W3C pages will have a doctype, but will return a 503 if you do a GET * for them with the Java User-Agent. + * ...and they all fail validation (see above) */ @Test public void testCatalog() { def http = new HttpURLClient( - url:'http://validator.w3.org/', + url:'https://www.w3schools.com', contentType: XML ) - def resp = http.request( path : 'about.html' ) + def resp = http.request( path : '/xml/note.xml' ) assert resp.data } /* REST testing with Twitter! * Tests POST with XML response, and DELETE with a JSON response. */ - + @Ignore // requires auth; 404 entire site @Test public void testPOST() { def http = new HttpURLClient(url:'https://api.twitter.com/1.1/statuses/') @@ -153,8 +159,8 @@ class HttpURLClientTest { println "Test tweet ID ${json.id} was deleted." } -// @Test - public void testHeadMethod() { + @Ignore // requires auth; 404 entire site + @Test public void testHeadMethod() { def http = new HttpURLClient(url:'http://api.twitter.com/1/statuses/') assert http.url.toString() == "http://api.twitter.com/1/statuses/" @@ -168,6 +174,7 @@ class HttpURLClientTest { assert resp.headers.Status == "200 OK" } + @Ignore // requires auth; 404 entire site @Test public void testParsers() { def parsers = new ParserRegistry() def done = false @@ -193,15 +200,15 @@ class HttpURLClientTest { assert resp.data } - /* http://googlesystem.blogspot.com/2008/04/google-search-rest-api.html - * http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Earth%20Day + /* https://googlesystem.blogspot.com/2008/04/google-search-rest-api.html + * https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Earth%20Day */ @Ignore @Test public void testJSON() { def http = new HttpURLClient() - def resp = http.request( url:'http://ajax.googleapis.com', + def resp = http.request( url:'https://ajax.googleapis.com', method:GET, contentType:JSON , path : '/ajax/services/search/web', query : [ v:'1.0', q: 'Calvin and Hobbes' ], @@ -222,7 +229,7 @@ class HttpURLClientTest { def http = new HttpURLClient() try { - def resp = http.request( url:'http://ajax.googleapis.com', + def resp = http.request( url:'https://ajax.googleapis.com', method:GET, contentType:JSON , Path : '/ajax/services/search/web', query : [ v:'1.0', q: 'Calvin and Hobbes' ] ) @@ -233,6 +240,6 @@ class HttpURLClientTest { @Test(expected = SocketTimeoutException) void testTimeout() { - new HttpURLClient(url: 'https://www.google.com/').request(timeout: 1) + new HttpURLClient(url: 'https://groovy-lang.org/').request(timeout: 1) } } diff --git a/src/test/groovy/groovyx/net/http/RESTClientTest.groovy b/src/test/groovy/groovyx/net/http/RESTClientTest.groovy.dead-sites similarity index 100% rename from src/test/groovy/groovyx/net/http/RESTClientTest.groovy rename to src/test/groovy/groovyx/net/http/RESTClientTest.groovy.dead-sites diff --git a/src/test/groovy/groovyx/net/http/RegistryTest.groovy b/src/test/groovy/groovyx/net/http/RegistryTest.groovy index 24574ac..7625f82 100644 --- a/src/test/groovy/groovyx/net/http/RegistryTest.groovy +++ b/src/test/groovy/groovyx/net/http/RegistryTest.groovy @@ -3,8 +3,11 @@ package groovyx.net.http import org.apache.http.ProtocolVersion import org.apache.http.entity.StringEntity import org.apache.http.message.BasicHttpResponse -import org.junit.Test import java.io.StringReader import java.io.ByteArrayInputStream -import static groovyx.net.http.ContentType.* +import org.junit.Test +import java.io.StringReader +import java.io.ByteArrayInputStream +import static groovyx.net.http.ContentType.* + /** * @author tnichols */ diff --git a/src/test/groovy/groovyx/net/http/thirdparty/GAETest.groovy b/src/test/groovy/groovyx/net/http/thirdparty/GAETest.groovy index b3a5a9f..9294e50 100644 --- a/src/test/groovy/groovyx/net/http/thirdparty/GAETest.groovy +++ b/src/test/groovy/groovyx/net/http/thirdparty/GAETest.groovy @@ -22,7 +22,7 @@ class GAETest { @Ignore @Test public void testURLFetchService() { http = newBuilder() - http.uri = 'http://ajax.googleapis.com/ajax/services/search/web' + http.uri = 'https://ajax.googleapis.com/ajax/services/search/web' http.request(GET) { uri.query = [ v:'1.0', q: 'HTTPBuilder' ] @@ -46,7 +46,7 @@ class GAETest { * This method will parse the content based on the response content-type */ @Test public void testGET() { - def http = newBuilder('http://www.google.com') + def http = newBuilder('https://www.google.com') http.get( path:'/search', query:[q:'Groovy'], headers:['User-Agent':"Firefox"] ) { resp, html -> println "response status: ${resp.statusLine}" @@ -61,7 +61,7 @@ class GAETest { } @Test public void testDefaultSuccessHandler() { - def http = newBuilder('http://www.google.com') + def http = newBuilder('https://www.google.com') def html = http.request( GET ) { headers = ['User-Agent':"Firefox"] uri.path = '/search' @@ -79,7 +79,7 @@ class GAETest { } @Test public void testSetHeaders() { - def http = newBuilder('http://www.google.com') + def http = newBuilder('https://www.google.com') def val = '1' def v2 = 'two' def h3 = 'three' @@ -114,7 +114,7 @@ class GAETest { * TEXT or BINARY. */ @Test public void testReaderWithDefaultResponseHandler() { - def http = newBuilder('http://www.google.com') + def http = newBuilder('https://www.google.com') def reader = http.get( contentType:TEXT ) @@ -127,7 +127,7 @@ class GAETest { } @Test public void testDefaultFailureHandler() { - def http = newBuilder('http://www.google.com') + def http = newBuilder('https://www.google.com') try { http.get( path:'/adsasf/kjsslkd' ) { @@ -147,8 +147,8 @@ class GAETest { * based on the given content-type, i.e. TEXT (text/plain). */ @Test public void testReader() { - def http = newBuilder('http://examples.oreilly.com') - http.get( uri:'http://examples.oreilly.com/9780596002527/examples/first.xml', + def http = newBuilder('https://resources.oreilly.com') + http.get( uri:'https://resources.oreilly.com/examples/9780596002527/raw/master/examples/first.xml', contentType: TEXT, headers: [Accept:'*/*'] ) { resp, reader -> println "response status: ${resp.statusLine}" println 'Headers:' @@ -172,6 +172,7 @@ class GAETest { * Tests POST with XML response, and DELETE with a JSON response. */ + @Ignore // 404 entire site @Test public void testPOST() { def http = newBuilder('https://api.twitter.com/1.1/statuses/') @@ -231,8 +232,8 @@ class GAETest { } } -// @Test - public void testHeadMethod() { + @Ignore // 404 entire site + @Test public void testHeadMethod() { def http = newBuilder('https://api.twitter.com/1.1/statuses/') http.auth.oauth twitter.consumerKey, twitter.consumerSecret, @@ -262,7 +263,7 @@ class GAETest { } // optional default URL for all actions: - http.uri = 'http://www.google.com' + http.uri = 'https://www.google.com' http.request(GET,TEXT) { req -> response.success = { resp, stream -> @@ -278,7 +279,7 @@ class GAETest { * Test a response handler that is assigned within a request config closure: */ @Test public void test404() { - newBuilder().request('http://www.google.com',GET,TEXT) { + newBuilder().request('https://www.google.com',GET,TEXT) { uri.path = '/asdfg/asasdfs' // should produce 404 response.'404' = { println 'got expected 404!' @@ -289,8 +290,8 @@ class GAETest { } } - /* http://googlesystem.blogspot.com/2008/04/google-search-rest-api.html - * http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Earth%20Day + /* https://googlesystem.blogspot.com/2008/04/google-search-rest-api.html + * https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Earth%20Day */ @Ignore @Test public void testJSON() { @@ -299,7 +300,7 @@ class GAETest { // builder.parser.'text/javascript' = builder.parsers."$JSON" - builder.request('http://ajax.googleapis.com',GET,JSON) { + builder.request('https://ajax.googleapis.com',GET,JSON) { uri.path = '/ajax/services/search/web' uri.query = [ v:'1.0', q: 'Calvin and Hobbes' ] //UA header required to get Google to GZIP response: @@ -333,22 +334,23 @@ class GAETest { http.auth.basic( 'user2', 'user2' ) + /* 404 and does NOT do auth http.request( GET, HTML ) { uri.path = '/auth-digest/' } + */ http.request( GET, HTML ) { uri.path = '/auth-basic/' } } + @Ignore // requires auth @Test public void testCatalog() { - def http = newBuilder( 'http://weather.yahooapis.com/forecastrss' ) - - http.parser.addCatalog getClass().getResource( '/rss-catalog.xml') - def xml = http.get( query : [p:'02110',u:'f'] ) - + def http = newBuilder( 'https://weather-ydn-yql.media.yahoo.com/forecastrss' ) + http.parser.addCatalog getClass().getResource( '/rss-catalog.xml') // 404 + def xml = http.get( query : [p:'02110',u:'f'] ) // does not work } def newBuilder( uri ) { @@ -358,4 +360,5 @@ class GAETest { } } } + }