Skip to content

Commit

Permalink
fix bad tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jja committed Aug 10, 2020
1 parent 77a526c commit 52eccc8
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 55 deletions.
4 changes: 4 additions & 0 deletions src/main/java/groovyx/net/http/EncoderRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 7 additions & 4 deletions src/test/groovy/groovyx/net/http/AsyncHTTPBuilderTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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/')

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 */ }
}

}
}
31 changes: 18 additions & 13 deletions src/test/groovy/groovyx/net/http/HTTPBuilderTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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:'
Expand All @@ -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/')

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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/'
}

Expand All @@ -315,32 +317,33 @@ 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.")
}
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/')
Expand All @@ -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']

Expand All @@ -374,4 +378,5 @@ class HTTPBuilderTest {
}
assert result == 'bob'
}

}
39 changes: 23 additions & 16 deletions src/test/groovy/groovyx/net/http/HttpURLClientTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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'] )

Expand Down Expand Up @@ -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'
Expand All @@ -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' )
Expand All @@ -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'
}

/**
Expand All @@ -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 "&ouml;": The entity "ouml" was referenced, but not declared.
//'http://validator.w3.org/docs/help.html' // fails &ouml; &mdash;
//'https://validator.w3.org/nu/about.html'
'https://www.w3schools.com/xml/cd_catalog.xml'
,
contentType: TEXT, headers: [Accept:'*/*'] )

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

Expand All @@ -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/')

Expand Down Expand Up @@ -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/"
Expand All @@ -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
Expand All @@ -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' ],
Expand All @@ -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' ] )
Expand All @@ -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)
}
}
7 changes: 5 additions & 2 deletions src/test/groovy/groovyx/net/http/RegistryTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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.Testimport java.io.StringReaderimport 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
*/
Expand Down
Loading

0 comments on commit 52eccc8

Please sign in to comment.