반응형
Web Server 및 Http Connection Test 하기
테스트를 위한 웹서버를 만든다.
기본 컨셉은 테스트를 하기 위한 작은단위의 서버를 만드는 것이다.
was 는 jetty를 사용한다.
WebClient.class
package test.example.stub; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class WebClient { public String getContent (URL url) { StringBuffer content = new StringBuffer(); try { HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); InputStream is = connection.getInputStream(); byte[] buffer = new byte[2048]; int count; while( -1 != (count = is.read(buffer))) { content.append(new String(buffer, 0, count)); } } catch ( IOException e) { return null; } return content.toString(); } }
TestWebClient.class
package test.example.stub; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.mortbay.jetty.HttpHeaders; import org.mortbay.jetty.Server; import org.mortbay.jetty.handler.AbstractHandler; import org.mortbay.jetty.servlet.Context; import org.mortbay.util.ByteArrayISO8859Writer; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.net.MalformedURLException; import java.net.URL; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; public class TestWebClient { @BeforeClass public static void setUp() throws Exception{ Server server = new Server(8888); TestWebClient t = new TestWebClient(); Context contentOkContext = new Context(server, "/testGetContentOk"); contentOkContext.setHandler(t.new TestGetContentOkHandler()); Context contentNotFoundContext = new Context(server, "/testGetContentNotFound"); contentNotFoundContext.setHandler(t.new TestGetContentNotFoundHandler()); server.setStopAtShutdown(true); server.start(); } @Test public void testGetContentOk() throws MalformedURLException { WebClient client = new WebClient(); URL url = new URL("http://localhost:8888/testGetContentOk"); String result = client.getContent(url); assertEquals("It works", result); } @Test public void testGetContentNotFound() throws MalformedURLException { WebClient client = new WebClient(); URL url = new URL("http://localhost:8888/testGetContentNotFound"); String result = client.getContent(url); assertNull(result); } @AfterClass public static void tearDown(){ // not doing } private class TestGetContentOkHandler extends AbstractHandler { @Override public void handle(String target, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, int dispatch) throws IOException, ServletException { OutputStream out = httpServletResponse.getOutputStream(); ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(); writer.write("It works"); writer.flush(); httpServletResponse.setIntHeader(HttpHeaders.CONTENT_LENGTH, writer.size()); writer.writeTo(out); out.flush(); } } private class TestGetContentNotFoundHandler extends AbstractHandler { @Override public void handle(String target, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, int dispatch) throws IOException, ServletException { httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); } } }
TestWebClient1.class
package test.example.stub; import org.junit.BeforeClass; import org.junit.Test; import java.io.IOException; import java.net.URL; import java.net.URLConnection; import java.net.URLStreamHandler; import java.net.URLStreamHandlerFactory; import static org.junit.Assert.assertEquals; public class TestWebClient1 { @BeforeClass public static void setUp(){ TestWebClient1 t = new TestWebClient1(); URL.setURLStreamHandlerFactory(t.new StubStreamHandlerFactory()); } private class StubStreamHandlerFactory implements URLStreamHandlerFactory { public URLStreamHandler createURLStreamHandler(String protocol) { return new StubHttpURLStreamHandler(); } } private class StubHttpURLStreamHandler extends URLStreamHandler { protected URLConnection openConnection(URL url) throws IOException { return new StubHttpURLConnection(url); } } @Test public void testGetContentOk() throws Exception { WebClient client = new WebClient(); String result = client.getContent(new URL("http://localhost")); assertEquals("It works", result); } }
StubHttpUrlConnection.class
package test.example.stub; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.ProtocolException; import java.net.URL; public class StubHttpURLConnection extends HttpURLConnection{ private boolean isInput = true; protected StubHttpURLConnection(URL u) { super(u); } public InputStream getInputStream() throws IOException { if (!isInput) { throw new ProtocolException("Cannot read from URLConnection" + " if doInput=false (call setDoInput(true))"); } ByteArrayInputStream bais = new ByteArrayInputStream(new String("It works").getBytes()); return bais; } @Override public void disconnect() { } @Override public boolean usingProxy() { return false; } @Override public void connect() throws IOException { } }
반응형
'개발자놀이터' 카테고리의 다른 글
Node.js 강좌 - 1. Node js 시작하기 (0) | 2016.04.23 |
---|---|
ubuntu 에서 apache와 tomcat 연동하기 (0) | 2016.03.10 |
intellij 에서 maven 에 등록한 jar파일이 읽히지 않을 때 (0) | 2015.09.29 |
gitignore 잘 적용되지 않을 때 (0) | 2015.09.26 |
maven 정리 두번째 (0) | 2015.06.22 |