-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
260 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,6 @@ | |
*/ | ||
public interface HttpRequest { | ||
|
||
|
||
/** | ||
* Returns the port number to which the request was sent. | ||
*/ | ||
|
67 changes: 67 additions & 0 deletions
67
plugin-api/src/test/java/org/sonar/api/server/http/JakartaHttpRequestTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Sonar Plugin API | ||
* Copyright (C) 2009-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.api.server.http; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.util.Enumeration; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class JakartaHttpRequestTest { | ||
|
||
@Test | ||
public void initRequest() { | ||
HttpServletRequest requestMock = mock(HttpServletRequest.class); | ||
when(requestMock.getServerPort()).thenReturn(80); | ||
when(requestMock.isSecure()).thenReturn(true); | ||
when(requestMock.getScheme()).thenReturn("https"); | ||
when(requestMock.getServerName()).thenReturn("hostname"); | ||
when(requestMock.getRequestURL()).thenReturn(new StringBuffer("https://hostname:80/path")); | ||
when(requestMock.getRequestURI()).thenReturn("/path"); | ||
when(requestMock.getQueryString()).thenReturn("param1=value1"); | ||
when(requestMock.getContextPath()).thenReturn("/path"); | ||
when(requestMock.getMethod()).thenReturn("POST"); | ||
when(requestMock.getParameter("param1")).thenReturn("value1"); | ||
when(requestMock.getParameterValues("param1")).thenReturn(new String[]{"value1"}); | ||
when(requestMock.getHeader("header1")).thenReturn("hvalue1"); | ||
Enumeration<String> headers = mock(Enumeration.class); | ||
when(requestMock.getHeaders("header1")).thenReturn(headers); | ||
|
||
JakartaHttpRequest request = new JakartaHttpRequest(requestMock); | ||
|
||
assertThat(request.getRawRequest()).isSameAs(requestMock); | ||
assertThat(request.getServerPort()).isEqualTo(80); | ||
assertThat(request.isSecure()).isTrue(); | ||
assertThat(request.getScheme()).isEqualTo("https"); | ||
assertThat(request.getServerName()).isEqualTo("hostname"); | ||
assertThat(request.getRequestURL()).isEqualTo("https://hostname:80/path"); | ||
assertThat(request.getRequestURI()).isEqualTo("/path"); | ||
assertThat(request.getQueryString()).isEqualTo("param1=value1"); | ||
assertThat(request.getContextPath()).isEqualTo("/path"); | ||
assertThat(request.getMethod()).isEqualTo("POST"); | ||
assertThat(request.getParameter("param1")).isEqualTo("value1"); | ||
assertThat(request.getParameterValues("param1")).containsExactly("value1"); | ||
assertThat(request.getHeader("header1")).isEqualTo("hvalue1"); | ||
assertThat(request.getHeaders("header1")).isEqualTo(headers); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
plugin-api/src/test/java/org/sonar/api/server/http/JakartaHttpResponseTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Sonar Plugin API | ||
* Copyright (C) 2009-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.api.server.http; | ||
|
||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.List; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class JakartaHttpResponseTest { | ||
|
||
@Test | ||
public void initResponse() throws IOException { | ||
HttpServletResponse responseMock = mock(HttpServletResponse.class); | ||
when(responseMock.getHeader("h1")).thenReturn("hvalue1"); | ||
when(responseMock.getHeaders("h1")).thenReturn(List.of("hvalue1")); | ||
when(responseMock.getStatus()).thenReturn(200); | ||
PrintWriter writer = mock(PrintWriter.class); | ||
when(responseMock.getWriter()).thenReturn(writer); | ||
|
||
JakartaHttpResponse response = new JakartaHttpResponse(responseMock); | ||
|
||
assertThat(response.getRawResponse()).isSameAs(responseMock); | ||
assertThat(response.getHeader("h1")).isEqualTo("hvalue1"); | ||
assertThat(response.getHeaders("h1")).asList().containsExactly("hvalue1"); | ||
assertThat(response.getStatus()).isEqualTo(200); | ||
assertThat(response.getWriter()).isEqualTo(writer); | ||
|
||
response.addHeader("h2", "hvalue2"); | ||
response.setHeader("h3", "hvalue3"); | ||
response.setStatus(201); | ||
response.setContentType("text/plain"); | ||
response.sendRedirect("http://redirect"); | ||
verify(responseMock).addHeader("h2", "hvalue2"); | ||
verify(responseMock).setHeader("h3", "hvalue3"); | ||
verify(responseMock).setStatus(201); | ||
verify(responseMock).setContentType("text/plain"); | ||
verify(responseMock).sendRedirect("http://redirect"); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
plugin-api/src/test/java/org/sonar/api/server/http/JavaxHttpRequestTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Sonar Plugin API | ||
* Copyright (C) 2009-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.api.server.http; | ||
|
||
import java.util.Enumeration; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class JavaxHttpRequestTest { | ||
|
||
@Test | ||
public void initRequest() { | ||
HttpServletRequest requestMock = mock(HttpServletRequest.class); | ||
when(requestMock.getServerPort()).thenReturn(80); | ||
when(requestMock.isSecure()).thenReturn(true); | ||
when(requestMock.getScheme()).thenReturn("https"); | ||
when(requestMock.getServerName()).thenReturn("hostname"); | ||
when(requestMock.getRequestURL()).thenReturn(new StringBuffer("https://hostname:80/path")); | ||
when(requestMock.getRequestURI()).thenReturn("/path"); | ||
when(requestMock.getQueryString()).thenReturn("param1=value1"); | ||
when(requestMock.getContextPath()).thenReturn("/path"); | ||
when(requestMock.getMethod()).thenReturn("POST"); | ||
when(requestMock.getParameter("param1")).thenReturn("value1"); | ||
when(requestMock.getParameterValues("param1")).thenReturn(new String[]{"value1"}); | ||
when(requestMock.getHeader("header1")).thenReturn("hvalue1"); | ||
Enumeration<String> headers = mock(Enumeration.class); | ||
when(requestMock.getHeaders("header1")).thenReturn(headers); | ||
|
||
JavaxHttpRequest request = new JavaxHttpRequest(requestMock); | ||
|
||
assertThat(request.getRawRequest()).isSameAs(requestMock); | ||
assertThat(request.getServerPort()).isEqualTo(80); | ||
assertThat(request.isSecure()).isTrue(); | ||
assertThat(request.getScheme()).isEqualTo("https"); | ||
assertThat(request.getServerName()).isEqualTo("hostname"); | ||
assertThat(request.getRequestURL()).isEqualTo("https://hostname:80/path"); | ||
assertThat(request.getRequestURI()).isEqualTo("/path"); | ||
assertThat(request.getQueryString()).isEqualTo("param1=value1"); | ||
assertThat(request.getContextPath()).isEqualTo("/path"); | ||
assertThat(request.getMethod()).isEqualTo("POST"); | ||
assertThat(request.getParameter("param1")).isEqualTo("value1"); | ||
assertThat(request.getParameterValues("param1")).containsExactly("value1"); | ||
assertThat(request.getHeader("header1")).isEqualTo("hvalue1"); | ||
assertThat(request.getHeaders("header1")).isEqualTo(headers); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
plugin-api/src/test/java/org/sonar/api/server/http/JavaxHttpResponseTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Sonar Plugin API | ||
* Copyright (C) 2009-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.api.server.http; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.List; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class JavaxHttpResponseTest { | ||
|
||
@Test | ||
public void initResponse() throws IOException { | ||
HttpServletResponse responseMock = mock(HttpServletResponse.class); | ||
when(responseMock.getHeader("h1")).thenReturn("hvalue1"); | ||
when(responseMock.getHeaders("h1")).thenReturn(List.of("hvalue1")); | ||
when(responseMock.getStatus()).thenReturn(200); | ||
PrintWriter writer = mock(PrintWriter.class); | ||
when(responseMock.getWriter()).thenReturn(writer); | ||
|
||
JavaxHttpResponse response = new JavaxHttpResponse(responseMock); | ||
|
||
assertThat(response.getRawResponse()).isSameAs(responseMock); | ||
assertThat(response.getHeader("h1")).isEqualTo("hvalue1"); | ||
assertThat(response.getHeaders("h1")).asList().containsExactly("hvalue1"); | ||
assertThat(response.getStatus()).isEqualTo(200); | ||
assertThat(response.getWriter()).isEqualTo(writer); | ||
|
||
response.addHeader("h2", "hvalue2"); | ||
response.setHeader("h3", "hvalue3"); | ||
response.setStatus(201); | ||
response.setContentType("text/plain"); | ||
response.sendRedirect("http://redirect"); | ||
verify(responseMock).addHeader("h2", "hvalue2"); | ||
verify(responseMock).setHeader("h3", "hvalue3"); | ||
verify(responseMock).setStatus(201); | ||
verify(responseMock).setContentType("text/plain"); | ||
verify(responseMock).sendRedirect("http://redirect"); | ||
} | ||
} |