1   /**
2    * Copyright (C) 2008 Atlassian
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.theplugin.util;
18  
19  import com.atlassian.theplugin.commons.remoteapi.RemoteApiMalformedUrlException;
20  import com.atlassian.theplugin.commons.remoteapi.RemoteApiSessionExpiredException;
21  import com.atlassian.theplugin.commons.remoteapi.rest.AbstractHttpSession;
22  import com.atlassian.theplugin.commons.util.HttpClientFactory;
23  import com.atlassian.theplugin.commons.configuration.*;
24  import junit.framework.TestCase;
25  import org.apache.commons.httpclient.HttpMethod;
26  import org.ddsteps.mock.httpserver.JettyMockServer;
27  import org.jdom.Document;
28  import org.jdom.JDOMException;
29  import org.mortbay.jetty.Server;
30  
31  import javax.servlet.http.HttpServletRequest;
32  import javax.servlet.http.HttpServletResponse;
33  import java.io.IOException;
34  import java.net.SocketTimeoutException;
35  
36  /**
37   * Created by IntelliJ IDEA.
38   * User: lguminski
39   * Date: Apr 23, 2008
40   * Time: 3:32:55 PM
41   * To change this template use File | Settings | File Templates.
42   */
43  public class AbstractHttpSessionTest extends TestCase {
44  	private Server httpServer;
45  	private JettyMockServer mockServer;
46  	private static final String SOME_URL = "/some_url";
47  
48  
49  	@Override
50  	protected void setUp() throws Exception {
51  		httpServer = new org.mortbay.jetty.Server(0);
52  		httpServer.start();
53  
54          ConfigurationFactory.setConfiguration(new PluginConfigurationBean());
55          mockServer = new JettyMockServer(httpServer);
56  	}
57  
58      public void testRetrieveGetResponseWithDataTransferTimeout() throws RemoteApiMalformedUrlException, IOException, RemoteApiSessionExpiredException, JDOMException {
59  		int timeout; // 7 sec
60  		long t1;
61  		String mockBaseUrl = "http://localhost:" + httpServer.getConnectors()[0].getLocalPort() + SOME_URL;
62  		mockServer.expect(SOME_URL, new TimeoutingOnDataTransferCallback());
63  		TestHttpSession session = new TestHttpSession(mockBaseUrl);
64  
65  		timeout = 100;
66  		HttpClientFactory.setDataTimeout(timeout);
67  		t1 = System.currentTimeMillis();
68  		try {
69  			session.retrieveGetResponse(mockBaseUrl);
70  			fail("It should fail but it didn't1");
71  		} catch (SocketTimeoutException e) {
72  			long diff = System.currentTimeMillis() - t1;
73  			if (diff > timeout * 20) { //too slow
74  				fail("Timeout doesn't work.");
75  			}
76  		}
77  	}
78  
79  	private class TimeoutingOnDataTransferCallback implements JettyMockServer.Callback {
80  		public void onExpectedRequest(String s, HttpServletRequest request, HttpServletResponse response) throws Exception {
81  			response.setContentType("text/xml");
82  			response.getOutputStream().write("<a/>".getBytes("UTF-8"));
83  			response.getOutputStream().flush();
84  			Thread.sleep(10000);
85  		}
86  	}
87  
88  	private class TestHttpSession extends AbstractHttpSession {
89  
90  		@Override
91  		protected Document retrieveGetResponse(String urlString) throws IOException, JDOMException, RemoteApiSessionExpiredException {
92  			return super.retrieveGetResponse(urlString);	//To change body of overridden methods use File | Settings | File Templates.
93  		}
94  
95  		@Override
96  		protected Document retrievePostResponse(String urlString, Document request) throws IOException, JDOMException, RemoteApiSessionExpiredException {
97  			return super.retrievePostResponse(urlString, request);	//To change body of overridden methods use File | Settings | File Templates.
98  		}
99  
100 		/**
101 		 * Public constructor for AbstractHttpSession
102 		 *
103 		 * @param baseUrl base URL for server instance
104 		 * @throws com.atlassian.theplugin.commons.remoteapi.RemoteApiMalformedUrlException
105 		 *          for malformed url
106 		 */
107 		public TestHttpSession(String baseUrl) throws RemoteApiMalformedUrlException {
108 			super(baseUrl);
109 		}
110 
111 		protected void adjustHttpHeader(HttpMethod method) {
112 
113 		}
114 
115 		protected void preprocessResult(Document doc) throws JDOMException, RemoteApiSessionExpiredException {
116 
117 		}
118 
119 	}
120 }