Created
January 30, 2014 14:06
-
-
Save canwe/8709024 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.diese.ems.setup.structure.employee.sbt; | |
import com.sun.net.httpserver.HttpExchange; | |
import com.sun.net.httpserver.HttpHandler; | |
import com.sun.net.httpserver.HttpServer; | |
import org.apache.commons.io.IOUtils; | |
import org.junit.Test; | |
import java.io.IOException; | |
import java.net.InetSocketAddress; | |
import java.util.HashMap; | |
import java.util.Map; | |
import static org.junit.Assert.assertEquals; | |
/** | |
* @author Dmitry Tsydzik | |
* @since Date: 12.06.13 | |
*/ | |
public class HttpServiceTest { | |
private HttpService httpService = new HttpServiceImpl(); | |
private String method; | |
private Map<String, String> params; | |
private String response = "response"; | |
private HttpHandler handler = new HttpHandler() { | |
@Override | |
public void handle(HttpExchange httpExchange) throws IOException { | |
method = httpExchange.getRequestMethod(); | |
String request = IOUtils.toString(httpExchange.getRequestBody()); | |
params = new HashMap<String, String>(); | |
for (String param : request.split("&")) { | |
String[] pair = param.split("="); | |
params.put(pair[0], pair[1]); | |
} | |
httpExchange.sendResponseHeaders(200, response.length()); | |
IOUtils.write(response, httpExchange.getResponseBody()); | |
} | |
}; | |
@Test(timeout = 1000) | |
public void testPost() throws IOException { | |
HttpServer server = HttpServer.create(new InetSocketAddress(36484), 0); | |
server.createContext("/", handler); | |
server.setExecutor(null); | |
server.start(); | |
HttpServiceResponse serviceResponse = httpService.post("http://localhost:36484", "parameter", "value"); | |
server.stop(0); | |
assertEquals("POST", method); | |
assertEquals(response, serviceResponse.getBody()); | |
assertEquals("value", params.get("parameter")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment