1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
38
39
40
41
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;
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) {
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);
93 }
94
95 @Override
96 protected Document retrievePostResponse(String urlString, Document request) throws IOException, JDOMException, RemoteApiSessionExpiredException {
97 return super.retrievePostResponse(urlString, request);
98 }
99
100
101
102
103
104
105
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 }