我想提出一些自動化測試來驗證我的服務器處理無效的請求URI。例如,測試人員能夠使用curl發送此作爲他的URL在Windows上:HttpClient,如何發送URI字符串中的非法字符
http://127.0.0.1/C:\an\incorrect\path\file.txt
我已經處理在服務器上處理這個,但還是希望儘可能地增加測試。試圖與HttpClient的做,但HttpClient的不喜歡的無效字符;)
// Run client
String Url = targetHost + filePath + fileName;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Url);
// run test
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(sourceDir.getAbsolutePath() + LusConstants.DIR_SEPERATOR + fileName),-1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(chunked);
httppost.setEntity(reqEntity);
httpclient.execute(httppost);
異常來自HttpClient的:
aused by: java.net.URISyntaxException: Illegal character in path at index 39: http://127.0.0.1:8080//a/deeper/path/an\incorrect\path\file.txt
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
... 4 more
有什麼想法?