1

I'm unit testing my Java/Jersey web service and running into an interesting test case. I'm checking the status codes of different URI entries to make sure that incorrect URIs won't destroy my code or anything. In test cases where I throw in invalid characters (such as !@$#<>, etc.), my browser pulls up a 404 error like I would expect, but JUnit is showing the error as a 500 error. This happens in cases where I throw in things like "<134->" and cases where I try injecting html code (such as "myURI<html><p>hello</p><br></html>/restofmyURI").

Any ideas why I would be getting different server responses for the same call, and/or how to consolidate the responses?

ZKSteffel
  • 1,115
  • 5
  • 13
  • 22
  • 2
    Do you encode the uri requests in your test cases? – Kaj Jun 03 '11 at 18:29
  • I haven't been. I'd be jumping right on that if this were a website with a direct user interface, but the URIs will be called from another service (my webservice is effectively a middleman, reading from a database and spitting out XML), so I don't know if their calls will be encoded. Do you think the encoding's the only thing causing the discrepancy? – ZKSteffel Jun 03 '11 at 18:35
  • Could be, but I can't say that that definately is the only difference. – Kaj Jun 03 '11 at 18:48
  • Any suggestions on how to try and implement that easily? Currently, I'm reading in the URL as `URL url = new URL("http://myurl/otherparameters/here");` then opening it as `url.openConnection`. Is there a way to encode that within the first line, perchance? – ZKSteffel Jun 03 '11 at 18:51
  • `URLEncoder.encode(urlString, ENCODING)` – Kaj Jun 03 '11 at 18:54
  • Just tried encoding the string as UTF-8 before creating the URL connection, and I got a "MalformedURLException: no protocol" in my failure trace. A closer look reveals that the `/`s in my path all get replaced... is there a better encoding that keeps them in there? – ZKSteffel Jun 03 '11 at 19:15
  • 2
    I wrote a method that splits on '/', encodes each part, and then concatenates them again. – Kaj Jun 03 '11 at 19:18
  • Ah, alright. I'll give that a shot, then. – ZKSteffel Jun 03 '11 at 19:30
  • Can you give us your JUnit test code so we can see exactly what you are doing? – Jesse Webb Jun 08 '11 at 19:29
  • Can't find my encoded version at the moment, but I'll throw in some code in my answer. – ZKSteffel Jun 08 '11 at 19:34

2 Answers2

1

Just want to tie up loose ends for my process here. I ended up not needing to encode my tests since the URLs would be called directly, not through a browser, but I had an encoding method in place before I realized that. Based upon Kaj's suggestion (see comments on my original question), I encoded the string in parts, splitting at any characters I needed to leave unencoded (namely : and / for this case). It's a bit more complicated than I feel it needs to be, but it served its purpose.

--Edit: Relevant Code--
Here is the method of my test which I call to open a connection to the server (using basic authentication):

private void getConnection(String target, String user, String pass) throws Exception  
{  
  URL url = new URL(target);  
  URLConnection conn = url.openConnection();  
  //Here's where basic auth kicks in  
  String creds = user + ":" + pass;  
  String encoded = new sun.misc.BASE64Encoder().encode(creds.getBytes());  
  conn.setRequestProperty("Authorization", "Basic " + encoded);  
  //This part doesn't rely on authentication  
  conn.connect();  
}  

The encoding (if necessary) would be done before being passed into this method.

Gangnus
  • 24,044
  • 16
  • 90
  • 149
ZKSteffel
  • 1,115
  • 5
  • 13
  • 22
  • 1
    you also might also want to check this excellent post: [http-url-address-encoding-in-java](http://stackoverflow.com/questions/724043/http-url-address-encoding-in-java) – asgs Jun 08 '11 at 20:00
  • @asgs Well that is much better than what I was using before. Thanks for the reference. – ZKSteffel Jun 08 '11 at 20:05
0

Instead of encoding the String directly, encode the url object:

That should help prevent the MalformedURLException:

try {
    URL url = new URL("http://myurl/otherparameters/here"); 
    String encodedurl = URLEncoder.encode(url.toString(),"UTF-8");  
} catch(MalformedURLException mue) {
    System.err.println(mue);
} catch(UnsupportedEncodingException uee) {
    System.err.println(uee);
}
ℛɑƒæĿᴿᴹᴿ
  • 4,983
  • 4
  • 38
  • 58
kensen john
  • 5,439
  • 5
  • 28
  • 36
  • 1
    I can't use the String to open a connection, though, without changing it back to a URL. I still get the MalformedURLException since the encoding encodes the `:` and `/` that are necessary for my URL to be defined properly. – ZKSteffel Jun 03 '11 at 20:32