/*
* Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.esb.integration.nhttp;
import org.apache.http.HttpRequest;
import org.wso2.esb.integration.ESBIntegrationTest;
import org.wso2.esb.integration.http.RequestInterceptor;
import org.wso2.esb.integration.http.SimpleHttpServer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.rmi.RemoteException;
public class GETRequestQueryParamTest extends ESBIntegrationTest {
@Override
public void init() {
super.init();
log.info("Verifying functionality against the bug: ESBJAVA-641");
}
@Override
public void successfulScenario() throws RemoteException {
updateESBConfiguration("/get_request.xml");
SimpleHttpServer httpServer = new SimpleHttpServer();
try {
httpServer.start();
TestRequestInterceptor interceptor = new TestRequestInterceptor();
httpServer.getRequestHandler().setInterceptor(interceptor);
String path = "/foo/bar";
String query1 = "?param1=value1";
String query2 = "?param1=value1¶m2=value2¶m3=value3";
makeGET(new URL(getProxyServiceURL("GETRequestProxy", false)));
assertEquals(path, interceptor.getLastRequestURI());
makeGET(new URL(getProxyServiceURL("GETRequestProxy", false) + query1));
assertEquals(path + query1, interceptor.getLastRequestURI());
makeGET(new URL(getProxyServiceURL("GETRequestProxy", false) + query2));
assertEquals(path + query2, interceptor.getLastRequestURI());
} catch (IOException e) {
handleError("Error while running the GET request test", e);
} finally {
try {
httpServer.stop();
} catch (IOException e) {
log.warn("Error while shutting down the HTTP server", e);
}
}
}
private void makeGET(URL url) throws IOException {
URLConnection conn = url.openConnection ();
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
log.info("Response received: " + sb.toString());
}
private static class TestRequestInterceptor implements RequestInterceptor {
private String lastRequestURI;
public void requestReceived(HttpRequest request) {
lastRequestURI = request.getRequestLine().getUri();
}
public String getLastRequestURI() {
return lastRequestURI;
}
}
}