-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UNDERTOW-2293] Add tests for asynchronous writing response, and extr…
…a tests for writing response on post before reading input Signed-off-by: Flavia Rainone <[email protected]>
- Loading branch information
Showing
7 changed files
with
438 additions
and
26 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
...t/src/test/java/io/undertow/servlet/test/response/writer/AsyncExceptionWriterServlet.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,51 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed 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 io.undertow.servlet.test.response.writer; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
|
||
/** | ||
* Asynchronous version of {@link ExceptionWriterServlet}. | ||
* | ||
* @author rmartinc | ||
*/ | ||
public class AsyncExceptionWriterServlet extends jakarta.servlet.http.HttpServlet { | ||
|
||
@Override | ||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { | ||
final var asyncContext = req.startAsync(); | ||
new Thread(()->{ | ||
try { | ||
resp.setContentType("text/plain;charset=UTF-8"); | ||
try (PrintWriter writer = resp.getWriter()) { | ||
new Exception("TestException").printStackTrace(writer); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
asyncContext.complete(); | ||
} | ||
}).start(); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...c/test/java/io/undertow/servlet/test/response/writer/AsyncLargeResponseWriterServlet.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,50 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed 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 io.undertow.servlet.test.response.writer; | ||
|
||
import jakarta.servlet.AsyncContext; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Asynchronous version of {@link LargeResponseWriterServlet}. | ||
* | ||
* @author Flavia Rainone | ||
*/ | ||
public class AsyncLargeResponseWriterServlet extends LargeResponseWriterServlet { | ||
|
||
@Override | ||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { | ||
final AsyncContext asyncContext = req.startAsync(); | ||
new Thread(()->{ | ||
try { | ||
String msg = getMessage(); | ||
resp.setContentLength(msg.length()); | ||
resp.getWriter().write(msg); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
asyncContext.complete(); | ||
} | ||
}).start(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
.../test/java/io/undertow/servlet/test/response/writer/AsyncResponseWriterOnPostServlet.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,64 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2012 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed 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 io.undertow.servlet.test.response.writer; | ||
|
||
import jakarta.servlet.AsyncContext; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Asynchronous version of {@link ResponseWriterOnPostServlet}. | ||
* | ||
* @author Flavia Rainone | ||
*/ | ||
public class AsyncResponseWriterOnPostServlet extends ResponseWriterOnPostServlet { | ||
|
||
@Override | ||
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { | ||
String test = req.getParameter("test"); | ||
if (!test.equals(CONTENT_LENGTH_FLUSH)) { | ||
throw new IllegalArgumentException("not a test " + test); | ||
} | ||
final AsyncContext asyncContext = req.startAsync(); | ||
new Thread(()->{ | ||
try { | ||
HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse(); | ||
contentLengthFlush(response); | ||
// read after writing the response (UNDERTOW-2243) | ||
while (true) { | ||
if (req.getInputStream().readLine(new byte[100], 0, 100) == -1) { | ||
req.getInputStream().close(); | ||
break; | ||
} | ||
} | ||
} catch (RuntimeException e) { | ||
exception = e; | ||
throw e; | ||
} catch (Throwable t) { | ||
exception = t; | ||
throw new RuntimeException(t); | ||
} finally { | ||
asyncContext.complete(); | ||
} | ||
}).start(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...et/src/test/java/io/undertow/servlet/test/response/writer/AsyncResponseWriterServlet.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,53 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed 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 io.undertow.servlet.test.response.writer; | ||
|
||
import jakarta.servlet.AsyncContext; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Asynchronous version of {@link ResponseWriterServlet}. | ||
* | ||
* @author Flavia Rainone | ||
*/ | ||
public class AsyncResponseWriterServlet extends ResponseWriterServlet { | ||
|
||
@Override | ||
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { | ||
|
||
String test = req.getParameter("test"); | ||
if (!test.equals(CONTENT_LENGTH_FLUSH)) { | ||
throw new IllegalArgumentException("not a test " + test); | ||
} | ||
final AsyncContext asyncContext = req.startAsync(); | ||
new Thread(()->{ | ||
try { | ||
contentLengthFlush((HttpServletResponse) asyncContext.getResponse()); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
asyncContext.complete(); | ||
} | ||
}).start(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...t/src/test/java/io/undertow/servlet/test/response/writer/ResponseWriterOnPostServlet.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 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2023 Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. | ||
* | ||
* Licensed 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 io.undertow.servlet.test.response.writer; | ||
|
||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Servlet that writes response on post, before reading input. | ||
* | ||
* @author Flavia Rainone | ||
*/ | ||
public class ResponseWriterOnPostServlet extends ResponseWriterServlet { | ||
|
||
protected static Throwable exception = null; | ||
|
||
@Override | ||
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException { | ||
|
||
String test = req.getParameter("test"); | ||
if (test.equals(CONTENT_LENGTH_FLUSH)) { | ||
contentLengthFlush(resp); | ||
// read after writing the response (UNDERTOW-2243) | ||
try { | ||
while (true) { | ||
if (req.getInputStream().readLine(new byte[100], 0, 100) == -1) { | ||
req.getInputStream().close(); | ||
break; | ||
} | ||
|
||
} | ||
} catch (Throwable e) { | ||
exception = e; | ||
throw e; | ||
} | ||
} else { | ||
throw new IllegalArgumentException("not a test " + test); | ||
} | ||
} | ||
|
||
public static Throwable getExceptionIfAny() { | ||
return exception; | ||
} | ||
} |
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
Oops, something went wrong.