问题单元测试与URI的连接 [英] Issues unit testing connection to URI

查看:198
本文介绍了问题单元测试与URI的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我的Java / Jersey Web服务并运行一个有趣的测试用例。我正在检查不同URI条目的状态代码,以确保不正确的URI不会破坏我的代码或任何东西。在我抛出无效字符(例如!@ $#<>等)的测试用例中,我的浏览器会像我期望的那样引发404错误,但是JUnit将错误显示为500错误。这种情况发生在我抛出诸如< 134->之类的情况以及我尝试注入html代码的情况下(例如 myURI< html>< p> hello< / p>< BR>< / HTML> / restofmyURI )。

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?

推荐答案

只想在这里为我的流程绑定松散的目标。我最终不需要对我的测试进行编码,因为URL将直接调用,而不是通过浏览器调用,但在我意识到之前我已经有了编码方法。根据Kaj的建议(见我原来问题的评论),我将字符串编成了部分,分成了我需要留下未编码的任何字符(即 / 对于这种情况)。它比我认为的要复杂一点,但它达到了它的目的。

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.

- 编辑:相关代码 -

这是我测试的方法,我打电话给你打开一个连接到服务器(使用基本身份验证):

-- 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.

这篇关于问题单元测试与URI的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆