@@ -0,0 +1,3398 @@
+package no.javabin.infrastructure.http.server;%0A%0Aimport static org.assertj.core.api.Assertions.assertThat;%0Aimport static org.mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.when;%0A%0Aimport java.io.IOException;%0Aimport java.io.PrintWriter;%0Aimport java.io.StringWriter;%0Aimport java.net.MalformedURLException;%0Aimport java.net.URL;%0Aimport java.util.UUID;%0A%0Aimport javax.servlet.ServletException;%0Aimport javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0Aimport no.javabin.heroes.HttpRequestException;%0Aimport org.jsonbuddy.JsonObject;%0Aimport org.jsonbuddy.parse.JsonParser;%0Aimport org.junit.Before;%0Aimport org.junit.Test;%0Aimport org.mockito.Mockito;%0A%0Apublic class ApiServletTest %7B%0A%0A    private ApiServlet servlet = new ApiServlet();%0A    private HttpServletRequest requestMock = Mockito.mock(HttpServletRequest.class);%0A    private HttpServletResponse responseMock = Mockito.mock(HttpServletResponse.class);%0A    private StringWriter responseBody = new StringWriter();%0A%0A    private class ExampleController %7B%0A%0A        @Get(%22/one%22)%0A        public JsonObject one(%0A                @RequestParam(%22name%22) String name%0A        ) %7B%0A            return new JsonObject().put(%22name%22, name);%0A        %7D%0A%0A        @Get(%22/error%22)%0A        public void throwError() %7B%0A            throw new HttpRequestException(401, %22You are not authorized%22);%0A        %7D%0A%0A        // TODO: Avoid using undeclared path parameters%0A        @Get(%22/user/:userId/message/:messageId%22)%0A        public URL privateMethod(%0A                @PathParam(%22userId%22) String userId,%0A                @PathParam(%22messageId%22) String messageId%0A        ) throws MalformedURLException %7B%0A            return new URL(%22https://messages.example.com/?user=%22 + userId + %22&message=%22 + messageId);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void shouldCallMethodWithArgumentsAndConvertReturn() throws ServletException, IOException %7B%0A        String name = UUID.randomUUID().toString();%0A        when(requestMock.getPathInfo()).thenReturn(%22/one%22);%0A        when(requestMock.getParameter(%22name%22)).thenReturn(name);%0A%0A        servlet.doGet(requestMock, responseMock);%0A%0A        assertThat(JsonParser.parseToObject(responseBody.toString()).requiredString(%22name%22))%0A            .isEqualTo(name);%0A        verify(responseMock).setContentType(%22application/json%22);%0A    %7D%0A%0A    @Test%0A    public void shouldOutputErrorToResponse() throws ServletException, IOException %7B%0A        when(requestMock.getPathInfo()).thenReturn(%22/error%22);%0A%0A        servlet.doGet(requestMock, responseMock);%0A        verify(responseMock).sendError(401, %22You are not authorized%22);%0A    %7D%0A%0A    @Test%0A    public void shouldGive404OnUnknownAction() throws ServletException, IOException %7B%0A        when(requestMock.getPathInfo()).thenReturn(%22/missing%22);%0A        servlet.doGet(requestMock, responseMock);%0A        verify(responseMock).sendError(404);%0A    %7D%0A%0A    @Test%0A    public void shouldDecodePathParams() throws ServletException, IOException %7B%0A        when(requestMock.getPathInfo()).thenReturn(%22/user/3341/message/abc%22);%0A        servlet.doGet(requestMock, responseMock);%0A        verify(responseMock).sendRedirect(%22https://messages.example.com/?user=3341&message=abc%22);%0A    %7D%0A%0A    @Before%0A    public void setupRequest() throws IOException %7B%0A        servlet.registerController(new ExampleController());%0A%0A        when(responseMock.getWriter()).thenReturn(new PrintWriter(responseBody));%0A    %7D%0A%7D%0A

Add test for ApiServlet