### Question:
ConfigurationServlet extends PassThroughAuthenticationServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doWithAuthentication(request, response, this::updateConfiguration); } @SuppressWarnings("unused") // production constructor  ConfigurationServlet();   ConfigurationServlet(WebClientFactory webClientFactory);   }### Answer:
@Test(expected = ServletException.class) public void whenPostWithoutFile_reportFailure() throws Exception { servlet.doPost(createPostRequest(), response); }
@Test public void afterSelectedFileHasBadBooleanValue_configurationIsUnchanged() throws Exception { LiveConfiguration.loadFromString(CONFIGURATION); servlet.doPost(createUploadRequest(createEncodedForm("append", ADDED_CONFIGURATION_WITH_BAD_BOOLEAN)), response); assertThat(LiveConfiguration.asString(), equalTo(CONFIGURATION)); }
@Test public void whenServerSends403StatusOnGet_returnToClient() throws Exception { factory.reportNotAuthorized(); servlet.doPost(request, response); assertThat(response.getStatus(), equalTo(SC_FORBIDDEN)); }
@Test public void whenServerSends401StatusOnGet_returnToClient() throws Exception { factory.reportAuthenticationRequired("Test-Realm"); servlet.doPost(request, response); assertThat(response.getStatus(), equalTo(SC_UNAUTHORIZED)); assertThat(response, containsHeader("WWW-Authenticate", "Basic realm=\"Test-Realm\"")); }
@Test public void whenRequestUsesHttp_authenticateWithHttp() throws Exception { servlet.doPost(createUploadRequest(createEncodedForm("replace", CONFIGURATION)), response); assertThat(factory.getClientUrl(), Matchers.startsWith("http:")); }
@Test public void whenRequestUsesHttps_authenticateWithHttps() throws Exception { HttpServletRequestStub request = createUploadRequest(createEncodedForm("replace", CONFIGURATION)); request.setSecure(true); servlet.doPost(request, response); assertThat(factory.getClientUrl(), Matchers.startsWith("https:")); }
@Test public void afterUploadWithReplace_useNewConfiguration() throws Exception { servlet.doPost(createUploadRequest(createEncodedForm("replace", CONFIGURATION)), response); assertThat(LiveConfiguration.asString(), equalTo(CONFIGURATION)); }
@Test public void afterUpload_redirectToMainPage() throws Exception { servlet.doPost(createUploadRequest(createEncodedForm("replace", CONFIGURATION)), response); assertThat(response.getRedirectLocation(), equalTo("")); }
@Test public void whenRestPortInaccessible_switchToLocalPort() throws Exception { LiveConfiguration.loadFromString(CONFIGURATION_WITH_REST_PORT); factory.throwConnectionFailure("localhost", REST_PORT); servlet.doPost(createUploadRequest(createEncodedForm("replace", CONFIGURATION_WITH_REST_PORT)), response); assertThat(createAuthenticationUrl(), containsString(Integer.toString(LOCAL_PORT))); }
@Test public void afterUploadWithAppend_useBothConfiguration() throws Exception { LiveConfiguration.loadFromString(CONFIGURATION); servlet.doPost(createUploadRequest(createEncodedForm("append", ADDED_CONFIGURATION)), createServletResponse()); assertThat(LiveConfiguration.asString(), equalTo(COMBINED_CONFIGURATION)); }
@Test public void whenSelectedFileIsNotYaml_reportError() throws Exception { LiveConfiguration.loadFromString(CONFIGURATION); servlet.doPost(createUploadRequest(createEncodedForm("append", NON_YAML)), response); assertThat(response.getHtml(), containsString(ConfigurationException.NOT_YAML_FORMAT)); }
@Test public void whenSelectedFileHasPartialYaml_reportError() throws Exception { LiveConfiguration.loadFromString(CONFIGURATION); servlet.doPost(createUploadRequest(createEncodedForm("append", PARTIAL_YAML)), response); assertThat(response.getHtml(), containsString(ConfigurationException.BAD_YAML_FORMAT)); }
@Test public void whenSelectedFileHasBadBooleanValue_reportError() throws Exception { LiveConfiguration.loadFromString(CONFIGURATION); servlet.doPost(createUploadRequest(createEncodedForm("append", ADDED_CONFIGURATION_WITH_BAD_BOOLEAN)), response); assertThat(response.getHtml(), containsString("blabla")); }