BAEL-2982 NanoHTTPD guide (#7148)
This commit is contained in:
parent
efe62f6dc4
commit
b748c86ce3
|
@ -96,6 +96,19 @@
|
|||
<artifactId>smack-java7</artifactId>
|
||||
<version>${smack.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- NanoHTTPD -->
|
||||
<dependency>
|
||||
<groupId>org.nanohttpd</groupId>
|
||||
<artifactId>nanohttpd</artifactId>
|
||||
<version>${nanohttpd.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.nanohttpd</groupId>
|
||||
<artifactId>nanohttpd-nanolets</artifactId>
|
||||
<version>${nanohttpd.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
@ -108,6 +121,7 @@
|
|||
<tomcat.version>8.5.24</tomcat.version>
|
||||
<smack.version>4.3.1</smack.version>
|
||||
<eclipse.paho.client.mqttv3.version>1.2.0</eclipse.paho.client.mqttv3.version>
|
||||
<nanohttpd.version>2.3.1</nanohttpd.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.nanohttpd;
|
||||
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
import fi.iki.elonen.router.RouterNanoHTTPD;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ApplicationController extends RouterNanoHTTPD {
|
||||
|
||||
ApplicationController() throws IOException {
|
||||
super(8072);
|
||||
addMappings();
|
||||
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addMappings() {
|
||||
addRoute("/", IndexHandler.class);
|
||||
addRoute("/users", UserHandler.class);
|
||||
}
|
||||
|
||||
public static class UserHandler extends DefaultHandler {
|
||||
@Override
|
||||
public String getText() {
|
||||
return "UserA, UserB, UserC";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMimeType() {
|
||||
return MIME_PLAINTEXT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response.IStatus getStatus() {
|
||||
return Response.Status.OK;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
package com.baeldung.nanohttpd;
|
||||
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ItemGetController extends NanoHTTPD {
|
||||
|
||||
ItemGetController() throws IOException {
|
||||
super(8071);
|
||||
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response serve(IHTTPSession session) {
|
||||
if (session.getMethod() == Method.GET) {
|
||||
String itemIdRequestParam = session.getParameters().get("itemId").get(0);
|
||||
return newFixedLengthResponse("Requested itemId = " + itemIdRequestParam);
|
||||
}
|
||||
return newFixedLengthResponse(Response.Status.NOT_FOUND, MIME_PLAINTEXT, "The requested resource does not exist");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.nanohttpd;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class ApplicationControllerUnitTest {
|
||||
|
||||
private static final String BASE_URL = "http://localhost:8072/";
|
||||
private static final HttpClient CLIENT = HttpClientBuilder.create().build();
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws IOException {
|
||||
new ApplicationController();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServer_whenRootRouteRequested_thenHelloWorldReturned() throws IOException {
|
||||
HttpResponse response = CLIENT.execute(new HttpGet(BASE_URL));
|
||||
assertTrue(IOUtils.toString(response.getEntity().getContent()).contains("Hello world!"));
|
||||
assertEquals(200, response.getStatusLine().getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServer_whenUsersRequested_thenThenAllUsersReturned() throws IOException {
|
||||
HttpResponse response = CLIENT.execute(new HttpGet(BASE_URL + "users"));
|
||||
assertEquals("UserA, UserB, UserC", IOUtils.toString(response.getEntity().getContent()));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.nanohttpd;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.HttpClientBuilder;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class ItemGetControllerUnitTest {
|
||||
|
||||
private static final String URL = "http://localhost:8071";
|
||||
private static final HttpClient CLIENT = HttpClientBuilder.create().build();
|
||||
|
||||
@BeforeClass
|
||||
public static void setUp() throws IOException {
|
||||
new ItemGetController();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServer_whenDoingGet_thenParamIsReadCorrectly() throws IOException {
|
||||
HttpResponse response = CLIENT.execute(new HttpGet(URL + "?itemId=1234"));
|
||||
assertEquals("Requested itemId = 1234", IOUtils.toString(response.getEntity().getContent()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenServer_whenDoingPost_then404IsReturned() throws IOException {
|
||||
HttpResponse response = CLIENT.execute(new HttpPost(URL));
|
||||
assertEquals(404, response.getStatusLine().getStatusCode());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue