Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle HTTP HEAD requests by setting response status to 200 OK #6390

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
commonAuthenticationHandler.doGet(request, response);
}

@Override
protected void doHead(HttpServletRequest request, HttpServletResponse response) {
response.setStatus(HttpServletResponse.SC_OK);
KD23243 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
protected void doOptions(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.wso2.carbon.identity.application.authentication.framework.servlet;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.wso2.carbon.identity.event.IdentityEventConfigBuilder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
* Unit tests for {@link CommonAuthenticationServlet}.
*/
public class CommonAuthenticationServletTest {

/**
* Sets up the test environment by configuring the carbon home system property
* and initializing the {@link CommonAuthenticationServlet} instance.
*/
@BeforeMethod
public void setUp() {
configureCarbonHome();
}

/**
* Tests the {@code doHead} method to ensure it sets the HTTP status to 200 (OK).
*/
@Test
public void testDoHead() {
HttpServletRequest mockRequest = mock(HttpServletRequest.class);
when(mockRequest.getMethod()).thenReturn("HEAD");
HttpServletResponse spyResponse = spy(HttpServletResponse.class);
CommonAuthenticationServlet servlet = new CommonAuthenticationServlet();
servlet.doHead(mockRequest, spyResponse);
verify(spyResponse, times(1)).setStatus(HttpServletResponse.SC_OK);
}

/**
* Configures the carbon home system property for the test environment.
*/
private void configureCarbonHome() {
String carbonHome = IdentityEventConfigBuilder.class.getResource("/").getFile();
System.setProperty("carbon.home", carbonHome);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<class name="org.wso2.carbon.identity.application.authentication.framework.services.PostAuthenticationMgtServiceTest"/>
<class name="org.wso2.carbon.identity.application.authentication.framework.services.ConditionalAuthenticationMgtServiceTest"/>

<class name="org.wso2.carbon.identity.application.authentication.framework.servlet.CommonAuthenticationServletTest"/>

<class name="org.wso2.carbon.identity.application.authentication.framework.config.ConfigurationFacadeTest"/>
<class name="org.wso2.carbon.identity.application.authentication.framework.config.loader.UIBasedConfigurationLoaderTest"/>
<class name="org.wso2.carbon.identity.application.authentication.framework.config.builder.FileBasedConfigurationBuilderTest"/>
Expand Down
Loading